SPOJ 7259 Light Switching (水题,区间01取反)
#include <iostream>
#include <stdio.h>
#include <algorithm>
#define lson rt<<1,L,mid
#define rson rt<<1|1,mid+1,R
/*
水题
题意:给出n个初始为0的数,有两种操作
0 a b 将区间[a,b]取反
1 a b 查询区间[a,b]中1的个数
*/
using namespace std;
const int maxn=;
int n,m; struct Node{
int num0,num1; //统计该区间0和1的个数
bool flag;
}tree[maxn<<]; void pushUp(int rt){
tree[rt].num0=tree[rt<<].num0+tree[rt<<|].num0;
tree[rt].num1=tree[rt<<].num1+tree[rt<<|].num1;
}
void build(int rt,int L,int R){
tree[rt].num0=R-L+;
tree[rt].num1=;
tree[rt].flag=false;
if(L==R)
return;
int mid=(L+R)>>;
build(lson);
build(rson);
pushUp(rt);
} void pushDown(Node &rt,Node &ls,Node &rs){
if(rt.flag){
ls.flag=!ls.flag; //一开始将ls和rs的flag直接设为true了,导致WA。。。
rs.flag=!rs.flag;
swap(ls.num0,ls.num1);
swap(rs.num0,rs.num1);
rt.flag=false;
}
}
void update(int rt,int L,int R,int l,int r){
if(l<=L&&R<=r){
tree[rt].flag=!tree[rt].flag;
swap(tree[rt].num0,tree[rt].num1);
return;
}
pushDown(tree[rt],tree[rt<<],tree[rt<<|]);
int mid=(R+L)>>;
if(l<=mid)
update(lson,l,r);
if(r>mid)
update(rson,l,r);
pushUp(rt);
} int query(int rt,int L,int R,int l,int r){
if(l<=L&&R<=r){
return tree[rt].num1;
}
pushDown(tree[rt],tree[rt<<],tree[rt<<|]);
int mid=(L+R)>>;
int ret=;
if(l<=mid)
ret+=query(lson,l,r);
if(r>mid)
ret+=query(rson,l,r);
return ret;
}
int main()
{
int t,a,b;
scanf("%d%d",&n,&m);
build(,,n);
for(int i=;i<=m;i++){
scanf("%d%d%d",&t,&a,&b);
if(t==){
update(,,n,a,b);
}
else{
printf("%d\n",query(,,n,a,b));
}
}
return ;
}
SPOJ 7259 Light Switching (水题,区间01取反)的更多相关文章
- SPOJ 3693 Maximum Sum(水题,记录区间第一大和第二大数)
#include <iostream> #include <stdio.h> #include <algorithm> #define lson rt<< ...
- Codeforces Round #367 (Div. 2)---水题 | dp | 01字典树
A.Beru-taxi 水题:有一个人站在(sx,sy)的位置,有n辆出租车,正向这个人匀速赶来,每个出租车的位置是(xi, yi) 速度是 Vi;求人最少需要等的时间: 单间循环即可: #inclu ...
- hiho152周 - 水题 区间问题
题目链接 给定两个区间集合 A 和 B,其中集合 A 包含 N 个区间[ A1, A2 ], [ A3, A4 ], ..., [ A2N-1, A2N ],集合 B 包含 M 个区间[ B1, B2 ...
- 采药 水题 dp 01背包问题 luogu1048
最基本的01背包,不需要太多解释,刚学dp的同学可以参见dd大牛的背包九讲,直接度娘“背包九讲”即可搜到 #include <cstdio> #include <cstring> ...
- Distinct Substrings SPOJ - DISUBSTR(后缀数组水题)
求不重复的子串个数 用所有的减去height就好了 推出来的... #include <iostream> #include <cstdio> #include <sst ...
- SPOJ - AMR11H Array Diversity (水题排列组合或容斥)
题意:给定一个序列,让你求两种数,一个是求一个子序列,包含最大值和最小值,再就是求一个子集包含最大值和最小值. 析:求子序列,从前往记录一下最大值和最小值的位置,然后从前往后扫一遍,每个位置求一下数目 ...
- SPOJ CNTPRIME 13015 Counting Primes (水题,区间更新,求区间的素数个数)
题目连接:http://www.spoj.com/problems/CNTPRIME/ #include <iostream> #include <stdio.h> #incl ...
- [HDU 2602]Bone Collector ( 0-1背包水题 )
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 水题啊水题 还给我WA了好多次 因为我在j<w[i]的时候状态没有下传.. #includ ...
- LuoguP2846[USACO08NOV]光开关Light Switching【线段树维护区间异或】By cellur925
题目传送门 题目大意,给你一串灯,按一下开关可以将灯的状态取反(开变成关,关变成开).维护这个序列的两种操作:询问区间内有多少灯是开着的,区间按灯. 开始想的是分别维护区间内0的数量,1的数量,两个懒 ...
随机推荐
- SpringMvc入门二----HelloWorld
1. 导入需要的架包: 2. 配置web.xml,添加Servlet <servlet> <servlet-name>springmvc</servlet-name> ...
- C++中delete[]是如何知道数组大小的
先看一段代码: int main(void) { int *pI = new int; int *pArray = new int[10]; int size = *(pArray-1); delet ...
- debian终端菱形乱码修复
最简安装debian的时候由于没有中文字库,若选择看中文环境会出现菱形乱码.先把zh.utf8换为us.utf8看着好顺眼些.按空格键取消已选的zh.utf8选项按空格键选择us.utf8选项ok
- ECC校验原理以及在Nand Flash中的应用
本篇文章主要介绍ECC基本原理以及在Nand Flash中的应用,本文记录自己对ECC校验原理的理解和学习. ECC介绍 ECC,全称为Error Correcting Code, ...
- 在线生成ICO图标、站标
网上一搜有很多,找了两个比较好用的,分别是http://ico.storyren.com/和http://www.ico.la/,前面的那个好像更好点.上传png.jpg.或gif格式的图片,按自己需 ...
- xml_editor
概要 该工程是用来操作xml, 目的是为了在程序中操作xml中各类节点更加简单, 下面按照 工程简介, 库内部实现, 库接口使用, xml工具使用, xpath简介 几个部分来介绍该c++库. 工程简 ...
- [转]Linux 分区 swap
如何合理设置Linux的swap分区 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://commandos.blog.51cto.c ...
- window store app 附件读取
public static async Task<bool> DisplayApplicationPicker(string folderName, string fileName) { ...
- HTTP报文格式
请求报文 <method><SP><uri><SP><version><CRLF> <head-name><: ...
- [转]如何利用ndk-stack工具查看so库的调用堆栈【代码示例】?
如何利用ndk-stack工具查看so库的调用堆栈[代码示例]? http://hi.baidu.com/subo4110/item/d00395b3bf63e4432bebe36d Step1:An ...