P2846 [USACO08NOV]光開關Light Switching
題目描述
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
說明
原題時間限制為2s,內存限制為16M
Solution:
本題比較水(是真的很水)。
和以前做過的某道好像叫做開關的題目一模一樣。
線段樹維護區間和,區間修改時等價於區間取反,$sum$變為長度減去原來的和,打個異或懶惰標記(兩次操作等於沒有操作)。
練手瞎搞一棄就好了。
代碼:
1 #include<bits/stdc++.h> 2 #define lson l,m,rt<<1 3 #define rson m+1,r,rt<<1|1 4 #define il inline 5 using namespace std; 6 7 const int N=100005; 8 int n,m,sum[N*3],lazy[N*3]; 9 10 il int gi(){ 11 int a=0;char x=getchar(); 12 while(x<‘0‘||x>‘9‘)x=getchar(); 13 while(x>=‘0‘&&x<=‘9‘)a=(a<<3)+(a<<1)+x-48,x=getchar(); 14 return a; 15 } 16 17 il void pushup(int rt){sum[rt]=sum[rt<<1]+sum[rt<<1|1];} 18 19 il void pushdown(int rt,int len){ 20 if(lazy[rt]){ 21 lazy[rt<<1]^=1;lazy[rt<<1|1]^=1; 22 sum[rt<<1]=(len-(len>>1))-sum[rt<<1]; 23 sum[rt<<1|1]=(len>>1)-sum[rt<<1|1]; 24 lazy[rt]=0; 25 } 26 } 27 28 il void update(int L,int R,int l,int r,int rt){ 29 if(L<=l&&R>=r){sum[rt]=(r-l+1)-sum[rt];lazy[rt]^=1;return;} 30 pushdown(rt,r-l+1); 31 int m=l+r>>1; 32 if(L<=m)update(L,R,lson); 33 if(R>m)update(L,R,rson); 34 pushup(rt); 35 } 36 37 il int query(int L,int R,int l,int r,int rt){ 38 if(L<=l&&R>=r)return sum[rt]; 39 pushdown(rt,r-l+1); 40 int m=l+r>>1,ret=0; 41 if(L<=m)ret+=query(L,R,lson); 42 if(R>m)ret+=query(L,R,rson); 43 return ret; 44 } 45 46 int main(){ 47 n=gi(),m=gi(); 48 int f,x,y; 49 while(m--){ 50 f=gi(),x=gi(),y=gi(); 51 if(!f)update(x,y,1,n,1); 52 else printf("%d\n",query(x,y,1,n,1)); 53 } 54 return 0; 55 }
P2846 [USACO08NOV]光開關Light Switching