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