Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.08 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define FRU freopen("out.txt","w",stdout)
  3. #define FRO freopen("in.txt","r",stdin)
  4. #define pb push_back
  5. #define mp make_pair
  6. #define ff first
  7. #define ss second
  8. #define mem(ara,n) memset(ara,n,sizeof ara)
  9. #define loop(i,j,n) for(i=j;i<n;i++)
  10. #define rloop(i,j,n) for(i=n;i>=j;i--)
  11. #define INF 2147483647
  12. #define ll long long
  13. #define pii pair<int,int>
  14. #define eps 1e-9
  15. #define mii map<int,int>
  16. #define vi vector<int>
  17. #define all(n) n.begin(),n.end()
  18. #define inf INF
  19.  
  20. //const int row[]={-1, -1, -1, 0, 0, 1, 1, 1}; // Kings Move
  21. //const int col[]={-1, 0, 1, -1, 1, -1, 0, 1}; // Kings Move
  22. //const int row[]={-2, -2, -1, -1, 1, 1, 2, 2}; // Knights Move
  23. //const int col[]={-1, 1, -2, 2, -2, 2, -1, 1}; // Knights Move
  24. //const int row[]={-1,0,0,1,0};
  25. //const int col[]={0,-1,1,0,0};
  26. int gcd(int a,int b)
  27. {
  28. return b==0?a:gcd(b,a%b);
  29. }
  30. int lcm(int a,int b)
  31. {
  32. return ((a*b)/gcd(a,b));
  33. }
  34.  
  35. /*bool bitcheck(int n,int pos)
  36. {
  37. return (bool)(n & (1<<pos));
  38. }
  39.  
  40. int biton(int n,int pos)
  41. {
  42. return n=n or (1<<pos);
  43. }
  44. int bitoff(int n,int pos)
  45. {
  46. return n=n & ~(1<<pos);
  47. }*/
  48.  
  49. using namespace std;
  50. #define lim 1024000
  51. #define left ps<<1
  52. #define right (ps<<1)+1
  53. #define mid (L+R)>>1
  54.  
  55. int st[3*lim],lazy[3*lim];
  56. string s;
  57.  
  58. ///st keeps the number of Buccaneer pirates. lazy keep track of current command.
  59.  
  60. void build(int ps,int L,int R)
  61. {
  62. lazy[ps]=0;
  63. if(L==R)
  64. {
  65. if((s[L]-'0')==1)st[ps]=1;
  66. else st[ps]=0;
  67. return ;
  68. }
  69. build(left,L,mid);
  70. build(right,(mid)+1,R);
  71. st[ps]=st[left]+st[right];
  72. }
  73.  
  74. void up(int ps,int L,int R)/// this function updates lazy and propagate to leaf.
  75. {
  76. if(lazy[ps]!=0)
  77. {
  78. //printf("%d %d %d ",L,R,lazy[ps]);
  79. if(L!=R)
  80. {
  81. if(lazy[ps]==3)
  82. {
  83. if(lazy[left]==1){lazy[left]=2; }
  84. else if(lazy[left]==2)lazy[left]=1;
  85. else lazy[left]=3-lazy[left];
  86. if(lazy[right]==1)lazy[right]=2;
  87. else if(lazy[right]==2)lazy[right]=1;
  88. else lazy[right]=3-lazy[right];
  89. }
  90. else lazy[left]=lazy[right]=lazy[ps];
  91. }
  92. if(lazy[ps]==1)st[ps]=(R-L+1);
  93. else if(lazy[ps]==2)st[ps]=0;
  94. else st[ps]=(R-L+1)-st[ps];
  95. lazy[ps]=0;
  96. //printf("%d\n",st[ps]);
  97. }
  98. }
  99.  
  100. void update(int ps,int L,int R,int i,int j,int v)
  101. {
  102. if(L>j|| R<i)return;
  103. up(ps,L,R);
  104. if(L>=i&& R<=j)
  105. {
  106.  
  107. lazy[ps]=v;
  108. }
  109. else
  110. {
  111. update(left,L,mid,i,j,v);
  112. update(right,(mid)+1,R,i,j,v);
  113.  
  114. }
  115. }
  116.  
  117.  
  118.  
  119. int query(int ps,int L,int R,int i,int j)
  120. {
  121. if(L>j|| R<i)return 0;
  122. up(ps,L,R);
  123. if(L>=i&& R<=j)
  124. {
  125.  
  126. return st[ps];
  127. }
  128. int ls=query(left,L,mid,i,j);
  129. int rs=query(right,(mid)+1,R,i,j);
  130. return ls+rs;
  131. }
  132.  
  133. void check(int ps,int L,int R,int i,int j)/// this function is just for debuging
  134. {
  135. if(L>j|| R<i)return;
  136. up(ps,L,R);
  137. if(L==R)printf("%d",st[ps]);
  138. else
  139. {
  140. check(left,L,mid,i,j);
  141. check(right,(mid)+1,R,i,j);
  142. }
  143. }
  144.  
  145. int main()
  146. {
  147. FRO;
  148. //FRU;
  149. //std::ios_base::sync_with_stdio(false);
  150. int a,b,c,i,j,k,tc,t;
  151. int n,m,cnt=0,len;
  152. string s1;
  153. scanf("%d",&tc);
  154. for(t=1; t<=tc; t++)
  155. {
  156. s="";
  157. scanf("%d",&n);
  158. for(i=0; i<n; i++)
  159. {
  160. scanf("%d",&m);
  161. cin>>s1;
  162. for(j=0; j<m; j++)s+=s1;
  163. }
  164. len=s.length();
  165. build(1,0,len-1);
  166. printf("Case %d:\n",t);
  167. scanf("%d",&n);
  168. char s[10];
  169. j=1;
  170. for(i=0; i<n; i++)
  171. {
  172. scanf("%s%d%d",s,&a,&b);
  173. if(s[0]=='F')update(1,0,len-1,a,b,1);
  174. else if(s[0]=='E')update(1,0,len-1,a,b,2);
  175. else if(s[0]=='I')update(1,0,len-1,a,b,3);
  176. else
  177. {
  178. check(1,0,len-1,0,len-1);printf("\n");
  179. printf("Q%d: %d\n",j++,query(1,0,len-1,a,b));
  180.  
  181. }
  182. }
  183.  
  184. }
  185. return 0;
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement