Advertisement
royalsflush

NKMOBILE com Segtree IV (0)

Sep 7th, 2012
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.87 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <algorithm>
  3. #include <string.h>
  4. using namespace std;
  5.  
  6. int tree[2*1024+10][2*1024+10];
  7. int s;
  8. int instr;
  9.  
  10. int queryY(int rootX, int rootY, int b, int t, int qb, int qt) {
  11.     if (qb<=b && qt>=t)
  12.         return tree[rootX][rootY];
  13.  
  14.     int midY=(b+t)/2;
  15.     int sum=0;
  16.  
  17.     if (qb<=midY) sum+=queryY(rootX,2*rootY+1,b,midY,qb,min(qt,midY));
  18.     if (qt>midY) sum+=queryY(rootX,2*rootY+2,midY+1,t,max(qb,midY+1),qt);
  19.  
  20.     return sum;
  21. }
  22.  
  23. int query(int rootX, int rootY, int l, int r, int b, int t,
  24.         int ql, int qr, int qb, int qt) {
  25.  
  26.     if (ql<=l && qr>=r)
  27.         return queryY(rootX,rootY,b,t,qb,qt);
  28.  
  29.     int midX = (l+r)/2;
  30.     int sum=0;
  31.  
  32.     if (ql<=midX) sum+=query(2*rootX+1,rootY,l,midX,b,t,ql,min(qr,midX),qb,qt);
  33.     if (qr>midX) sum+=query(2*rootX+2,rootY,midX+1,r,b,t,max(midX+1,ql),qr,qb,qt);
  34.  
  35.     return sum;
  36. }
  37.  
  38. void updateY(int rootX, int rootY, int b, int t, int y, int val) {
  39.     if (b==t) {
  40.         tree[rootX][rootY]+=val;
  41.         return;
  42.     }
  43.  
  44.     int midY=(b+t)/2;
  45.  
  46.     if (y<=midY) updateY(rootX,2*rootY+1,b,midY,y,val);
  47.     else updateY(rootX,2*rootY+2,midY+1,t,y,val);
  48.    
  49.     tree[rootX][rootY]=tree[rootX][2*rootY+1]+tree[rootX][2*rootY+2];
  50. }
  51.  
  52. void update(int rootX, int rootY, int l, int r, int b, int t,
  53.         int x, int y, int val) {
  54.    
  55.     updateY(rootX,rootY,b,t,y,val);
  56.     if (l==r) return;
  57.  
  58.     int midX = (l+r)/2;
  59.  
  60.     if (x<=midX) update(2*rootX+1,rootY,l,midX,b,t,x,y,val);
  61.     else update(2*rootX+2,rootY,midX+1,r,b,t,x,y,val);
  62.  
  63.     tree[rootX][rootY]=tree[2*rootX+1][rootY]+tree[2*rootX+2][rootY];
  64. }
  65.  
  66. int main() {
  67.     while (1) {
  68.         scanf("%d", &instr);
  69.  
  70.         if (instr==3)
  71.             return 0;
  72.         else if (instr==0)
  73.             scanf("%d", &s);
  74.         else if (instr==1) {
  75.             int x,y,a;
  76.             scanf("%d %d %d", &x,&y,&a);
  77.             update(0,0,0,s-1,0,s-1,x,y,a);
  78.         }
  79.         else {
  80.             int l,b,r,t;
  81.             scanf("%d %d %d %d", &l,&b,&r,&t);
  82.             printf("%d\n", query(0,0,0,s-1,0,s-1,
  83.                         l,r,b,t));
  84.         }
  85.     }
  86.     return 0;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement