Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- int const N = 1024010;
- int arr[N];
- int n = 0;
- //to do segment tree with lazy propagation function
- int seg[N*4]; // store sum of subtree
- char lazy[N*4]; //lazy propagation symbol ( '\0' = off , 'F' = Filling 1 , 'E' = Emptying , 'I' = Inversing )
- void build(int p = 1 , int b = 0 , int e = n - 1){
- // printf("Test build idx: %d , len :(%d,%d)\n",p,b,e);
- if(b == e){
- seg[p] = arr[b];
- return;
- }
- int m = (b+e)/2;
- build(p*2,b,m);
- build(p*2+1,m+1,e);
- seg[p] = seg[p*2] + seg[p*2+1];
- }
- void change(char &curstats , char newvalue){
- if(newvalue == 'F' || newvalue == 'E'){
- curstats = newvalue;
- }
- else if(newvalue == 'I'){
- if(curstats == 'F'){
- curstats = 'E';
- }
- else if(curstats == 'E'){
- curstats = 'F';
- }
- else if(curstats == 'I'){
- curstats = '\0';
- }
- else if(curstats == '\0'){
- curstats = 'I';
- }
- else{
- printf("Error %c\n",curstats);
- return;
- }
- }
- }
- void push(int p,int b,int e){
- if(lazy[p] == '\0')return;
- if(lazy[p] == 'F'){
- seg[p] = (e-b+1);
- }
- else if(lazy[p] == 'E'){
- seg[p] = 0;
- }
- else if(lazy[p] == 'I'){
- seg[p] = (e-b+1) - seg[p];
- }
- if(b != e){
- change(lazy[p*2] , lazy[p]);
- change(lazy[p*2+1] , lazy[p]);
- }
- lazy[p] = '\0';
- }
- void update(int l,int r,char k,int p = 1 , int b = 0 , int e = n - 1){
- push(p,b,e);
- if(b > r || e < l)return;
- if(l <= b && e <= r){
- change(lazy[p],k);
- push(p,b,e);
- return;
- }
- int m = (b+e)/2;
- update(l,r,k,p*2,b,m);
- update(l,r,k,p*2+1,m+1,e);
- seg[p] = seg[p*2] + seg[p*2+1];
- }
- int query(int l,int r,int p = 1 ,int b = 0 ,int e = n - 1){
- push(p,b,e);
- if(b > r || e < l)return 0;
- if(l <= b && e <= r)return seg[p];
- int m = (b+e)/2;
- return query(l,r,p*2,b,m) + query(l,r,p*2+1,m+1,e);
- }
- int main(){
- for(int i = 0 ; i < N ; i ++)lazy[i] = '\0';
- int m;
- scanf("%d",&m);
- for(int i = 0 ; i < m ; i ++){
- int time;
- char str[N];
- scanf("%d",&time);
- scanf(" %s",str);
- for(int t = 0 ; t < time ; t ++){
- for(int j = 0 ; str[j] != '\0' ; j ++){
- arr[n++] = str[j] - '0';
- }
- }
- }
- /* for(int i = 0 ; i < n ; i ++){ //test arr
- printf("%d",arr[i]);
- }
- */
- build();
- int oc;
- scanf("%d",&oc);
- for(int t = 0 ; t < oc ; t ++){
- char type;
- int L,R;
- scanf(" %c %d %d",&type,&L,&R);
- if(type == 'F'){
- update(L,R,type);
- }
- else if(type == 'E'){
- update(L,R,type);
- }
- else if(type == 'I'){
- update(L,R,type);
- }
- else if(type == 'S'){
- printf("%d\n",query(L,R));
- // update(L,R,type);
- }
- else{
- printf("error %c\n",type);
- return 0;
- }
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment