洛谷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 之间的灯有多少是亮着的.
输出格式:
输入输出样例
4 5
0 1 2
0 2 4
1 2 3
0 2 4
1 1 4
1
2
说明

原题时间限制为2s,内存限制为16M
至今才知道原来修改操作也需要下放标记,这样省去之前很多很麻烦的判断写法了T.T
利用异或的特性减少了很多if判断和修改。用1和0不断异或即可。
线段树记录区间1的数量,lazy标记记录当前字数是否应该反转
0^1 = 1 1^1 = 0 。。。。
#include <bits/stdc++.h>
inline void read(int &x)
{
x = ;char ch = getchar();char c = ch;
while(ch > '' || ch < '')c = ch, ch = getchar();
while(ch <= '' && ch >= '')x = x * + ch - '',ch = getchar();
if(c == '-')x = -x;
}
const int MAXN = + ;
const int MAXM = + ;
inline void swap(int &a,int &b)
{
int tmp = a;
a = b;
b = tmp;
} int stdata[MAXN << ];int lazy[MAXN << ];
//线段树存数字1的个数,lazy:0则不动, 1则反置 int n,m; void putdown(int o, int l, int r)
{
int mid = (l + r) >> ;
lazy[o << ] ^= lazy[o];
lazy[o << | ] ^= lazy[o];
lazy[o] = ;
stdata[o << ] = (mid - l + ) - stdata[o << ];
stdata[o << | ] = (r - mid) - stdata[o << | ];
} void modify(int ll, int rr, int o = , int l = , int r = n)
{
if(ll <= l && rr >= r)
{
stdata[o] = (r - l + ) - stdata[o];
lazy[o] ^= ;
return;
}
int mid = (l + r) >> ;
if(lazy[o])putdown(o, l, r);
if(mid >= ll)modify(ll, rr, o << , l, mid);
if(mid < rr) modify(ll, rr, o << | , mid + , r);
stdata[o] = stdata[o << ] + stdata[o << | ];
} int query(int ll, int rr, int o = , int l = , int r = n)
{
if(ll <= l && rr >= r)
{
return stdata[o];
}
int ans = ;
int mid = (l + r) >> ;
if(lazy[o]) putdown(o, l, r);
if(mid >= ll) ans += query(ll, rr, o << , l, mid);
if(mid < rr)ans += query(ll, rr, o << | , mid + , r);
return ans;
} int main()
{
read(n);read(m);
for(int i = ;i <= m;i ++)
{
int tmp1,tmp2,tmp3;
read(tmp1);read(tmp2);read(tmp3);
if(tmp1 == )
{
modify(tmp2, tmp3);
}
else
{
printf("%d\n", query(tmp2, tmp3));
}
}
return ;
}
洛谷P2826 [USACO08NOV]光开关Light Switching [2017年6月计划 线段树02]的更多相关文章
- 洛谷——P2846 [USACO08NOV]光开关Light Switching
P2846 [USACO08NOV]光开关Light Switching 题目大意: 灯是由高科技——外星人鼠标操控的.你只要左击两个灯所连的鼠标, 这两个灯,以及之间的灯都会由暗变亮,或由亮变暗.右 ...
- 洛谷P1774 最接近神的人_NOI导刊2010提高(02) [2017年6月计划 线段树03]
P1774 最接近神的人_NOI导刊2010提高(02) 题目描述 破解了符文之语,小FF开启了通往地下的道路.当他走到最底层时,发现正前方有一扇巨石门,门上雕刻着一幅古代人进行某种活动的图案.而石门 ...
- 洛谷P2723 丑数 Humble Numbers [2017年 6月计划 数论07]
P2723 丑数 Humble Numbers 题目背景 对于一给定的素数集合 S = {p1, p2, ..., pK},考虑一个正整数集合,该集合中任一元素的质因数全部属于S.这个正整数集合包括, ...
- 洛谷P2073 送花 [2017年6月计划 线段树01]
P2073 送花 题目背景 小明准备给小红送一束花,以表达他对小红的爱意.他在花店看中了一些花,准备用它们包成花束. 题目描述 这些花都很漂亮,每朵花有一个美丽值W,价格为C. 小明一开始有一个空的花 ...
- 洛谷P1352 没有上司的舞会 [2017年5月计划 清北学堂51精英班Day3]
P1352 没有上司的舞会 题目描述 某大学有N个职员,编号为1~N.他们之间有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子 结点的直接上司.现在有个周年庆宴会,宴会每邀请来一个职 ...
- 洛谷P1757 通天之分组背包 [2017年4月计划 动态规划06]
P1757 通天之分组背包 题目背景 直达通天路·小A历险记第二篇 题目描述 自01背包问世之后,小A对此深感兴趣.一天,小A去远游,却发现他的背包不同于01背包,他的物品大致可分为k组,每组中的物品 ...
- 洛谷 P3919 【模板】可持久化数组(可持久化线段树/平衡树)-可持久化线段树(单点更新,单点查询)
P3919 [模板]可持久化数组(可持久化线段树/平衡树) 题目背景 UPDATE : 最后一个点时间空间已经放大 标题即题意 有了可持久化数组,便可以实现很多衍生的可持久化功能(例如:可持久化并查集 ...
- 洛谷 P4125 [WC2012]记忆中的水杉树【扫描线+set+线段树】
我没有找到能在bzojAC的代码--当然我也WA了--但是我在洛谷过了,那就假装过了吧 minmax线段树一开始写的只能用min更新min,max更新max,实际上是可以互相更新的-- 首先看第二问, ...
- P2846 [USACO08NOV]光开关Light Switching
题目描述 Farmer John tries to keep the cows sharp by letting them play with intellectual toys. One of th ...
随机推荐
- htaccess apache重定向学习
1.推荐博客:http://www.cnblogs.com/adforce/archive/2012/11/23/2784664.html 2.测试工具:https://htaccess.madewi ...
- js封装设置获取cookie
var docCookies = { getItem: function (sKey) { return decodeURIComponent(document.cookie.replace(new ...
- Windows Server 2008 R2 部署服务
Windows Server 2008 R2 部署服务 部分参考: Windows Server 2008 R2 部署服务 - 马睿的技术博客 - 51CTO技术博客http://marui.blog ...
- 深入浅出 Java Concurrency (40): 并发总结 part 4 性能与伸缩性[转]
性能与伸缩性 使用线程的一种说法是为了提高性能.多线程可以使程序充分利用闲置的资源,提高资源的利用率,同时能够并行处理任务,提高系统的响应性. 但是很显然,引入线程的同时也引入了系统的复杂性.另外系统 ...
- 出错提示:“Could not flush the DNS Resolver Cache: 执行期间,函数出了问题”的解决方法
在DNS解析中,出错提示:"Could not flush the DNS Resolver Cache: 执行期间,函数出了问题"的解决方法 . 由于公司网站空间更换了服务商. ...
- eclipse memory analyzer对系统内存溢出堆文件解析0(转)
前言 在平时工作过程中,有时会遇到OutOfMemoryError,我们知道遇到Error一般表明程序存在着严重问题,可能是灾难性的.所以找出是什么原因造成OutOfMemoryError非常重要.现 ...
- 嘴巴题4 「BZOJ1827」[Usaco2010 Mar] gather 奶牛大集会
1827: [Usaco2010 Mar]gather 奶牛大集会 Description Bessie正在计划一年一度的奶牛大集会,来自全国各地的奶牛将来参加这一次集会.当然,她会选择最方便的地点来 ...
- CodeForces - 752B
CodeForces - 752Bhttps://vjudge.net/problem/597648/origin简单模拟,主要是细节特殊情况多考虑一下,看代码就行 #include<iostr ...
- Pickle(1)
1,pickle用于字符显示与存储之间的转换 2,要注意几个点 (1) 使用dump和load: (2) 版本号的要求: 3,官方文档的两个例子 4,pickle之后,数据是什么样的呢? https: ...
- Spring的自定义注解简单实现
1.注解的示例为在方法入参上加后缀 注解代码示例: @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documente ...