-
Notifications
You must be signed in to change notification settings - Fork 0
/
2785.cpp
64 lines (64 loc) · 1.27 KB
/
2785.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <stdio.h>
#include <algorithm>
#define MAX 4010
#define MAX2 16000100
using namespace std;
int A[MAX];
int B[MAX];
int C[MAX];
int D[MAX];
int CD[MAX2];
int n;
int search_left(int tar){
int ans=1000000000;
int left=0;
int right=n*n-1;
while(left<=right){
int mid=(left+right)/2;
if(CD[mid]==tar){
ans=min(ans,mid);
right=mid-1;
}else if(CD[mid]<tar){
left=mid+1;
}else right=mid-1;
}
return ans;
}
int search_right(int tar){
int ans=-1;
int left=0;
int right=n*n-1;
while(left<=right){
int mid=(left+right)/2;
if(CD[mid]==tar){
ans=max(ans,mid);
left=mid+1;
}else if(CD[mid]<tar){
left=mid+1;
}else right=mid-1;
}
return ans;
}
int main(){
cin >> n;
for(int i=0;i<n;i++){
scanf("%d%d%d%d",&A[i],&B[i],&C[i],&D[i]);
}
for(int j=0;j<n;j++){
for(int k=0;k<n;k++){
CD[j*n+k]=C[j]+D[k];
}
}
sort(CD,CD+n*n);
int tmp;
int ans=0;
for(int j=0;j<n;j++){
for(int k=0;k<n;k++){
tmp=A[j]+B[k];
int tmp2=search_right(-tmp)-search_left(-tmp)+1;
if(tmp2!=-1000000000)ans+=tmp2;
}
}
printf("%d\n",ans);
}