Luo Gu p3870 switch segment tree template

This two-day learning for a long time and then do a water problem

I use a template, not even any optimization

AC on the complexity and very explosive 10 points over 1500 ms

The problem is to lazy [] into a number of changes under the record, every change when mod 2, since the turn, and then return the same opinions

Modify become

    sum[root] = r - l + 1 - sum[root];

The other almost no difference between

#include<bits/stdc++.h>

using namespace std;

inline int read(){int s=0,w=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();return s*w;}
inline void write(int x) {if(x<0) putchar('-'),x=-x; if(x>9) write(x/10); putchar(x%10+'0'); }

int sum[400010],lazy[400010];

void add(int root,int l,int r,int k){
    if(k == 0)//如果不修改的话直接退出就好了
        return ;
    lazy[root] += k;//记录次数
    sum[root] = r - l + 1 - sum[root];//修改
    lazy[root] %= 2;//2以外的毫无意义
    return ;
}

void putdown(int root,int l,int r){
    if(lazy[root] == 0)//没有标记不下发
        return ;
    int mod = lazy[root] % 2; int mid = (l + r) >> 1;
    add(root * 2,l,mid,mod);//左右孩子下发
    add(root * 2 + 1,mid + 1,r,mod);
    lazy[root] = 0;//标记清空
    return ;
}

void change(int root,int l,int r,int x,int y){
    if(l > y || r < x)//不在范围内退出(好像没必要)
        return ;
    if(l >= x && r <= y){//完全在范围内就标记然后下发
        add(root,l,r,1);
        return ;
    }
    int mid = (l + r) >> 1;
    putdown(root,l,r);//标记不到正确位置就继续发
    int left = root << 1;
    int right = left + 1;
    if(x <= mid) change(left,l,mid,x,y);
    if(mid < y) change(right,mid + 1,r,x,y);
    sum[root] = sum[left] + sum[right];//更新父节点
}

int find(int root,int l,int r,int x,int y){
    if(l >= x && r <= y){//完全包含加上返回
        return sum[root];
    }
    int mid = (l + r) >> 1,ans = 0;
    int left = root << 1;
    int right = left + 1;
    putdown(root,l,r);//传到需要的地方
    if(x <= mid) ans += find(left,l,mid,x,y);
    if(mid < y) ans += find(right,mid + 1,r,x,y);
    return ans;
}

int main(){
    int n,m;
    cin >> n >> m;
    for(int i = 1; i <= m;++i){
        int x,y,z;
        scanf("%d%d%d",&x,&y,&z);
        if(x == 0){
            change(1,1,n,y,z);
        }
        else{
            write(find(1,1,n,y,z));
            cout<<endl;
        }
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/lztzs/p/11112136.html