codevs 1191 数轴染色 区间更新加延迟标记
在一条数轴上有N个点,分别是1~N。一开始所有的点都被染成黑色。接着
我们进行M次操作,第i次操作将[Li,Ri]这些点染成白色。请输出每个操作执行后
剩余黑色点的个数。
输入一行为N和M。下面M行每行两个数Li、Ri
输出M行,为每次操作后剩余黑色点的个数。
10 3
3 3
5 7
2 8
9
6
3
数据限制
对30%的数据有1<=N<=2000,1<=M<=2000
对100%数据有1<=Li<=Ri<=N<=200000,1<=M<=200000
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
#define true ture
#define false flase
using namespace std;
#define ll long long
int scan()
{
int res = , ch ;
while( !( ( ch = getchar() ) >= '' && ch <= '' ) )
{
if( ch == EOF ) return << ;
}
res = ch - '' ;
while( ( ch = getchar() ) >= '' && ch <= '' )
res = res * + ( ch - '' ) ;
return res ;
}
struct is
{
int l,r;
int num;
int lazy;
}tree[*];
void build_tree(int l,int r,int pos)
{
tree[pos].l=l;
tree[pos].r=r;
tree[pos].lazy=-;
if(l==r)
{
tree[pos].num=;
//scanf("%lld",&tree[pos].num);
return;
}
int mid=(l+r)/;
build_tree(l,mid,pos*);
build_tree(mid+,r,pos*+);
tree[pos].num=tree[pos*].num+tree[pos*+].num;
}
void update(int l,int r,int change,int pos)
{
if(tree[pos].l==l&&tree[pos].r==r)
{
tree[pos].lazy=change;
tree[pos].num=;
return;
}
if(tree[pos].lazy==)
{
tree[pos*].num=(tree[pos*].r+-tree[pos*].l)*tree[pos].lazy;
tree[pos*+].num=(tree[pos*+].r+-tree[pos*+].l)*tree[pos].lazy;
tree[pos*].lazy=tree[pos].lazy;
tree[pos*+].lazy=tree[pos].lazy;
tree[pos].lazy=-;
}
int mid=(tree[pos].l+tree[pos].r)/;
if(r<=mid)
update(l,r,change,pos*);
else if(l>mid)
update(l,r,change,pos*+);
else
{
update(l,mid,change,pos*);
update(mid+,r,change,pos*+);
}
tree[pos].num=tree[pos*].num+tree[pos*+].num;
}
int query(int l,int r,int pos)
{
//cout<<l<<" "<<r<<" "<<pos<<endl;
if(tree[pos].l==l&&tree[pos].r==r)
return tree[pos].num;
if(tree[pos].lazy==)
{
tree[pos*].num=;
tree[pos*+].num=;
tree[pos*].lazy=;
tree[pos*+].lazy=;
tree[pos].lazy=-;
}
int mid=(tree[pos].l+tree[pos].r)/;
if(l>mid)
return query(l,r,pos*+);
else if(r<=mid)
return query(l,r,pos*);
else
return query(l,mid,pos*)+query(mid+,r,pos*+);
}
int main()
{
int x,q,i,t;
while(~scanf("%d",&x))
{
scanf("%d",&q);
build_tree(,x,);
while(q--)
{
int flag,change=,l,r;
scanf("%d%d",&l,&r);
update(l,r,change,);
printf("%d\n",query(,x,));
}
}
return ;
}
codevs 1191 数轴染色 区间更新加延迟标记的更多相关文章
- Codevs 1191 数轴染色
1191 数轴染色 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 在一条数轴上有N个点,分别是1-N.一开始所有的点都被染成黑色. ...
- codevs 1191 线段树 区间更新(水)
题目描述 Description 在一条数轴上有N个点,分别是1-N.一开始所有的点都被染成黑色.接着我们进行M次操作,第i次操作将[Li,Ri]这些点染成白色.请输出每个操作执行后剩余黑色点的个数. ...
- hdu 1698 线段数的区间更新 以及延迟更新
先说说区间更新和单点更新的区别 主要的区别是搜索的过程 前者需要确定一个区间 后者就是一个点就好了 贴上两者代码 void updata(int i)//单点更新 { int l=stu[i].l; ...
- poj 3468 A Simple Problem with Integers 线段树加延迟标记
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal ...
- POJ3468 线段树(区间更新,区间求和,延迟标记)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 97196 ...
- 【wikioi】1191 数轴染色(线段树+水题)
http://wikioi.com/problem/1191/ 太水的线段树了,敲了10分钟就敲完了,但是听说还有一种并查集的做法?不明觉厉. #include <cstdio> #inc ...
- codevs 1299 线段树 区间更新查询
1299 切水果 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 大师 Master 题解 查看运行结果 题目描述 Description 简单的说,一共N个水果排成 ...
- HDU 1698 Just a Hook 线段树区间更新、
来谈谈自己对延迟标记(lazy标记)的理解吧. lazy标记的主要作用是尽可能的降低时间复杂度. 这样说吧. 如果你不用lazy标记,那么你对于一个区间更新的话是要对其所有的子区间都更新一次,但如果用 ...
- Gym 101911E "Painting the Fence"(线段树区间更新+双端队列)
传送门 题意: 庭院中有 n 个围栏,每个围栏上都被涂上了不同的颜色(数字表示): 有 m 条指令,每条指令给出一个整数 x ,你要做的就是将区间[ x第一次出现的位置 , x最后出现的位置 ]中的围 ...
随机推荐
- 编译错误 ----- /usr/bin/ld: cannot find -lc
yum install glibc-static glib-static是Gcc链接时使用到的库.
- Kettle定时抽取两个库中的两个表到目标库SYS_OPLOG表
A库a表(红色为抽取字段): 关联用户表: B库b表(红色为抽取字段): 关联用户表 C目标库SYS_OPLOG表(c表) 利用kettle抽取A库a表(具体名称见上图),B库b表的上面红色框起来 ...
- Ajax学习整理笔记
AJAX技术的出现使得javascript技术大火.不懂AJAX的同学百度一下,了解AJAX能做什么就可以了. 代码: <!DOCTYPE html> <html> <h ...
- JaveScript-简介
1.JaveScript:脚本语言.(弱类型语言)可以写在head,也可以写在head里,同样可以写在html外面<script src=""></script& ...
- Impala SQL 语言元素(翻译)[转载]
原 Impala SQL 语言元素(翻译) 本文来源于http://my.oschina.net/weiqingbin/blog/189413#OSC_h2_2 摘要 http://www.cloud ...
- WireShark 基本介绍
文中内容主要转自:http://www.cnblogs.com/TankXiao/archive/2012/10/10/2711777.html 一.Wireshark 与 Fiddler 比较: F ...
- Windows10 64位下安装TensorFlow谷歌人工智能系统已官方原生支持
Windows10 64位下安装TensorFlow谷歌人工智能系统已官方原生支持 GitHub - tensorflow/tensorflow: Computation using data flo ...
- iOS之第三方库以及XCode插件介绍
前言 第三方库是现在的程序员离不开的东西 不光是APP开发 基本上所有的商业项目 都会或多或少的使用到第三方库 Github上Star>100的开源库数量如下 可以看到JS以绝对的优势排名第一 ...
- silverlight数据绑定
控件绑定 <Grid x:Name="LayoutRoot"> <StackPanel> <ScrollBar x:Name="bar&qu ...
- where T : class含义
.NET支持的类型参数约束有以下五种: where T : struct | T必须是一个结构类型where T : class ...