EOJ 2525 Light Switching
来源:互联网 发布:html img js 编辑:程序博客网 时间:2024/05/09 07:27
http://acm.cs.ecnu.edu.cn/problem.php?problemid=2525
题意:给一排灯操作,0,s, e表示改变编号从s到e的灯的状态,1,s,e表示查询编号s到e的亮的盏数。
线段树,查询区间,更新区间。
#include <stdio.h>#include <map>#include <iostream>#include <algorithm>using namespace std;#define ls(p) p << 1 #define rs(p) p << 1 | 1const int maxn = 100005;struct tree{int l,r;int sum;int col;}t[maxn<<2];void pushup(int p){t[p].sum = t[ls(p)].sum + t[rs(p)].sum;}void pushdown(int p){if(t[p].col){t[ls(p)].col ^= 1;t[rs(p)].col ^= 1;t[ls(p)].sum = t[ls(p)].r - t[ls(p)].l + 1 - t[ls(p)].sum;t[rs(p)].sum = t[rs(p)].r - t[rs(p)].l + 1 - t[rs(p)].sum;t[p].col = 0;}}void build(int l, int r, int p){t[p].l = l;t[p].r = r;t[p].sum = 0;t[p].col = 0;if(l == r)return;int m = (l+r)>>1;build(l,m,ls(p));build(m+1,r,rs(p));}void update(int p, int L, int R){int l = t[p].l;int r = t[p].r;if(L <= l && r <= R){t[p].sum = r - l + 1 - t[p].sum;t[p].col ^= 1;return;}pushdown(p);int m = (l + r) >> 1;if(L <= m)update(ls(p), L, R);if(R > m)update(rs(p), L, R);pushup(p);}int query(int L,int R,int p) {int l = t[p].l;int r = t[p].r; if (L <= l && r <= R) {return t[p].sum;}pushdown(p);int m = (l + r) >> 1;int ret = 0;if(L <= m)ret += query(L , R , ls(p));if(R > m)ret += query(L , R , rs(p));return ret;}int main(){int n,m;while(~scanf("%d%d",&n,&m)){build(1,n,1);while(m --){int op,s,e;scanf("%d%d%d",&op,&s,&e);if(op == 0){update(1, s, e);}else{int ans = query(s,e,1);printf("%d\n",ans);}}}return 0;}
- EOJ 2525 Light Switching
- hdu3533 Light Switching Game
- Puzzle - Light Bulb Switching
- POJ 3533 Light Switching Game
- [TOJ] 3148 Light Switching -- 线段树
- 洛谷P2826[USACO08NOV]光开关Light Switching
- EOJ
- EOJ
- EOJ
- 【USACO 2008 Nov Gold】 3.Light Switching(lites 开关灯) 区间修改线段树
- Light
- EOJ gauss
- EOJ 1255
- EOJ 1851
- EOJ 2096
- EOJ 2141
- EOJ 2562
- EOJ 1818
- hdu 4602 Partition 快速幂,构造递推关系 (13多校#1,1003)
- 罕见橙色乳房云美景 云团酷似牛乳房而得名
- Ping其他电脑ping不通的解决方法
- Java技术开源全文本搜索引擎
- POJ 2352 Stars 树状数组(入门)
- EOJ 2525 Light Switching
- ExcelComon
- (Problem 34)Digit factorials
- ExcelConfig
- VIM常用命令与设置
- HTML 基础____字体
- 前缀表达式、中缀表达式、后缀表达式及转换实现
- ACM 位运算
- 国内数据库技术大牛:牛新庄博士自传(附:项目经验)