#include <bits/stdc++.h> using namespace std; int n; int x,y,z; int A[500000]; int B[500000]; int a,b; int wynik; int mid; int ile (int szuk){ int p=0; int k=a-1; while(p<k){ mid= (p+k)/2; if(A[mid]>=szuk){ k=mid; } else{p=mid+1;} } if((A[k]==szuk)){ int c=k; p=0; k=a-1; while(p<k){ mid= (p+k+1)/2; if(A[mid]<=szuk){ p=mid; } else{k=mid-1;} } int d=p; return d-c+1; } else{return 0;} } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cin>>n; for(int i=0;i<n;i++){ cin>>x>>y>>z; if(x==1) { A[a]=y-z; a++; } if(x==2) { B[b]=y-z; b++; } } sort(A,A+a); sort(B,B+b); for(int i=0;i<b;i++){ int cos=ile(B[i]); int p=0; int k=b-1; while(p<k){ mid= (p+k+1)/2; if(B[mid]<=B[i]){ p=mid; } else{k=mid-1;} } int cos2=p; int cos3=cos2-i+1; i=cos2; wynik=wynik+min(cos, cos3); } cout<<wynik; }
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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | #include <bits/stdc++.h> using namespace std; int n; int x,y,z; int A[500000]; int B[500000]; int a,b; int wynik; int mid; int ile (int szuk){ int p=0; int k=a-1; while(p<k){ mid= (p+k)/2; if(A[mid]>=szuk){ k=mid; } else{p=mid+1;} } if((A[k]==szuk)){ int c=k; p=0; k=a-1; while(p<k){ mid= (p+k+1)/2; if(A[mid]<=szuk){ p=mid; } else{k=mid-1;} } int d=p; return d-c+1; } else{return 0;} } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cin>>n; for(int i=0;i<n;i++){ cin>>x>>y>>z; if(x==1) { A[a]=y-z; a++; } if(x==2) { B[b]=y-z; b++; } } sort(A,A+a); sort(B,B+b); for(int i=0;i<b;i++){ int cos=ile(B[i]); int p=0; int k=b-1; while(p<k){ mid= (p+k+1)/2; if(B[mid]<=B[i]){ p=mid; } else{k=mid-1;} } int cos2=p; int cos3=cos2-i+1; i=cos2; wynik=wynik+min(cos, cos3); } cout<<wynik; } |