#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取反)的更多相关文章

  1. SPOJ 3693 Maximum Sum(水题,记录区间第一大和第二大数)

    #include <iostream> #include <stdio.h> #include <algorithm> #define lson rt<< ...

  2. Codeforces Round #367 (Div. 2)---水题 | dp | 01字典树

    A.Beru-taxi 水题:有一个人站在(sx,sy)的位置,有n辆出租车,正向这个人匀速赶来,每个出租车的位置是(xi, yi) 速度是 Vi;求人最少需要等的时间: 单间循环即可: #inclu ...

  3. hiho152周 - 水题 区间问题

    题目链接 给定两个区间集合 A 和 B,其中集合 A 包含 N 个区间[ A1, A2 ], [ A3, A4 ], ..., [ A2N-1, A2N ],集合 B 包含 M 个区间[ B1, B2 ...

  4. 采药 水题 dp 01背包问题 luogu1048

    最基本的01背包,不需要太多解释,刚学dp的同学可以参见dd大牛的背包九讲,直接度娘“背包九讲”即可搜到 #include <cstdio> #include <cstring> ...

  5. Distinct Substrings SPOJ - DISUBSTR(后缀数组水题)

    求不重复的子串个数 用所有的减去height就好了 推出来的... #include <iostream> #include <cstdio> #include <sst ...

  6. SPOJ - AMR11H Array Diversity (水题排列组合或容斥)

    题意:给定一个序列,让你求两种数,一个是求一个子序列,包含最大值和最小值,再就是求一个子集包含最大值和最小值. 析:求子序列,从前往记录一下最大值和最小值的位置,然后从前往后扫一遍,每个位置求一下数目 ...

  7. SPOJ CNTPRIME 13015 Counting Primes (水题,区间更新,求区间的素数个数)

    题目连接:http://www.spoj.com/problems/CNTPRIME/ #include <iostream> #include <stdio.h> #incl ...

  8. [HDU 2602]Bone Collector ( 0-1背包水题 )

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 水题啊水题 还给我WA了好多次 因为我在j<w[i]的时候状态没有下传.. #includ ...

  9. LuoguP2846[USACO08NOV]光开关Light Switching【线段树维护区间异或】By cellur925

    题目传送门 题目大意,给你一串灯,按一下开关可以将灯的状态取反(开变成关,关变成开).维护这个序列的两种操作:询问区间内有多少灯是开着的,区间按灯. 开始想的是分别维护区间内0的数量,1的数量,两个懒 ...

随机推荐

  1. C++ 实现不能被继承的类

    方法一: #include <iostream> using namespace std; class A { public: static A* getInstance(); stati ...

  2. Viewport Resizer下载 谷歌前端自适应开发工具

    原文链接:http://www.phpbiji.cn/article/index/id/107/cid/6.html Viewport Resizer下载 谷歌前端自适应开发工具 在前端开发过程中,随 ...

  3. Linux操作杂记

    centos7修改默认运行等级 查看当前默认运行等级: systemctl get-dafault 修改默认运行等级为5: systemctl set-default graphical.target ...

  4. jquery easyui combox实用方法记录

    // combogrid刷新 $(“#cc").combogrid('grid').datagrid('load'); // combogrid设置默认选中哪一行 $('#cc').comb ...

  5. Winform合并多个Excel文件到一个文件中(源文件.xls,实际是.xml)

    1.下面两个文件.xls是给的文件,实际上是.xml文件 2.具体的代码 private void btOK_Click(object sender, EventArgs e) { //0.获取路径文 ...

  6. asp.net图片上传实例

    网站后台都需要有上传图片的功能,下面的例子就是实现有关图片上传. 缺点:图片上传到本服务器上,不适合大量图片上传. 第一.图片上传,代码如下: xxx.aspx 复制代码代码如下: <td cl ...

  7. 深入解析PHP 5.3.x 的strtotime() 时区设定 警告信息修复

    在某些参考资料中是说这两个方法任选其一就可,但经我测试,必须两个方法同时使用,才不会再出现错误提示 PHP Warning: strtotime(): It is not safe to rely o ...

  8. sqlServer2008 手工注入

    接着上一篇的<mysql手工注入> 参考:http://hi.baidu.com/ciqing_s/item/971bf994365130accc80e5ed http://hi.baid ...

  9. php进行多个数组合并zip

    $a = array_zip(array("Dog","Cat","Horse"), array(1,2,3), array("l ...

  10. C#中Delegate

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...