SuitNdtie

SMMR-174: Liverpool ManU

May 28th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.60 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. int const N = 1024010;
  4. int arr[N];
  5.  
  6. int n = 0;
  7.  
  8. //to do segment tree with lazy propagation function
  9. int seg[N*4]; // store sum of subtree
  10. char lazy[N*4]; //lazy propagation symbol ( '\0' = off  , 'F' = Filling 1 , 'E' = Emptying , 'I' = Inversing )
  11.  
  12. void build(int p = 1 , int b = 0 , int e = n - 1){
  13. //  printf("Test build idx: %d , len :(%d,%d)\n",p,b,e);
  14.     if(b == e){
  15.         seg[p] = arr[b];
  16.         return;
  17.     }
  18.     int m = (b+e)/2;
  19.     build(p*2,b,m);
  20.     build(p*2+1,m+1,e);
  21.     seg[p] = seg[p*2] + seg[p*2+1];
  22. }
  23.  
  24. void change(char &curstats , char newvalue){
  25.     if(newvalue == 'F' || newvalue == 'E'){
  26.         curstats = newvalue;
  27.     }
  28.     else if(newvalue == 'I'){
  29.         if(curstats == 'F'){
  30.             curstats = 'E';
  31.         }
  32.         else if(curstats == 'E'){
  33.             curstats = 'F';
  34.         }
  35.         else if(curstats == 'I'){
  36.             curstats = '\0';
  37.         }
  38.         else if(curstats == '\0'){
  39.             curstats = 'I';
  40.         }
  41.         else{
  42.             printf("Error %c\n",curstats);
  43.             return;
  44.         }
  45.     }
  46. }
  47. void push(int p,int b,int e){
  48.     if(lazy[p] == '\0')return;
  49.    
  50.     if(lazy[p] == 'F'){
  51.         seg[p] = (e-b+1);
  52.     }
  53.     else if(lazy[p] == 'E'){
  54.         seg[p] = 0;
  55.     }
  56.     else if(lazy[p] == 'I'){
  57.         seg[p] = (e-b+1) - seg[p];
  58.     }
  59.    
  60.     if(b != e){
  61.         change(lazy[p*2] , lazy[p]);
  62.         change(lazy[p*2+1] , lazy[p]);
  63.     }
  64.     lazy[p] = '\0';
  65. }
  66.  
  67. void update(int l,int r,char k,int p = 1 , int b = 0 , int e = n - 1){
  68.     push(p,b,e);
  69.     if(b > r || e < l)return;
  70.     if(l <= b && e <= r){
  71.         change(lazy[p],k);
  72.         push(p,b,e);
  73.         return;
  74.     }
  75.     int m = (b+e)/2;
  76.     update(l,r,k,p*2,b,m);
  77.     update(l,r,k,p*2+1,m+1,e);
  78.     seg[p] = seg[p*2] + seg[p*2+1];
  79. }
  80.  
  81. int query(int l,int r,int p = 1 ,int b = 0 ,int e = n - 1){
  82.     push(p,b,e);
  83.     if(b > r || e < l)return 0;
  84.     if(l <= b && e <= r)return seg[p];
  85.     int m = (b+e)/2;
  86.     return query(l,r,p*2,b,m) + query(l,r,p*2+1,m+1,e);
  87. }
  88.  
  89. int main(){
  90.     for(int i = 0 ; i < N ; i ++)lazy[i] = '\0';
  91.     int m;
  92.     scanf("%d",&m);
  93.     for(int i = 0 ; i < m ; i ++){
  94.         int time;
  95.         char str[N];
  96.         scanf("%d",&time);
  97.         scanf(" %s",str);
  98.         for(int t = 0 ; t < time ; t ++){
  99.             for(int j = 0 ; str[j] != '\0' ; j ++){
  100.                 arr[n++] = str[j] - '0';
  101.             }
  102.         }
  103.     }
  104. /*  for(int i = 0 ; i < n ; i ++){ //test arr
  105.         printf("%d",arr[i]);
  106.     }
  107.     */
  108.     build();
  109.     int oc;
  110.     scanf("%d",&oc);
  111.    
  112.     for(int t = 0 ; t < oc ; t ++){
  113.         char type;
  114.         int L,R;
  115.         scanf(" %c %d %d",&type,&L,&R);
  116.         if(type == 'F'){
  117.             update(L,R,type);
  118.         }
  119.         else if(type == 'E'){
  120.             update(L,R,type);
  121.         }
  122.         else if(type == 'I'){
  123.             update(L,R,type);
  124.         }
  125.         else if(type == 'S'){
  126.             printf("%d\n",query(L,R));
  127.         //  update(L,R,type);
  128.         }
  129.         else{
  130.             printf("error %c\n",type);
  131.             return 0;
  132.         }
  133.     }
  134.     return 0;
  135.    
  136. }
Add Comment
Please, Sign In to add comment