#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. ie8中使用placeholder

    placeholder 是 html5 中的新属性,考虑到还有不少 ie8 的用户,所以找了一个 ie8 的 placeholder 的补丁,如下: <script type="tex ...

  2. Cllimbing Stairs [LeetCode 70]

    1- 问题描述 You are climbing a stair case. It takes n steps to reach to the top. Each time you can eithe ...

  3. UIPickerView基本用法

    #import "ViewController.h" #import <UIKit/UIKit.h> @interface ViewController : UIVie ...

  4. mysql数据库创建database(实例),和用户,并授权

    前言:mysql创建用户的方法分成三种:INSERT USER表的方法.CREATE USER的方法.GRANT的方法. 一.账号名称的构成方式 账号的组成方式:用户名+主机(所以可以出现重复的用户名 ...

  5. Linux C 程序 进程间通信(20)

    进程间通信 1.进程间通信的几种手段:    (1).管道        数据只能由一个进程流向另一个进程(其中一个读管道,一个写管道),如果要建立全双工通信,需要建立两个管道        只能用于 ...

  6. c#多层嵌套Json

    Newtonsoft.Json.Net20.dll 下载请访问http://files.cnblogs.com/hualei/Newtonsoft.Json.Net20.rar 在.net 2.0中提 ...

  7. JVM学习---JAVA内存

    一.JAVA运行时数据区域:JAVA中的运行时内存区域有的随着虚拟机进程的启动而存在,有的区域则是依赖用户线程的启动和结束而建立和销毁的.包括以下的几个区域. 图. JAVA虚拟机运行时数据区 1.程 ...

  8. linux命令详解之useradd命令

    useradd命令使用方法,还包括用户账号的添加.删除与修改.用户口令的管理.用户组的管理方法. Linux系统是一个多用户多任务的分时操作系统,任何一个要使用系统资源的用户,都必须首先向系统管理员申 ...

  9. php威盾解密的例子分享

    例子,批量解密  代码如下 复制代码 <?php/************************************威盾PHP加密专家解密算法 By:zhrt*http://www.111 ...

  10. 动画气泡指示当前滑动值--第三方开源--DiscreteSeekbar

    DiscreteSeekbar在github上的项目主页是:https://github.com/AnderWeb/discreteSeekBar DiscreteSeekbar可以自定制的属性很多, ...