社区讨论

求调

P3870[TJOI2009] 开关参与者 1已保存回复 0

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
0 条
当前快照
1 份
快照标识符
@mk82os0i
此快照首次捕获于
2026/01/10 16:58
上个月
此快照最后确认于
2026/01/13 15:55
上个月
查看原帖
样例过了,只有10分,求调
CPP
#include<cstdio>
using namespace std;

struct Tree{
    #define maxn 100010
    #define pushup(u) w[u]=w[u*2]+w[u*2+1];
    int w[maxn*4],tag[maxn*4];
    void maketag(int u,int len){
        w[u]=len-w[u];
        tag[u]^=1;
    }
    void pushdown(int u,int L,int R){
        int M=(L+R)/2;
        if(tag[u]){
            maketag(u*2,M-L+1);
            maketag(u*2+1,R-M);
        }
        tag[u]=0;
    }
    #define In(l,r,L,R) (l<=L && R<=r)
    #define Out(l,r,L,R) (L>r || R<l)
    void update(int u,int l,int r,int L,int R){
        if(In(l,r,L,R))maketag(u,R-L+1);
        else if(!Out(l,r,L,R)){
            int m=(l+r)/2;
            pushdown(u,l,r);
            update(u*2,l,m,L,R);
            update(u*2+1,m+1,r,L,R);
            pushup(u);
        }
    }
    int query(int u,int l,int r,int L,int R){
        if(In(l,r,L,R))return w[u];
        else if(!Out(l,r,L,R)){
            int m=(l+r)/2;
            pushdown(u,l,r);
            return query(u*2,l,m,L,R)+query(u*2+1,m+1,r,L,R);
        }
        else return 0;
    }
    void build(int u,int l,int r){
        w[u]=0;tag[u]=0;
        if(l==r)return;
        int m=(l+r)/2;
        build(u*2,l,m);build(u*2+1,m+1,r);
    }
}T;

int main(){
    int n,m;
    scanf("%d%d",&n,&m);
    T.build(1,1,n);
    for(int o=1;o<=m;o++){
        int c,a,b;
        scanf("%d%d%d",&c,&a,&b);
        if(c==0)T.update(1,1,n,a,b);
        else printf("%d\n",T.query(1,1,n,a,b));
    }
    return 0;
}

回复

0 条回复,欢迎继续交流。

正在加载回复...