标签:
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1287 Accepted Submission(s): 290
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 const int maxn = 200010; 5 struct QU { 6 int x1,x2,y,id,f; 7 QU(int a = 0,int b = 0,int c = 0,int d = 0,int e = 0) { 8 x1 = a; 9 x2 = b; 10 y = c; 11 id = d; 12 f = e; 13 } 14 bool operator<(const QU &t)const { 15 return y < t.y; 16 } 17 } Q[maxn],A[maxn],B[maxn]; 18 LL C[maxn],ans[maxn]; 19 void add(int i,int val) { 20 while(i < maxn) { 21 C[i] += val; 22 i += i&-i; 23 } 24 } 25 LL sum(int i,LL ret = 0) { 26 while(i > 0) { 27 ret += C[i]; 28 i -= i&-i; 29 } 30 return ret; 31 } 32 void cdq(int L,int R) { 33 if(R <= L) return; 34 int mid = (L + R)>>1; 35 cdq(L,mid); 36 cdq(mid+1,R); 37 int a = 0,b = 0,j = 0; 38 for(int i = L; i <= mid; ++i) 39 if(Q[i].id == -1) A[a++] = Q[i]; 40 for(int i = mid + 1; i <= R; ++i) 41 if(Q[i].id != -1) B[b++] = Q[i]; 42 sort(A,A + a); 43 sort(B,B + b); 44 for(int i = 0; i < b; ++i) { 45 for(; j < a && A[j].y <= B[i].y; ++j) add(A[j].x1,A[j].f); 46 ans[B[i].id] += B[i].f*sum(B[i].x2); 47 ans[B[i].id] -= B[i].f*sum(B[i].x1); 48 } 49 for(int i = 0; i < j; ++i) add(A[i].x1,-A[i].f); 50 } 51 int main() { 52 int n,m,op,x,y,z,tot,ask; 53 while(scanf("%d",&n),n) { 54 scanf("%d",&m); 55 ask = tot = 0; 56 memset(ans,0,sizeof ans); 57 while(m--) { 58 scanf("%d%d%d%d",&op,&x,&y,&z); 59 if(op == 1) Q[tot++] = QU(x + y,0,y - x,-1,z); 60 else { 61 int cx = x + y; 62 int cy = y - x; 63 int x1 = cx - z; 64 int x2 = cx + z; 65 int y1 = cy - z; 66 int y2 = cy + z; 67 Q[tot++] = QU(x1-1,x2,y2,ask,1); 68 Q[tot++] = QU(x1-1,x2,y1-1,ask++,-1); 69 } 70 } 71 cdq(0,tot-1); 72 for(int i = 0; i < ask; ++i) 73 printf("%I64d\n",ans[i]); 74 } 75 return 0; 76 }
标签:
原文地址:http://www.cnblogs.com/crackpotisback/p/4779635.html