http://wikioi.com/problem/1690/

这题可不能算是水题了。。

在线段树中,我只想到了lazy改变,但是没想到lazy变后size怎么变,我的策略变成了lazy为0时size也为0.。这显然不科学啊。

根据加加减减,s=A-s 的方法正好可以表示开关灯剩余的数量。

我没想到啊!!!!!!

还好,,,做到了一题这种题,如果考场上出我做错,那么天杀的啊!!!

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } #define lc x<<1
#define rc x<<1|1
#define MID (l+r)>>1
#define lson l, m, lc
#define rson m+1, r, rc
const int N=200005;
int s[N*10], L, R, n;
bool add[N*10];
inline void pushup(const int &x) { s[x]=s[lc]+s[rc]; }
inline void pushdown(const int &x, const int &m) {
if(add[x]) {
add[lc]^=1; add[rc]^=1;
s[lc]=(m-(m>>1))-s[lc];
s[rc]=(m>>1)-s[rc];
add[x]=0;
}
}
void update(const int &l, const int &r, const int &x) {
if(L<=l && r<=R) { add[x]^=1; s[x]=r-l+1-s[x]; return; }
pushdown(x, r-l+1);
int m=MID;
if(L<=m) update(lson); if(m<R) update(rson);
pushup(x);
}
int query(const int &l, const int &r, const int &x) {
if(L<=l && r<=R) return s[x];
pushdown(x, r-l+1);
int m=MID, ret=0;
if(L<=m) ret+=query(lson); if(m<R) ret+=query(rson);
pushup(x);
return ret;
}
int main() {
read(n);
int m=getint(), cs;
while(m--) {
read(cs); read(L); read(R);
if(!cs) update(1, n, 1);
else printf("%d\n", query(1, n, 1));
}
return 0;
}

题目描述 Description

YYX家门前的街上有N(2<=N<=100000)盏路灯,在晚上六点之前,这些路灯全是关着的,六点之后,会有M(2<=m& lt;=100000)个人陆续按下开关,这些开关可以改变从第i盏灯到第j盏灯的状态,现在YYX想知道,从第x盏灯到第y盏灯中有多少是亮着的(1& lt;=i,j,x,y<=N)

输入描述 Input Description

第 1 行: 用空格隔开的两个整数N和M
第 2..M+1 行: 每行表示一个操作, 有三个用空格分开的整数: 指令号(0代表按下开关,1代表询问状态), x 和 y 

输出描述 Output Description

第 1..询问总次数 行:对于每一次询问,输出询问的结果

样例输入 Sample Input

4 5
0 1 2
0 2 4
1 2 3
0 2 4
1 1 4

样例输出
Sample Output

1
2
 

数据范围及提示
Data Size & Hint

一共4盏灯,5个操作,下面是每次操作的状态(X代表关上的,O代表开着的):

XXXX -> OOXX -> OXOO -> 询问1~3 -> OOXX -> 询问1~4

【wikioi】1690 开关灯(线段树)的更多相关文章

  1. codevs 1690 开关灯 线段树+延迟标记

    1690 开关灯  时间限制: 1 s  空间限制: 128000 KB   题目描述 Description YYX家门前的街上有N(2<=N<=100000)盏路灯,在晚上六点之前,这 ...

  2. codevs 1690 开关灯 线段树区间更新 区间查询Lazy

    题目描述 Description YYX家门前的街上有N(2<=N<=100000)盏路灯,在晚上六点之前,这些路灯全是关着的,六点之后,会有M(2<=m<=100000)个人 ...

  3. codevs 1690 开关灯 线段树水题

    没什么好说的,标记put表示开关是否开着. #include<cstdio> #include<cstring> #include<algorithm> using ...

  4. BZOJ 1230: [Usaco2008 Nov]lites 开关灯( 线段树 )

    线段树.. --------------------------------------------------------------------------------- #include< ...

  5. codevs1690 开关灯(线段树)

    1690 开关灯 USACO  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond     题目描述 Description YYX家门前的街上有N(2< ...

  6. B1230 [Usaco2008 Nov]lites 开关灯 线段树

    就是线段树维护异或和.之前我线段树区间修改down都是修改当前区间,结果debug出不来,改成每次向下了. 题干: Description Farmer John尝试通过和奶牛们玩益智玩具来保持他的奶 ...

  7. bzoj1230 开关灯 线段树

    好久没写线段树了..被一道线段树裸题卡了一个上午 对区间进行异或,查询的时候直接用区间长度减去原有是数量就是改变完的数量 #include<bits/stdc++.h> using nam ...

  8. BZOJ 1230 Usaco2008 Nov 开关灯 线段树

    思路: 用线段树模拟题中的操作就好 (标记异或 长度=区间总长度-当前已开灯的长度) //By SiriusRen #include <cstdio> using namespace st ...

  9. 【codevs1690】开关灯 线段树

    这道题需要支持区间修改和区间询问,因此采用线段树加以维护. 由于求的是开着的灯的数目,因此维护sum:区间[ l , r ]中开着的灯的数目. tag取做0/1,表示区间是否反转,在进行标记下传时,如 ...

  10. 【codevs1690】开关灯 线段树 区间修改+区间求和(标记)

    [codevs1690]开关灯 2014年2月15日4930 题目描述 Description YYX家门前的街上有N(2<=N<=100000)盏路灯,在晚上六点之前,这些路灯全是关着的 ...

随机推荐

  1. Python Django 的 django templatedoesnotexist

    django 1.8版本的解决方案 在  setting.py 这个文件里 TEMPLATES = [ ...... #原来的 #'DIRS': [ ], //  这个 列表里添加 template路 ...

  2. Linux 查看系统用户的登录日志

    查看用户登录系统的日志   有两类日志记录用户登录的行为,一是记录登录者的数据,一个是记录用户的登录时间   一,记录用户登录数据         /var/log/wtmp日志文件记录用户登录的数据 ...

  3. HDOJ 1870

    #include<stdio.h> #include<stack> #include<string.h> #include<iostream> usin ...

  4. excel中如何批量将所有的网址设为超链接

    首先如果数据较少的话,只需要双击鼠标左键,回车,就会自动转换成超链接. 转自: http://zhidao.baidu.com/question/200363361.html?qbl=relate_q ...

  5. Django对静态文件的处理——部署阶段

    参考:http://blog.makto.me/post/2012-11-09/static-files-in-django-deployment HTML模板中的用法: {% load static ...

  6. zpf 视图

    2014年8月19日 18:12:16 smarty使用了2年, 使用PHP本身做模版引擎也有4个多月了, 最终还是在我的这个框架中抛弃了smarty,转用原生的PHP代码做模版引擎,并简单写了一个视 ...

  7. GLSL的qualifier

    uniform:从应用程序到vertex shader 到fragment shader都能使用,但是值一直不变: varying:从vertex shader到fragment shader,在fr ...

  8. Andoird自定义ViewGroup实现竖向引导界面

    一般进入APP都有欢迎界面,基本都是水平滚动的,今天和大家分享一个垂直滚动的例子. 先来看看效果把: 首先是布局文件: <com.example.verticallinearlayout.Ver ...

  9. Perl中的正则表达式

    转自:http://c20031776.blog.163.com/blog/static/684716252013624383887/ Perl 程序中,正则表达式有三种存在形式 分别是 (1 模式匹 ...

  10. ***CI中的数据库操作(insert_id新增后返回记录ID)

    在system/application/config 文件夹和里面的config文件里已经配置了参数 $active_group = "default";$db['default' ...