代码完全一样
#include<iostream>
#include<cmath>
#include<algorithm>
#include<stdio.h>
using namespace std;
const int N = 4 * (200000);
int n,m;
struct tree{
int sum,tag;
}a[N];
/*
>>k /2^k
<<k *2^k
*/
int data[N];
void pushdown(int now,int l,int r) {
if(a[now].tag) {
int mid = (l+r)>>1,len = r-l+1;
a[now<<1|1].tag ^= 1;
a[now<<1].tag ^= 1;
a[now<<1].sum = (len - (len>>1)) - a[now<<1].sum;
a[now<<1|1].sum = (len>>1) - a[now<<1|1].sum;
a[now].tag = 0;
}
}
void build(int l,int r,int now) {
if(l == r) {
a[now].sum = data[l];
return ;
}
int mid = (l+r)>>1;
build(l,mid,now<<1);
build(mid+1,r,now<<1|1);
a[now].sum = a[now<<1].sum + a[(now<<1)|1].sum;
}
void change(int l,int r,int x,int y,int now) {//from x to y
pushdown(now,l,r);
if(x <= l && r <= y) {
a[now].tag ^= 1;
a[now].sum = r-l+1 - a[now].sum;
return;
}
int mid = (l+r)>>1;
if(x <= mid)change(l,mid,x,y,now<<1);
if(y >= mid+1 )change(mid+1,r,x,y,now<<1|1);
a[now].sum = a[now<<1].sum + a[now<<1|1].sum;
}
int query(int l,int r,int x,int y,int now) {
pushdown(now,l,r);
if(x <= l && r <= y)return a[now].sum;
int mid = (l+r)>>1;
int ans = 0;
if(x <= mid)ans += query(l,mid,x,y,now<<1);
if(y >= mid+1)ans += query(mid+1,r,x,y,now<<1|1);
return ans;
}
int main() {
scanf("%d%d",&n,&m);
char t;
for(int i = 1; i <= n; i++) {
cin >> t;
data[i] = int(t-'0') ;
}
build(1,n,1);
int op,l,r;
for(int i = 1; i <= m; i++) {
scanf("%d%d%d",&op,&l,&r);
if(op == 0) change(1,n,l,r,1);
else cout << query(1,n,l,r,1) << "\n";
}
return 0;
}
玄学啊