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 。。。。

 #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]的更多相关文章

  1. 洛谷——P2846 [USACO08NOV]光开关Light Switching

    P2846 [USACO08NOV]光开关Light Switching 题目大意: 灯是由高科技——外星人鼠标操控的.你只要左击两个灯所连的鼠标, 这两个灯,以及之间的灯都会由暗变亮,或由亮变暗.右 ...

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

    P1774 最接近神的人_NOI导刊2010提高(02) 题目描述 破解了符文之语,小FF开启了通往地下的道路.当他走到最底层时,发现正前方有一扇巨石门,门上雕刻着一幅古代人进行某种活动的图案.而石门 ...

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

    P2723 丑数 Humble Numbers 题目背景 对于一给定的素数集合 S = {p1, p2, ..., pK},考虑一个正整数集合,该集合中任一元素的质因数全部属于S.这个正整数集合包括, ...

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

    P2073 送花 题目背景 小明准备给小红送一束花,以表达他对小红的爱意.他在花店看中了一些花,准备用它们包成花束. 题目描述 这些花都很漂亮,每朵花有一个美丽值W,价格为C. 小明一开始有一个空的花 ...

  5. 洛谷P1352 没有上司的舞会 [2017年5月计划 清北学堂51精英班Day3]

    P1352 没有上司的舞会 题目描述 某大学有N个职员,编号为1~N.他们之间有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子 结点的直接上司.现在有个周年庆宴会,宴会每邀请来一个职 ...

  6. 洛谷P1757 通天之分组背包 [2017年4月计划 动态规划06]

    P1757 通天之分组背包 题目背景 直达通天路·小A历险记第二篇 题目描述 自01背包问世之后,小A对此深感兴趣.一天,小A去远游,却发现他的背包不同于01背包,他的物品大致可分为k组,每组中的物品 ...

  7. 洛谷 P3919 【模板】可持久化数组(可持久化线段树/平衡树)-可持久化线段树(单点更新,单点查询)

    P3919 [模板]可持久化数组(可持久化线段树/平衡树) 题目背景 UPDATE : 最后一个点时间空间已经放大 标题即题意 有了可持久化数组,便可以实现很多衍生的可持久化功能(例如:可持久化并查集 ...

  8. 洛谷 P4125 [WC2012]记忆中的水杉树【扫描线+set+线段树】

    我没有找到能在bzojAC的代码--当然我也WA了--但是我在洛谷过了,那就假装过了吧 minmax线段树一开始写的只能用min更新min,max更新max,实际上是可以互相更新的-- 首先看第二问, ...

  9. P2846 [USACO08NOV]光开关Light Switching

    题目描述 Farmer John tries to keep the cows sharp by letting them play with intellectual toys. One of th ...

随机推荐

  1. neo4j 实战、实例、示例 创建电影关系图 -1

    1. 创建关系 因为代码占篇幅太大,创建整个"电源关系图"的代码在文章最下方. 2. 简单分析创建语句 2.1 创建电影节点 CREATE (TheMatrix:Movie {ti ...

  2. mac下解压bin文件

    在mac下要解压Android-ndk-r10e-darwin-x86_64.bin文件. 1.进入文件所在目录,修改文件的读取权限 chmod a+x android-ndk-r10e-darwin ...

  3. 怎么解决VirtualBox无法安装增强工具

    点击「设备」-「安装增强功能」,然后就弹出下面这个东西,百度和 bing 了很久,终于解决啦~ Unable to insert the virtual optical disk D:\Program ...

  4. ES6之主要知识点(九)Set和Map

    1.Set ES6 提供了新的数据结构 Set.它类似于数组,但是成员的值都是唯一的,没有重复的值. Set 本身是一个构造函数,用来生成 Set 数据结构. const s = new Set(); ...

  5. window环境下Python+OpenCV配置

    最近开始学习OpenCV来进行计算机视觉实验,选择了Python作为实验语言,工欲善其事,必先利其器.先总结下安装配置. 现在opencv目测只支持Python2.7X版本的,还依赖于numpy和ma ...

  6. linux支持大容量硬盘

    1.fdisk使用msdos格式分区,最大支持2T硬盘,要使用大于2T硬盘需使用parted命令使用GPT格式分区. 2.除修改分区格式外,linux内核需添加GPT分区格式支持,修改如下: CONF ...

  7. linux拆分文件

    1.先看下文件总的行数: wc -l filename 我们现在来看看它具体的参数该怎么用: split支持自定义输出文件大小和输出文件行数两种模式,此外还可以定义每一行最大的值. -l 按输出文件行 ...

  8. 图解nginx配置文件nginx.conf

    1. 一个server表示一个虚拟主机, 说白了就是网站, 一个nginx可以有多个server 2. listen网站监听的端口 3. server_name网站的域名 4. root是网站的相对目 ...

  9. 廖雪峰Java11多线程编程-1线程的概念-3线程的状态

    1线程的状态 线程终止的的原因: run()或call()方法执行完成,线程正常结束 线程抛出一个未捕获的Exception或Error 直接调用该线程的stop()方法来结束该线程--该方法容易导致 ...

  10. day45作业

    利用 html + css 写一个最基本的页面 <!DOCTYPE html> <html lang="en"> <head> <meta ...