Advertisement
omar-alhelo

UVa-11402 (RTE)

Jun 30th, 2018
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.52 KB | None | 0 0
  1. /*
  2. * Omar
  3. * UVa-11402
  4. */
  5.  
  6. #include <stdio.h>
  7. #include <string.h>
  8.  
  9. typedef enum mode {NORM,INV,ONE,ZERO} mode;
  10.  
  11. char str[1024100], *end=str;
  12. int seg[1024010*5];
  13. mode lazy[1024010*5];
  14.  
  15. void dup(char *tmp, int n){int i,j;
  16.   for(i=0 ; i<n ; i++)
  17.     for(j=0 ; tmp[j] ; j++)
  18.       *end=tmp[j]-'0', end++;
  19. }
  20. int build(int p, int L, int R){
  21.   if(L==R) return seg[p]=str[L];
  22.  
  23.   int a = build(p<<1, L, (L+R)/2);
  24.   int b = build((p<<1)+1, (L+R)/2+1, R);
  25.  
  26.   return seg[p]=a+b;
  27. }
  28. void change_mode(int p, mode m){
  29.   if(m==NORM)       return;
  30.   else if(m==ONE)   lazy[p]=ONE;
  31.   else if(m==ZERO)  lazy[p]=ZERO;
  32.   else if(m==INV){
  33.     if(lazy[p]==NORM)       lazy[p]=INV;
  34.     else if(lazy[p]==INV)   lazy[p]=NORM;
  35.     else if(lazy[p]==ONE)   lazy[p]=ZERO;
  36.     else if(lazy[p]==ZERO)  lazy[p]=ONE;
  37.   }
  38. }
  39. void Lazy_UPD(int p, int len, mode m){
  40.   if(m==NORM) return;
  41.   else if(m==ONE) seg[p]=len;
  42.   else if(m==ZERO) seg[p]=0;
  43.   else if(m==INV) seg[p]=len-seg[p];
  44. }
  45. int RcntQ(int p, int L, int R, int i, int j){
  46.   if(i>R || j<L) return 0;
  47.  
  48.   Lazy_UPD(p, R-L+1, lazy[p]);
  49.   if(L!=R){
  50.     change_mode( p<<1   , lazy[p]);
  51.     change_mode((p<<1)+1, lazy[p]);
  52.   }
  53.   lazy[p]=NORM;
  54.  
  55.   if(L>=i && R<=j) return seg[p];
  56.  
  57.   int a = RcntQ(p<<1, L, (L+R)/2, i, j);
  58.   int b = RcntQ((p<<1)+1, (L+R)/2+1, R, i, j);
  59.  
  60.   return a+b;
  61. }
  62. int UPD(int p, int L, int R, int i, int j, mode m){
  63.   if(i>R || j<L) return 0;
  64.  
  65.   Lazy_UPD(p, R-L+1, lazy[p]); /*Resolve any pending Update.*/
  66.   int before = seg[p];  /*Store value before performing the target Update*/
  67.   if(L!=R){
  68.     change_mode( p<<1   , lazy[p]);
  69.     change_mode((p<<1)+1, lazy[p]);
  70.   }
  71.   lazy[p]=NORM;
  72.  
  73.   if(L>=i && R<=j)
  74.   {
  75.     Lazy_UPD(p, R-L+1, m);
  76.     if(L!=R){
  77.       change_mode( p<<1   , m);
  78.       change_mode((p<<1)+1, m);
  79.     }
  80.     return seg[p]-before;
  81.   }
  82.  
  83.   int a = UPD(p<<1, L, (L+R)/2, i, j, m);
  84.   int b = UPD((p<<1)+1, (L+R)/2+1, R, i, j, m);
  85.  
  86.   seg[p]+= a+b; /*Update node*/
  87.   return seg[p]-before;
  88. }
  89.  
  90. int main(){
  91.   int T,M,i,j,q;
  92.   scanf("%i", &T);
  93.   for(i=1 ;i<=T;i++)
  94.   {
  95.     memset(lazy, 0, sizeof(lazy));
  96.     end=str;
  97.     printf("Case %i:\n", i);
  98.     for(scanf("%i",&M);M--;)
  99.     {
  100.       int n;
  101.       char tmp[111];
  102.       scanf("%i%s",&n,tmp);
  103.       dup(tmp,n);
  104.     }
  105.     *end='\0';
  106.     build(1, 0, (end-str-1));
  107.     scanf("%i",&M);
  108.     for(j=1, q=1;j<=M; j++)
  109.     {
  110.       char ch[10];
  111.       int l,r;
  112.       scanf("%s%i%i", ch, &l,&r);
  113.       if(ch[0]=='S') printf("Q%i: %i\n", q++, RcntQ(1, 0, (end-str-1), l, r));
  114.       else {
  115.         mode m = ch[0]=='F'?ONE:ch[0]=='E'?ZERO:INV;
  116.         UPD(1, 0, (end-str-1), l, r, m);
  117.       }
  118.     }
  119.   }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement