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 之间的灯有多少是亮着的.
输出格式:
输入输出样例
4 5
0 1 2
0 2 4
1 2 3
0 2 4
1 1 4
1
2
说明
原题时间限制为2s,内存限制为16M
Solution:
本题比较水(是真的很水)。
和以前做过的某道好像叫做开关的题目一模一样。
线段树维护区间和,区间修改时等价于区间取反,$sum$变为长度减去原来的和,打个异或懒惰标记(两次操作等于没有操作)。
练手瞎搞一弃就好了。
代码:
#include<bits/stdc++.h>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define il inline
using namespace std; const int N=;
int n,m,sum[N*],lazy[N*]; il int gi(){
int a=;char x=getchar();
while(x<''||x>'')x=getchar();
while(x>=''&&x<='')a=(a<<)+(a<<)+x-,x=getchar();
return a;
} il void pushup(int rt){sum[rt]=sum[rt<<]+sum[rt<<|];} il void pushdown(int rt,int len){
if(lazy[rt]){
lazy[rt<<]^=;lazy[rt<<|]^=;
sum[rt<<]=(len-(len>>))-sum[rt<<];
sum[rt<<|]=(len>>)-sum[rt<<|];
lazy[rt]=;
}
} il void update(int L,int R,int l,int r,int rt){
if(L<=l&&R>=r){sum[rt]=(r-l+)-sum[rt];lazy[rt]^=;return;}
pushdown(rt,r-l+);
int m=l+r>>;
if(L<=m)update(L,R,lson);
if(R>m)update(L,R,rson);
pushup(rt);
} il int query(int L,int R,int l,int r,int rt){
if(L<=l&&R>=r)return sum[rt];
pushdown(rt,r-l+);
int m=l+r>>,ret=;
if(L<=m)ret+=query(L,R,lson);
if(R>m)ret+=query(L,R,rson);
return ret;
} int main(){
n=gi(),m=gi();
int f,x,y;
while(m--){
f=gi(),x=gi(),y=gi();
if(!f)update(x,y,,n,);
else printf("%d\n",query(x,y,,n,));
}
return ;
}
P2846 [USACO08NOV]光开关Light Switching的更多相关文章
- 洛谷——P2846 [USACO08NOV]光开关Light Switching
P2846 [USACO08NOV]光开关Light Switching 题目大意: 灯是由高科技——外星人鼠标操控的.你只要左击两个灯所连的鼠标, 这两个灯,以及之间的灯都会由暗变亮,或由亮变暗.右 ...
- 洛谷P2826 [USACO08NOV]光开关Light Switching [2017年6月计划 线段树02]
P2826 [USACO08NOV]光开关Light Switching 题目描述 Farmer John tries to keep the cows sharp by letting them p ...
- LuoguP2846[USACO08NOV]光开关Light Switching【线段树维护区间异或】By cellur925
题目传送门 题目大意,给你一串灯,按一下开关可以将灯的状态取反(开变成关,关变成开).维护这个序列的两种操作:询问区间内有多少灯是开着的,区间按灯. 开始想的是分别维护区间内0的数量,1的数量,两个懒 ...
- 洛谷P2846 光开关Light Switching
题目描述 灯是由高科技--外星人鼠标操控的.你只要左击两个灯所连的鼠标, 这两个灯,以及之间的灯都会由暗变亮,或由亮变暗.右击两个灯所连的鼠 标,你就可以知道这两个灯,以及之间的灯有多少灯是亮的.起初 ...
- SPOJ 7259 Light Switching (水题,区间01取反)
#include <iostream> #include <stdio.h> #include <algorithm> #define lson rt<< ...
- POJ 3533 Light Switching Game(三维Nim积)题解
思路:三维Nim积 代码: #include<set> #include<map> #include<stack> #include<cmath> #i ...
- POJ 3553 Light Switching Game 博弈论 nim积 sg函数
http://poj.org/problem?id=3533 变成三维的nim积..前面hdu那个算二维nim积的题的函数都不用改,多nim积一次就过了...longlong似乎不必要但是还是加上了 ...
- USACO 2008 Nov Gold 3.Light Switching 线段树
Code: #include<cstdio> #include<algorithm> #include<cstring> using namespace std; ...
- Light Switching(SPOJ LITE)—— 线段树成段更新异或值
题目连接:http://www.spoj.com/problems/LITE/en/. 题意:有若干个灯泡,每次对一段操作,这一段原先是亮的,就关了:原先是关着的,就打开.询问某一段的打开的灯泡的个数 ...
随机推荐
- 【BZOJ4866】[YNOI2017] 由乃的商场之旅(莫队)
点此看题面 大致题意: 给你一个字符串,每次给你一段区间,问这段区间内有多少个字符串在重新排列后可以变成一个回文串. 关于莫队 详见这篇博客:莫队算法学习笔记(一)--普通莫队. 关于回文 要使一个字 ...
- 【51nod1705】七星剑(成环DP)
点此看题面 大致题意: 你要把一把剑从0星升至7星,有n颗宝石供你选择,第i颗宝石的价值是c[i],用第i颗宝石将剑从k-1星升至k星的成功率是prob[k][i],而失败后会掉lose[k][i], ...
- 2017.12.20 Java中的 IO/XML学习总结 File类详细
IO / XML 一.File类 1.定义/概念 Java是面向对象的语言,要想把数据存到文件中,就必须要有一个对象表示这个文件.File类的作用就是代表一个特定的文件或目录,并提供了若干方法对这些文 ...
- ScriptMaker
0x00 前言 pwn脚本千篇一律,之前也是保存了一份模板,每次都用它,但还是觉得每次都复制一次各种名字还是有的累,于是就写了一份脚本生成器 0x01 ScriptMaker #!/usr/bin/e ...
- python查看安装包
D:\Python27\Scripts>pip listbackports.ssl-match-hostname (3.4.0.2)basicauth (0.2)certifi (14.5.14 ...
- 第二篇:ssh.invoke_shell() 切换root出现的新问题
接上一篇:按照上一篇的方式,在没有对ssh.invoke_shell()执行后的登录提示符进行判断的话,那边有部分机器就回因为返回为空导致程序卡死. 正常机器 ssh.recv(9999) 命令返 ...
- wusir 面试题答案在老男孩的视频里
注意:你问答案在哪里?答案在视频里了,就是不给你写. 第一部分 Python基础篇(80题) 为什么学习Python? 通过什么途径学习的Python? Python和Java.PHP.C.C#.C+ ...
- 多线程并发测试(apache ad)
1.配置 ThreadPoolTaskExecutor bean <?xml version="1.0" encoding="UTF-8"?> ...
- python3.7 time模块
#!/usr/bin/env python __author__ = "lrtao2010" #python3.7 time模块 #time模块没有time.py文件,是内置到解释 ...
- B1023 组个最小数 (20分)
B1023 组个最小数 (20分) 给定数字 0-9各若干个.你可以以任意顺序排列这些数字,但必须全部使用.目标是使得最后得到的数尽可能小(注意 0 不能做首位).例如:给定两个 0,两个 1,三个 ...