社区讨论
线段树10分求调
P3870[TJOI2009] 开关参与者 2已保存回复 3
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 3 条
- 当前快照
- 1 份
- 快照标识符
- @m4b7biay
- 此快照首次捕获于
- 2024/12/05 18:56 去年
- 此快照最后确认于
- 2025/11/04 13:19 4 个月前
CPP
#include<bits/stdc++.h>
using namespace std;
struct node{
int l,r,sum,gai;
}tree[401000];
int n,m;
void build(int p,int l,int r){
tree[p].l=l,tree[p].r=r;
if(l==r){
tree[p].sum=0;
return;
}
int mid=(tree[p].l+tree[p].r)/2;
build(p*2,l,mid);
build(p*2+1,mid+1,r);
tree[p].sum=tree[p*2].sum+tree[p*2+1].sum;
}
void down(int p){
int d=tree[p].gai;
if(d%2!=0){
tree[p*2].gai+=tree[p].gai;
tree[p*2+1].gai+=tree[p].gai;
tree[p*2].sum=tree[p*2].r-tree[p*2].l+1-tree[p*2].sum;
tree[p*2+1].sum=tree[p*2+1].r-tree[p*2+1].l+1-tree[p*2+1].sum;
}
tree[p].gai-=d;
}
void update(int p,int l,int r){
if(tree[p].l>=l&&tree[p].r<=r){
tree[p].sum=tree[p].r-tree[p].l+1-tree[p].sum;
tree[p].gai^=1;
return;
}
down(p);
int mid=(tree[p].l+tree[p].r)/2;
if(l<=mid)update(p*2,l,mid);
if(r>mid)update(p*2+1,mid+1,r);
tree[p].sum=tree[p*2].sum+tree[p*2+1].sum;
}
int ask(int p,int l,int r){
if(tree[p].l>=l&&tree[p].r<=r) return tree[p].sum;
down(p);
int ans=0;
int mid=(tree[p].l+tree[p].r)/2;
if(l<=mid)ans+=ask(p*2,l,mid);
if(r>mid)ans+=ask(p*2+1,mid+1,r);
return ans;
}
int main(){
scanf("%d%d",&n,&m);
build(1,1,n);
while(m--){
int c,a,b;
scanf("%d%d%d",&c,&a,&b);
if(c==0){
update(1,a,b);
}
else{
printf("%d\n",ask(1,a,b));
}
}
return 0;
}
回复
共 3 条回复,欢迎继续交流。
正在加载回复...