Test instructions
N, M,n lamps, M-times operation
Two operations 0: This interval all the state reversed, initially all 0
1: Ask how many lights are lit in this interval.
Bare-wire section tree, weak explosion.
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define N 101000# Define INF 0x3f3f3f3fusing namespace std;struct segment_tree{int l,r,x;bool flag;} S[n<<2];int n,m;void pushup (int x) {s[x].x=s[x<<1].x+s[x<<1|1].x;} void pushdown (int x) {if (S[x].flag) {s[x].flag=0;if (S[X].L<S[X].R) {s[x<<1].flag^=1;s[x<<1|1].flag^= 1;} s[x].x=s[x].r-s[x].l+1-s[x].x;}} void build (int note,int l,int r) {s[note].l=l;s[note].r=r;if (l==r) return; int Mid=l+r>>1;build (note<<1,l, mid); build (note<<1|1,mid+1,r);} void Add (int note,int l,int R) {if (s[note].l==l&&s[note].r==r) {s[note].flag^=1;pushdown (note); return;} Pushdown (note); int mid=s[note].l+s[note].r>>1;if (R<=MID) Add (note<<1,l,r), Pushdown (note<<1| 1); else if (l>mid) Add (note<<1|1,l,r), Pushdown (note<<1), else Add (note<<1,l,mid), add (note< <1|1,MID+1,R);p ushup (note);} int query (int note,int l,int r) {pushdown (note); if (S[note]. L==L&&R==S[NOTE].R) return S[note].x;int mid=s[note].l+s[note].r>>1;if (r<=mid) return query (note <<1,L,R), else if (l>mid) return query (NOTE<<1|1,L,R), Else return query (Note<<1,l,mid) +query ( NOTE<<1|1,MID+1,R);} int main () {int i,j,k;int a,b,c;scanf ("%d%d", &n,&m), Build (1,1,n), while (m--) {scanf ("%d%d%d", &a,&b, &C), if (a==0) Add (1,b,c), Else printf ("%d\n", Query (1,b,c));} return 0;}
Copy to Google TranslateTranslation Results
"Usaco-Nov Gold" 3.Light switching (lites switch light) interval Modify segment tree