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的数量,两个懒 ...
随机推荐
- Integer类的装箱和拆箱到底是怎样实现的?
先解释一下装箱和拆箱: 装箱就是 自动将基本数据类型转换为包装器类型:拆箱就是 自动将包装器类型转换为基本数据类型. 下表是基本数据类型对应的包装器类型: int(4字节) Integer byt ...
- 12个git实战建议和技巧
摘要:git无疑是现在最热门的版本控制工具,而且正在进一步侵占SVN以及CVS的市场.本文作者从国外技术问答社区Stack Overflow整理的12个很实用的git使用技巧和建议,希望对你有帮助. ...
- linux服务器修改ftp默认21端口方法
1.登录服务器,打开vsftp.conf文件 # vim /etc/vsftpd/vsftpd.conf 2.在文件末尾增加listen_port=8021 #remote_charset=CP125 ...
- 【译】Android系统简介—— Activity
续上一篇,继续介绍Android系统.上一篇: [译]Android系统简介 本文主要介绍构建Android应用的一些主要概念: Activity Activity是应用程序中一个单独的有UI的页面( ...
- matlab实现复合梯形法则
复合梯形法则: function int_f = CompoundEchelon( f, a, b, m ) % input : f : function handler % a : the lowe ...
- String对象中常用的方法
String对象中常用的方法 1.charCodeAt方法返回一个整数,代表指定位置字符的Unicode编码.strObj.charCodeAt(index)说明:index将被处理字符的从零开始 ...
- 微软职位内部推荐-Principal Dev Manager
微软近期Open的职位: Title: Principal Dev Manager Location: Beijing The R&D of Shared Data Platform at S ...
- VS2013中设置大小写的快捷键
1.我们在定义头文件时,通常需要定义: #ifndef _MainMenu_H_#define _MainMenu_H_ your code... #endif 我们需要将头文件名设置为大写的: ...
- cocos2dx中的格子地图TileMap
格子地图的优点: a.节省内存,我们知道对于一款游戏来说,如果以图片来作为地图的话,对于神庙逃亡,魂斗罗这样的场景很多,地图很长的游戏显然不现实,因为图片很占内存,但是这些游戏的地图有一个特点就是:重 ...
- Thinking in life(1)
There is always one things we donot notice---time ,which is the most important to all of us.By watch ...