Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- #define FRU freopen("out.txt","w",stdout)
- #define FRO freopen("in.txt","r",stdin)
- #define pb push_back
- #define mp make_pair
- #define ff first
- #define ss second
- #define mem(ara,n) memset(ara,n,sizeof ara)
- #define loop(i,j,n) for(i=j;i<n;i++)
- #define rloop(i,j,n) for(i=n;i>=j;i--)
- #define INF 2147483647
- #define ll long long
- #define pii pair<int,int>
- #define eps 1e-9
- #define mii map<int,int>
- #define vi vector<int>
- #define all(n) n.begin(),n.end()
- #define inf INF
- //const int row[]={-1, -1, -1, 0, 0, 1, 1, 1}; // Kings Move
- //const int col[]={-1, 0, 1, -1, 1, -1, 0, 1}; // Kings Move
- //const int row[]={-2, -2, -1, -1, 1, 1, 2, 2}; // Knights Move
- //const int col[]={-1, 1, -2, 2, -2, 2, -1, 1}; // Knights Move
- //const int row[]={-1,0,0,1,0};
- //const int col[]={0,-1,1,0,0};
- int gcd(int a,int b)
- {
- return b==0?a:gcd(b,a%b);
- }
- int lcm(int a,int b)
- {
- return ((a*b)/gcd(a,b));
- }
- /*bool bitcheck(int n,int pos)
- {
- return (bool)(n & (1<<pos));
- }
- int biton(int n,int pos)
- {
- return n=n or (1<<pos);
- }
- int bitoff(int n,int pos)
- {
- return n=n & ~(1<<pos);
- }*/
- using namespace std;
- #define lim 1024000
- #define left ps<<1
- #define right (ps<<1)+1
- #define mid (L+R)>>1
- int st[3*lim],lazy[3*lim];
- string s;
- ///st keeps the number of Buccaneer pirates. lazy keep track of current command.
- void build(int ps,int L,int R)
- {
- lazy[ps]=0;
- if(L==R)
- {
- if((s[L]-'0')==1)st[ps]=1;
- else st[ps]=0;
- return ;
- }
- build(left,L,mid);
- build(right,(mid)+1,R);
- st[ps]=st[left]+st[right];
- }
- void up(int ps,int L,int R)/// this function updates lazy and propagate to leaf.
- {
- if(lazy[ps]!=0)
- {
- //printf("%d %d %d ",L,R,lazy[ps]);
- if(L!=R)
- {
- if(lazy[ps]==3)
- {
- if(lazy[left]==1){lazy[left]=2; }
- else if(lazy[left]==2)lazy[left]=1;
- else lazy[left]=3-lazy[left];
- if(lazy[right]==1)lazy[right]=2;
- else if(lazy[right]==2)lazy[right]=1;
- else lazy[right]=3-lazy[right];
- }
- else lazy[left]=lazy[right]=lazy[ps];
- }
- if(lazy[ps]==1)st[ps]=(R-L+1);
- else if(lazy[ps]==2)st[ps]=0;
- else st[ps]=(R-L+1)-st[ps];
- lazy[ps]=0;
- //printf("%d\n",st[ps]);
- }
- }
- void update(int ps,int L,int R,int i,int j,int v)
- {
- if(L>j|| R<i)return;
- up(ps,L,R);
- if(L>=i&& R<=j)
- {
- lazy[ps]=v;
- }
- else
- {
- update(left,L,mid,i,j,v);
- update(right,(mid)+1,R,i,j,v);
- }
- }
- int query(int ps,int L,int R,int i,int j)
- {
- if(L>j|| R<i)return 0;
- up(ps,L,R);
- if(L>=i&& R<=j)
- {
- return st[ps];
- }
- int ls=query(left,L,mid,i,j);
- int rs=query(right,(mid)+1,R,i,j);
- return ls+rs;
- }
- void check(int ps,int L,int R,int i,int j)/// this function is just for debuging
- {
- if(L>j|| R<i)return;
- up(ps,L,R);
- if(L==R)printf("%d",st[ps]);
- else
- {
- check(left,L,mid,i,j);
- check(right,(mid)+1,R,i,j);
- }
- }
- int main()
- {
- FRO;
- //FRU;
- //std::ios_base::sync_with_stdio(false);
- int a,b,c,i,j,k,tc,t;
- int n,m,cnt=0,len;
- string s1;
- scanf("%d",&tc);
- for(t=1; t<=tc; t++)
- {
- s="";
- scanf("%d",&n);
- for(i=0; i<n; i++)
- {
- scanf("%d",&m);
- cin>>s1;
- for(j=0; j<m; j++)s+=s1;
- }
- len=s.length();
- build(1,0,len-1);
- printf("Case %d:\n",t);
- scanf("%d",&n);
- char s[10];
- j=1;
- for(i=0; i<n; i++)
- {
- scanf("%s%d%d",s,&a,&b);
- if(s[0]=='F')update(1,0,len-1,a,b,1);
- else if(s[0]=='E')update(1,0,len-1,a,b,2);
- else if(s[0]=='I')update(1,0,len-1,a,b,3);
- else
- {
- check(1,0,len-1,0,len-1);printf("\n");
- printf("Q%d: %d\n",j++,query(1,0,len-1,a,b));
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement