洛谷P2826 [USACO08NOV]光开关Light Switching [2017年6月计划 线段树02]

P2826 [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

 

至今才知道原来修改操作也需要下放标记,这样省去之前很多很麻烦的判断写法了T.T

利用异或的特性减少了很多if判断和修改。用1和0不断异或即可。

线段树记录区间1的数量,lazy标记记录当前字数是否应该反转

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 //线段树存数字1的个数,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 }

 

 

posted @ 2017-06-10 21:00  嘒彼小星  阅读(242)  评论(0编辑  收藏  举报