【codevs1690】开关灯 线段树
这道题需要支持区间修改和区间询问,因此采用线段树加以维护。
由于求的是开着的灯的数目,因此维护sum:区间[ l , r ]中开着的灯的数目。
tag取做0/1,表示区间是否反转,在进行标记下传时,如果tag=1,则下传,否则返回。(tag的选取一般跟操作有关)
代码如下:
#include <bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
int n,m;
struct node{
#define lson t[k].lc
#define rson t[k].rc
int lc,rc,sum,tag;
}t[maxn<<1];
int tot=1;
inline void pushup(int k){t[k].sum=t[lson].sum+t[rson].sum;}
inline void pushdown(int k,int l,int r){
int mid=l+r>>1;
if(t[k].tag==0)return;
t[lson].sum=mid-l+1-t[lson].sum,t[lson].tag^=1;
t[rson].sum=r-mid-t[rson].sum,t[rson].tag^=1;
t[k].tag=0;
}
void build(int k,int l,int r){
if(l==r)return;
int mid=l+r>>1;
lson=++tot,build(lson,l,mid);
rson=++tot,build(rson,mid+1,r);
}
void modify(int k,int l,int r,int x,int y){
if(l==x&&r==y){t[k].sum=r-l+1-t[k].sum,t[k].tag^=1;return;}
int mid=l+r>>1;
pushdown(k,l,r);
if(y<=mid)modify(lson,l,mid,x,y);
else if(x>mid)modify(rson,mid+1,r,x,y);
else modify(lson,l,mid,x,mid),modify(rson,mid+1,r,mid+1,y);
pushup(k);
}
int query(int k,int l,int r,int x,int y){
if(l==x&&r==y)return t[k].sum;
int mid=l+r>>1;
pushdown(k,l,r);
if(y<=mid)return query(lson,l,mid,x,y);
else if(x>mid)return query(rson,mid+1,r,x,y);
else return query(lson,l,mid,x,mid)+query(rson,mid+1,r,mid+1,y);
}
void read_and_parse(){
scanf("%d%d",&n,&m);
build(1,1,n);
}
void solve(){
int opt,x,y;
while(m--){
scanf("%d%d%d",&opt,&x,&y);
if(opt==0)modify(1,1,n,x,y);
else printf("%d\n",query(1,1,n,x,y));
}
}
int main(){
read_and_parse();
solve();
return 0;
}
【codevs1690】开关灯 线段树的更多相关文章
- codevs1690 开关灯(线段树)
1690 开关灯 USACO 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description YYX家门前的街上有N(2< ...
- BZOJ 1230: [Usaco2008 Nov]lites 开关灯( 线段树 )
线段树.. --------------------------------------------------------------------------------- #include< ...
- codevs 1690 开关灯 线段树+延迟标记
1690 开关灯 时间限制: 1 s 空间限制: 128000 KB 题目描述 Description YYX家门前的街上有N(2<=N<=100000)盏路灯,在晚上六点之前,这 ...
- codevs 1690 开关灯 线段树区间更新 区间查询Lazy
题目描述 Description YYX家门前的街上有N(2<=N<=100000)盏路灯,在晚上六点之前,这些路灯全是关着的,六点之后,会有M(2<=m<=100000)个人 ...
- B1230 [Usaco2008 Nov]lites 开关灯 线段树
就是线段树维护异或和.之前我线段树区间修改down都是修改当前区间,结果debug出不来,改成每次向下了. 题干: Description Farmer John尝试通过和奶牛们玩益智玩具来保持他的奶 ...
- bzoj1230 开关灯 线段树
好久没写线段树了..被一道线段树裸题卡了一个上午 对区间进行异或,查询的时候直接用区间长度减去原有是数量就是改变完的数量 #include<bits/stdc++.h> using nam ...
- BZOJ 1230 Usaco2008 Nov 开关灯 线段树
思路: 用线段树模拟题中的操作就好 (标记异或 长度=区间总长度-当前已开灯的长度) //By SiriusRen #include <cstdio> using namespace st ...
- 【codevs1690】开关灯 线段树 区间修改+区间求和(标记)
[codevs1690]开关灯 2014年2月15日4930 题目描述 Description YYX家门前的街上有N(2<=N<=100000)盏路灯,在晚上六点之前,这些路灯全是关着的 ...
- codevs 1690 开关灯 线段树水题
没什么好说的,标记put表示开关是否开着. #include<cstdio> #include<cstring> #include<algorithm> using ...
随机推荐
- Linux进程信号
信号 名称 描述 1 HUP 挂起 2 INT 中断 3 QUIT 结束运行 9 KILL 无条件终止 11 SEGV 段错误 15 TERM 尽可能终止 17 STOP 无条件停止运行,但不终止 1 ...
- pickle.dump()和pickle.load()
python的pickle模块实现了基本的数据序列和反序列化. 通过pickle模块的序列化操作我们能够将程序中运行的对象信息保存到文件中去,永久存储: 通过pickle模块的反序列化操作,我们能够从 ...
- Asp.NetCore应用--部署到 ubuntu 进行托管
准备过程 netcore linux发布包(本人是通过vs2017发布) ubuntu 16.0.4虚机 进行托管 ubuntu netcore发布文件路径 服务器设置为将对 http://< ...
- kafka安装使用配置1.1
官方文档 rz上传到/usr/local/下 解压 tar xzvf 文件 改名 mv 文件 名字 环境变量 vi /etc/profile export KAFKA_HOME=/usr/local/ ...
- 解决Eclipse发布到Tomcat丢失依赖jar包的问题
解决Eclipse发布到Tomcat丢失依赖jar包的问题 如果jar文件是以外部依赖的形式导入的.Eclipse将web项目发布到Tomcat时,是不会自动发布这些依赖的. 可以通过Eclipse在 ...
- FFmpeg4.0笔记:封装ffmpeg的解码功能类CDecode
Github https://github.com/gongluck/FFmpeg4.0-study/tree/master/Cff CDecode.h /********************** ...
- layui2.5 修改layuicms
雷哥layui2.5版本学习 学习地址: https://www.bilibili.com/video/av59813890/?p=30 注意: 修改layuicms时注意下面是缓存的js, < ...
- vscode 插件 配置
第一页 第二页 第三页 settings.json配置 { "editor.fontSize": 20, "files.autoSave": "off ...
- win10子系统Ubuntu重置
重置: 在win10命令行下执行: lxrun /uninstall /full 安装: win+R打开bash 执行命令: lxrun /install /y
- 解决 mysql (10038)
1.授权 mysql>grant all privileges on *.* to 'root'@'%' identified by 'youpassword' with grant o ...