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. SPSS超详细操作:分层回归(hierarchical multiple regression)

    SPSS超详细操作:分层回归(hierarchical multiple regression) 1.问题与数据 最大携氧能力(maximal aerobic capacity, VO2max)是评价 ...

  2. java代码优化写法1(转摘)

    源文地址:https://blog.csdn.net/qq_15766297/article/details/70503222 代码优化,一个很重要的课题.可能有些人觉得没用,一些细小的地方有什么好修 ...

  3. java编程题古典算法之兔子问题

    1.题目如下. 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 分析每个月的兔子对数: 1---- ...

  4. MySQL数据库--创建表,查询

    MySQL创建表: 表(一)Student (学生表): CREATE TABLE `Student` ( `sno` ) DEFAULT NULL, `sname` ) DEFAULT NULL, ...

  5. Windowns API 第24篇 WTSEnumerateSessions 枚举session信息

    函数原型: BOOL WTSEnumerateSessions( HANDLE hServer, DWORD Reserved, DWORD Version, PWTS_SESSION_INFO* p ...

  6. Spring的自定义注解简单实现

    1.注解的示例为在方法入参上加后缀 注解代码示例: @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documente ...

  7. SQL的特点

    1.综合统一 2.高度非过程化 3.面向集合的操作方式 4.一同一种语法结构提供两种使用5.语言简洁易学易用

  8. centos 安装系列

    装coreseek https://www.jb51.net/os/RedHat/462185.html 卸载mysql https://www.cnblogs.com/cyl048/p/687908 ...

  9. Java Iterator模式

    Iterator迭代器的定义:迭代器(Iterator)模式,又叫做游标(Cursor)模式.GOF给出的定义为:提供一种方法访问一个容器(container)对象中各个元素,而又不需暴露该对象的内部 ...

  10. vim编辑shell

      vi编辑 u撤销 i输入 dd删除游标所在的那一整行(常用) yy复制游标所在的那一行(常用) p 为将已复制的数据在光标下一行贴上 nyy n 为数字.复制光标所在的向下 n 行,例如 20yy ...