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 }

转载于:https://www.cnblogs.com/huibixiaoxing/p/6979921.html

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

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

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

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

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

  3. 洛谷P4458 /loj#2512.[BJOI2018]链上二次求和(线段树)

    题面 传送门(loj) 传送门(洛谷) 题解 我果然是人傻常数大的典型啊-- 题解在这儿 //minamoto #include<bits/stdc++.h> #define R regi ...

  4. 洛谷P4555 [国家集训队]最长双回文串(manacher 线段树)

    题意 题目链接 Sol 我的做法比较naive..首先manacher预处理出以每个位置为中心的回文串的长度.然后枚举一个中间位置,现在要考虑的就是能覆盖到i - 1的回文串中 中心最靠左的,和能覆盖 ...

  5. 洛谷 - P4556 [Vani有约会]雨天的尾巴 /【模板】线段树合并(树上差分+线段树合并)

    题目链接:点击查看 题目大意:给出一棵树,再给出 m 次操作,每次操作会选择两个点 ( x , y ) ,使得这条路径上的所有点的种类 z 加一,最后问每个点的哪个种类出现的频率最高,若多个种类出现频 ...

  6. 洛谷P2429 制杖题 [2017年6月计划 数论10]

    P2429 制杖题 题目描述 求不大于 m 的. 质因数集与给定质数集有交集的自然数之和. 输入输出格式 输入格式: 第一行二个整数 n,m. 第二行 n 个整数,表示质数集内的元素 p[i]. 输出 ...

  7. 洛谷P1474 [USACO 2.3]货币系统 Money Systems [2017年4月计划 动态规划04]

    P1474 货币系统 Money Systems 题目描述 母牛们不但创建了它们自己的政府而且选择了建立了自己的货币系统.由于它们特殊的思考方式,它们对货币的数值感到好奇. 传统地,一个货币系统是由1 ...

  8. 洛谷 P2919 [USACO08NOV]守护农场Guarding the Farm

    题目描述 The farm has many hills upon which Farmer John would like to place guards to ensure the safety ...

  9. 洛谷 P3750 [六省联考2017]分手是祝愿

    传送门 题解 //Achen #include<algorithm> #include<iostream> #include<cstring> #include&l ...

最新文章

  1. java学习之借书系统
  2. 关于无服务器(Serverless)架构你要搞懂的8件事
  3. window.open打开一个新空白页面,不会自动刷新【解决方案】
  4. Tomcat集群快速入门2
  5. 关于编译PCL1.71
  6. JLOI2015 解题报告
  7. uni-app 实现小程序rsa加密(非对称加密原理)
  8. 计算机网络中缓存技术,编程达人
  9. Java对象类型转换
  10. cdlinux 无线网密码破解
  11. 2022 XbotPark(冬季)科创训练营重庆明月湖站总结
  12. Java面经背诵版(一)
  13. opencv 摄像头基本使用
  14. 《离散数学》速成-练习题答案(含题目)
  15. 《微积分:一元函数积分学》——基本积分表
  16. excel自动调整列宽_Java 设置Excel自适应行高、列宽
  17. CF1009F Dominant Indices
  18. Slidworks2018基础到实战设计视频教程 产品建模 渲染 钣金设计
  19. 计算机网络系统的维护,简论事业单位计算机网络管理系统的维护
  20. 【热搜】想卷深度学习必会的10题【最全AI面经】

热门文章

  1. 共享卫士2.08.03下载
  2. 天啊,为什么我的 Redis 变慢了。。
  3. Java 强、弱、软、虚,你属于哪一种?
  4. Spring Boot 中的 RestTemplate 不好用?试试 Retrofit!
  5. 阿里巴巴在 Serverless 计算领域的探索
  6. Nginx 搭建图片服务器
  7. Andriod --- JetPack (四):BaseObservable 与 ObservableField 双向绑定
  8. Android --- TabHost 切换时,改变选项卡下字体的状态(大小、加粗、默认被选中第一个)
  9. Springboot+JPA 对应关系查询时导致的堆栈溢出 :java.lang.StackOverflowError:
  10. Android --- Android layout属性应有尽有