Advertisement
royalsflush

HOMEM SPOJ Br AC

Sep 7th, 2012
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.07 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <algorithm>
  3. #include <string.h>
  4. using namespace std;
  5.  
  6. int n,m;
  7. int tree[20*100010][2];
  8. int needsUp[20*100010];
  9. int qVec[3];
  10.  
  11. inline void shift(int root,int l, int r, int qtd) {
  12.     int v1=tree[root][0];
  13.     int v2=tree[root][1];
  14.     int v0=(r-l+1)-v1-v2;
  15.  
  16.     if (qtd==1) {
  17.         tree[root][0]=v0;
  18.         tree[root][1]=v1;
  19.     }
  20.     else
  21.         if (qtd==2) {
  22.             tree[root][0]=v2;
  23.             tree[root][1]=v0;
  24.         }
  25. }
  26.  
  27. inline void propagate(int root, int l, int r) {
  28.     shift(root,l,r,needsUp[root]);
  29.  
  30.     if (l<r) {
  31.         needsUp[2*root+1]+=needsUp[root];
  32.         if (needsUp[2*root+1]>=3) needsUp[2*root+1]-=3;
  33.         needsUp[2*root+2]+=needsUp[root];
  34.         if (needsUp[2*root+2]>=3) needsUp[2*root+2]-=3;
  35.     }
  36.     needsUp[root]=0;   
  37. }
  38.  
  39. void update(int root, int l, int r, int ql, int qr) {
  40.     if (ql<=l && qr>=r) {
  41.         shift(root,l,r,1);
  42.    
  43.         if (l<r) {
  44.             needsUp[2*root+1]++;
  45.             if (needsUp[2*root+1]>=3) needsUp[2*root+1]-=3;
  46.             needsUp[2*root+2]++;
  47.             if (needsUp[2*root+2]>=3) needsUp[2*root+2]-=3;
  48.         }
  49.  
  50.         return;
  51.     }
  52.  
  53.     int mid=(l+r)/2;
  54.  
  55.     if (ql<=mid) update(2*root+1,l,mid,ql,min(qr,mid));
  56.     if (qr>mid) update(2*root+2,mid+1,r,max(mid+1,ql),qr);
  57.  
  58.     propagate(2*root+1,l,mid);
  59.     propagate(2*root+2,mid+1,r);
  60.  
  61.     tree[root][0]=tree[2*root+1][0]+tree[2*root+2][0];
  62.     tree[root][1]=tree[2*root+1][1]+tree[2*root+2][1];
  63. }
  64.  
  65. void query(int root, int l, int r, int ql, int qr) {
  66.     propagate(root,l,r);
  67.    
  68.     if (ql<=l && qr>=r) {
  69.         qVec[1]+=tree[root][0];
  70.         qVec[2]+=tree[root][1];
  71.         return;
  72.     }  
  73.  
  74.     int mid=(l+r)/2;
  75.  
  76.     if (ql<=mid) query(2*root+1,l,mid,ql,min(qr,mid));
  77.     if (qr>mid) query(2*root+2,mid+1,r,max(mid+1,ql),qr);
  78. }
  79.  
  80. int main() {
  81.     while (scanf("%d %d", &n,&m)!=EOF) {
  82.         memset(tree,0,sizeof(tree));
  83.         memset(needsUp,0,sizeof(needsUp));
  84.  
  85.         for (int i=0; i<m; i++) {
  86.             char c; int a,b;
  87.             scanf(" %c %d %d", &c,&a,&b);
  88.             a--, b--;
  89.  
  90.             if (c=='M')
  91.                 update(0,0,n-1,a,b);
  92.             else {
  93.                 qVec[1]=qVec[2]=0;
  94.                 query(0,0,n-1,a,b);
  95.                 qVec[0]=(b-a+1)-qVec[1]-qVec[2];
  96.  
  97.                 printf("%d %d %d\n", qVec[0],
  98.                     qVec[1], qVec[2]);
  99.             }
  100.         }
  101.         printf("\n");
  102.     }
  103.  
  104.     return 0;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement