Advertisement
royalsflush

Homem, Elefante, Rato TLE

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