洛谷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 }
时间: 2024-08-05 20:39:17

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

Luogu 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 th

洛谷P2723 丑数 Humble Numbers [2017年 6月计划 数论07]

P2723 丑数 Humble Numbers 题目背景 对于一给定的素数集合 S = {p1, p2, ..., pK},考虑一个正整数集合,该集合中任一元素的质因数全部属于S.这个正整数集合包括,p1.p1*p2.p1*p1.p1*p2*p3...(还有其 它).该集合被称为S集合的“丑数集合”.注意:我们认为1不是一个丑数. 题目描述 你的工作是对于输入的集合S去寻找“丑数集合”中的第N个“丑数”.所有答案可以用longint(32位整数)存储. 补充:丑数集合中每个数从小到大排列,每个丑

【洛谷】P2073 送花 [2017年6月计划 线段树01]

P2073 送花 题目背景 小明准备给小红送一束花,以表达他对小红的爱意.他在花店看中了一些花,准备用它们包成花束. 题目描述 这些花都很漂亮,每朵花有一个美丽值W,价格为C. 小明一开始有一个空的花束,他不断地向里面添加花.他有以下几种操作: 操作 含义 1 W C 添加一朵美丽值为W,价格为C的花. 3 小明觉得当前花束中最便宜的一朵花太廉价,不适合送给小红,所以删除最便宜的一朵花. 2 小明觉得当前花束中最贵的一朵花太贵,他心疼自己的钱,所以删除最贵的一朵花. -1 完成添加与删除,开始包

洛谷P1774 最接近神的人_NOI导刊2010提高(02) [2017年6月计划 线段树03]

P1774 最接近神的人_NOI导刊2010提高(02) 题目描述 破解了符文之语,小FF开启了通往地下的道路.当他走到最底层时,发现正前方有一扇巨石门,门上雕刻着一幅古代人进行某种活动的图案.而石门上方用古代文写着“神的殿堂”.小FF猜想里面应该就有王室的遗产了.但现在的问题是如何打开这扇门…… 仔细研究后,他发现门上的图案大概是说:古代人认为只有智者才是最容易接近神明的.而最聪明的人往往通过一种仪式选拔出来.仪式大概是指,即将隐退的智者为他的候选人写下一串无序的数字,并让他们进行一种操作,即

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 th

洛谷P2835 刻录光盘 [2017年6月计划 强连通分量02]

P2835 刻录光盘 题目描述 在JSOI2005夏令营快要结束的时候,很多营员提出来要把整个夏令营期间的资料刻录成一张光盘给大家,以便大家回去后继续学习.组委会觉得这个主意不错!可是组委会一时没有足够的空光盘,没法保证每个人都能拿到刻录上资料的光盘,又来不及去买了,怎么办呢?! 组委会把这个难题交给了LHC,LHC分析了一下所有营员的地域关系,发现有些营员是一个城市的,其实他们只需要一张就可以了,因为一个人拿到光盘后,其他人可以带着U盘之类的东西去拷贝啊! 可是,LHC调查后发现,由于种种原因

洛谷P3459 [POI2007]MEG-Megalopolis [2017年6月计划 树上问题02]

[POI2007]MEG-Megalopolis 题目描述 Byteotia has been eventually touched by globalisation, and so has Byteasar the Postman, who once roamedthe country lanes amidst sleepy hamlets and who now dashes down the motorways. But it is those strolls inthe days of

洛谷 P1020 导弹拦截 【最长上升子序列】 || 【线段树】

题目链接:https://www.luogu.org/problemnew/show/P1020 题目描述 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能高于前一发的高度.某天,雷达捕捉到敌国的导弹来袭.由于该系统还在试用阶段,所以只有一套系统,因此有可能不能拦截所有的导弹. 输入导弹依次飞来的高度(雷达给出的高度数据是不大于50000的正整数),计算这套系统最多能拦截多少导弹,如果要拦截所有导弹

【模板】LIS模板 洛谷P1091 [NOIP2004提高组]合唱队形 [2017年4月计划 动态规划11]

以题写模板. 写了两个:n^2版本与nlogn版本 P1091 合唱队形 题目描述 N位同学站成一排,音乐老师要请其中的(N-K)位同学出列,使得剩下的K位同学排成合唱队形. 合唱队形是指这样的一种队形:设K位同学从左到右依次编号为1,2…,K,他们的身高分别为T1,T2,…,TK, 则他们的身高满足T1<...<Ti>Ti+1>…>TK(1<=i<=K). 你的任务是,已知所有N位同学的身高,计算最少需要几位同学出列,可以使得剩下的同学排成合唱队形. 输入输出格