洛谷P2826 [USACO08NOV]光开关Light Switching [2017年6月计划 线段树02]
P2826 [USACO08NOV]光開關(guān)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.
燈是由高科技——外星人鼠標(biāo)操控的。你只要左擊兩個燈所連的鼠標(biāo),
這兩個燈,以及之間的燈都會由暗變亮,或由亮變暗。右擊兩個燈所連的鼠
標(biāo),你就可以知道這兩個燈,以及之間的燈有多少燈是亮的。你的任務(wù)是在LZ
之前算出燈的亮滅。
輸入輸出格式
輸入格式:第1 行: 用空格隔開的兩個整數(shù)N 和M,n 是燈數(shù)
第2..M+1 行: 每行表示一個操作, 有三個用空格分開的整數(shù): 指令號, S_i 和E_i
第1 種指令(用0 表示)包含兩個數(shù)字S_i 和E_i (1 <= S_i <= E_i <= N), 它們表示起
始開關(guān)和終止開關(guān). 表示左擊
第2 種指令(用1 表示)同樣包含兩個數(shù)字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,內(nèi)存限制為16M
?
至今才知道原來修改操作也需要下放標(biāo)記,這樣省去之前很多很麻煩的判斷寫法了T.T
利用異或的特性減少了很多if判斷和修改。用1和0不斷異或即可。
線段樹記錄區(qū)間1的數(shù)量,lazy標(biāo)記記錄當(dāng)前字?jǐn)?shù)是否應(yīng)該反轉(zhuǎn)
0^1 = 1 1^1 = 0 。。。。
1 #include <bits/stdc++.h> 2 inline void read(int &x) 3 { 4 x = 0;char ch = getchar();char c = ch; 5 while(ch > '9' || ch < '0')c = ch, ch = getchar(); 6 while(ch <= '9' && ch >= '0')x = x * 10 + ch - '0',ch = getchar(); 7 if(c == '-')x = -x; 8 } 9 const int MAXN = 100000 + 10; 10 const int MAXM = 1000000 + 10; 11 inline void swap(int &a,int &b) 12 { 13 int tmp = a; 14 a = b; 15 b = tmp; 16 } 17 18 int stdata[MAXN << 2];int lazy[MAXN << 2]; 19 //線段樹存數(shù)字1的個數(shù),lazy:0則不動, 1則反置 20 21 int n,m; 22 23 void putdown(int o, int l, int r) 24 { 25 int mid = (l + r) >> 1; 26 lazy[o << 1] ^= lazy[o]; 27 lazy[o << 1 | 1] ^= lazy[o]; 28 lazy[o] = 0; 29 stdata[o << 1] = (mid - l + 1) - stdata[o << 1]; 30 stdata[o << 1 | 1] = (r - mid) - stdata[o << 1 | 1]; 31 } 32 33 void modify(int ll, int rr, int o = 1, int l = 1, int r = n) 34 { 35 if(ll <= l && rr >= r) 36 { 37 stdata[o] = (r - l + 1) - stdata[o]; 38 lazy[o] ^= 1; 39 return; 40 } 41 int mid = (l + r) >> 1; 42 if(lazy[o])putdown(o, l, r); 43 if(mid >= ll)modify(ll, rr, o << 1, l, mid); 44 if(mid < rr) modify(ll, rr, o << 1 | 1, mid + 1, r); 45 stdata[o] = stdata[o << 1] + stdata[o << 1 | 1]; 46 } 47 48 int query(int ll, int rr, int o = 1, int l = 1, int r = n) 49 { 50 if(ll <= l && rr >= r) 51 { 52 return stdata[o]; 53 } 54 int ans = 0; 55 int mid = (l + r) >> 1; 56 if(lazy[o]) putdown(o, l, r); 57 if(mid >= ll) ans += query(ll, rr, o << 1, l, mid); 58 if(mid < rr)ans += query(ll, rr, o << 1 | 1, mid + 1, r); 59 return ans; 60 } 61 62 int main() 63 { 64 read(n);read(m); 65 for(int i = 1;i <= m;i ++) 66 { 67 int tmp1,tmp2,tmp3; 68 read(tmp1);read(tmp2);read(tmp3); 69 if(tmp1 == 0) 70 { 71 modify(tmp2, tmp3); 72 } 73 else 74 { 75 printf("%d\n", query(tmp2, tmp3)); 76 } 77 } 78 return 0; 79 }?
?
轉(zhuǎn)載于:https://www.cnblogs.com/huibixiaoxing/p/6979921.html
總結(jié)
以上是生活随笔為你收集整理的洛谷P2826 [USACO08NOV]光开关Light Switching [2017年6月计划 线段树02]的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [BZOJ1984] 月下“毛景树”
- 下一篇: 三个案例,解读静态代码块和构造代码块