[USACO08NOV]光开关Light Switching&&[TJOI2009]开关
题目描述
Farmer John tries to keep the cows sharp by letting them play with intellectual toys. One of the larger toys is the lights in the barn. Each of the N (2 <= N <= 100,000) cow stalls conveniently numbered 1..N has a colorful light above it.
At the beginning of the evening, all the lights are off. The cows control the lights with a set of N pushbutton switches that toggle the lights; pushing switch i changes the state of light i from off to on or from on to off.
The cows read and execute a list of M (1 <= M <= 100,000) operations expressed as one of two integers (0 <= operation <= 1).
The first kind of operation (denoted by a 0 command) includes two subsequent integers S_i and E_i (1 <= S_i <= E_i <= N) that indicate a starting switch and ending switch. They execute the operation by pushing each pushbutton from S_i through E_i inclusive exactly once.
The second kind of operation (denoted by a 1 command) asks the cows to count how many lights are on in the range given by two integers S_i and E_i (1 <= S_i <= E_i <= N) which specify the inclusive range in which the cows should count the number of lights that are on.
Help FJ ensure the cows are getting the correct answer by processing the list and producing the proper counts.
灯是由高科技——外星人鼠标操控的。你只要左击两个灯所连的鼠标,这两个灯,以及之间的灯都会由暗变亮,或由亮变暗。右击两个灯所连的鼠标,你就可以知道这两个灯,以及之间的灯有多少灯是亮的。起初所有灯都是暗的,你的任务是在LZ之前算出灯的亮灭。
输入输出格式
输入格式:
第1 行: 用空格隔开的两个整数N 和M,n 是灯数
第2..M+1 行: 每行表示一个操作, 有三个用空格分开的整数: 指令号, S_i 和E_i
第1 种指令(用0 表示)包含两个数字S_i 和E_i (1 <= S_i <= E_i <= N), 它们表示起始开关和终止开关. 表示左击
第2 种指令(用1 表示)同样包含两个数字S_i 和E_i (1 <= S_i <= E_i <= N), 不过这种指令是询问从S_i 到E_i 之间的灯有多少是亮着的.
输出格式:
输入输出样例
输入样例#1:
4 5 0 1 2 0 2 4 1 2 3 0 2 4 1 1 4
输出样例#1:
1 2
solution
显然是一道线段树啦,总感觉和洛谷P2574 XOR的艺术那么想,开灯和关灯就相当于xor1那这道题和线段树裸题的区别无非就是lazytag的地方,也没什么其他的难点了
初一时写的丑陋的代码
#include<bits/stdc++.h> using namespace std; const int MAXN = 2* 100000; struct node { int l; int r; long long sum; int len; inline int mid() { return (l+r)>>1; } };node tree[MAXN*4]; int add[MAXN*4]; inline int read() { char ch; int fl=1; int x=0; do{ ch= getchar(); if(ch=='-') fl=-1; }while(ch<'0'||ch>'9'); do{ x=(x<<3)+(x<<1)+ch-'0'; ch=getchar(); }while(ch>='0'&&ch<='9'); return x*fl; } int n,m; int a[MAXN]; int op,x,y; int ans; inline void pushdown(int now) { if(!add[now]) return; int lc=now<<1,rc=now<<1|1; add[lc]^=1; add[rc]^=1; tree[lc].sum=(tree[lc].len)-tree[lc].sum; tree[rc].sum=(tree[rc].len)-tree[rc].sum; add[now]=0; return; } inline void make(int l,int r,int now) { tree[now].l=l; tree[now].r=r; tree[now].len=tree[now].r-tree[now].l+1; if(l==r) { tree[now].sum=a[l]; return; } else { int mid=tree[now].mid(); make(l,mid,now<<1); make(mid+1,r,now<<1|1); tree[now].sum=tree[now<<1].sum+tree[now<<1|1].sum; return; } } inline void xg(int now,int y,int z) { int lc=now<<1,rc=now<<1|1; int l=tree[now].l,r=tree[now].r; if(l>z||y>r) return; if(y<=l&&r<=z) { tree[now].sum=tree[now].len-tree[now].sum; add[now]^=1; return; } else { if(add[now]) pushdown(now); xg(lc,y,z); xg(rc,y,z); tree[now].sum=tree[lc].sum+tree[rc].sum; } } inline void question(int now,int x,int y) { int lc=now<<1,rc=now<<1|1; int l=tree[now].l; int r=tree[now].r; if(l>y||x>r) return; if(x<=l&&r<=y) { ans+=tree[now].sum; return; } else { if(add[now]) pushdown(now); question(lc,x,y); question(rc,x,y); return; } } int main() { n=read(); m=read(); for(int i=1;i<=n;i++) a[i]=0; make(1,n,1); for(int i=1;i<=m;i++) { op=read(); x=read(); y=read(); ans=0; if(op==0) xg(1,x,y); else question(1,x,y),cout<<ans<<endl; } }