1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
| #include<bits/stdc++.h>
#define pl p<<1 #define pr p<<1|1 #define ll long long
using namespace std;
const int N=1e5+5; ll n,m,a[N],w[N<<2],lazy[N<<2];
void pushup(ll p){ w[p]=w[pl]+w[pr]; }
void pushdown(ll p,ll l,ll r){ if(lazy[p]==0)return; lazy[pl]=!lazy[pl]; lazy[pr]=!lazy[pr]; ll mid=(l+r)>>1; w[pl]=(mid-l+1)-w[pl]; w[pr]=(r-mid)-w[pr]; lazy[p]=0; }
void updata(ll p,ll l,ll r,ll L,ll R){ if(r<L||l>R) return; if(l>=L&&r<=R){ lazy[p]=!lazy[p]; w[p]=r-l+1-w[p]; return; } pushdown(p,l,r); ll mid=l+r>>1; updata(pl,l,mid,L,R); updata(pr,mid+1,r,L,R); pushup(p); } ll query(ll p,ll l,ll r,ll L,ll R){ if(r<L||l>R)return 0; if(l>=L&&r<=R)return w[p]; ll mid=l+r>>1; pushdown(p,l,r); return query(pl,l,mid,L,R)+query(pr,mid+1,r,L,R); }
int main(){ ll op,k,x,y; cin>>n>>m;
for(int i=1;i<=m;i++){ cin>>op>>x>>y; if(op==0){ updata(1,1,n,x,y); } if(op==1){ cout<<query(1,1,n,x,y)<<endl; } } return 0; }
|