题目描述 Description

在一条数轴上有N个点,分别是1~N。一开始所有的点都被染成黑色。接着
我们进行M次操作,第i次操作将[Li,Ri]这些点染成白色。请输出每个操作执行后
剩余黑色点的个数。

输入描述 Input Description

输入一行为N和M。下面M行每行两个数Li、Ri

输出描述 Output Description

输出M行,为每次操作后剩余黑色点的个数。

样例输入 Sample Input

10 3
3 3
5 7
2 8

样例输出 Sample Output

9
6
3

数据范围及提示 Data Size & Hint

数据限制
对30%的数据有1<=N<=2000,1<=M<=2000
对100%数据有1<=Li<=Ri<=N<=200000,1<=M<=200000

思路:把区间的值改成0,求区间和;
#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 数轴染色 区间更新加延迟标记的更多相关文章

  1. Codevs 1191 数轴染色

    1191 数轴染色 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 在一条数轴上有N个点,分别是1-N.一开始所有的点都被染成黑色. ...

  2. codevs 1191 线段树 区间更新(水)

    题目描述 Description 在一条数轴上有N个点,分别是1-N.一开始所有的点都被染成黑色.接着我们进行M次操作,第i次操作将[Li,Ri]这些点染成白色.请输出每个操作执行后剩余黑色点的个数. ...

  3. hdu 1698 线段数的区间更新 以及延迟更新

    先说说区间更新和单点更新的区别 主要的区别是搜索的过程 前者需要确定一个区间 后者就是一个点就好了 贴上两者代码 void updata(int i)//单点更新 { int l=stu[i].l; ...

  4. poj 3468 A Simple Problem with Integers 线段树加延迟标记

    A Simple Problem with Integers   Description You have N integers, A1, A2, ... , AN. You need to deal ...

  5. POJ3468 线段树(区间更新,区间求和,延迟标记)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 97196   ...

  6. 【wikioi】1191 数轴染色(线段树+水题)

    http://wikioi.com/problem/1191/ 太水的线段树了,敲了10分钟就敲完了,但是听说还有一种并查集的做法?不明觉厉. #include <cstdio> #inc ...

  7. codevs 1299 线段树 区间更新查询

    1299 切水果  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 大师 Master 题解  查看运行结果     题目描述 Description 简单的说,一共N个水果排成 ...

  8. HDU 1698 Just a Hook 线段树区间更新、

    来谈谈自己对延迟标记(lazy标记)的理解吧. lazy标记的主要作用是尽可能的降低时间复杂度. 这样说吧. 如果你不用lazy标记,那么你对于一个区间更新的话是要对其所有的子区间都更新一次,但如果用 ...

  9. Gym 101911E "Painting the Fence"(线段树区间更新+双端队列)

    传送门 题意: 庭院中有 n 个围栏,每个围栏上都被涂上了不同的颜色(数字表示): 有 m 条指令,每条指令给出一个整数 x ,你要做的就是将区间[ x第一次出现的位置 , x最后出现的位置 ]中的围 ...

随机推荐

  1. PHP + Ajax处理大数据查询并导出Excel

    思路:使用ajax多次请求服务器,分段生成多个Excel,然后打包压缩成zip,超链接指向下载的文件然后下载. [HTML部分] <input type="button" v ...

  2. [py][mx]django城市-教学机构-教师模型设计

    分析下城市-教学机构-教师模型设计 CourseOrg 课程信息 Teacher 教师信息 CityDict 城市信息 代码 from datetime import datetime from dj ...

  3. oj1500(Message Flood)字典树

    大意:输入几个字符串,然后再输入几个字符串,看第一次输入的字符串有多少没有在后面的字符串中出现(后输入的字符串不一定出现在之前的字符串中) #include <stdio.h> #incl ...

  4. 【Cocos2dx 3.x Lua】CCParallaxNode使用

    我们知道,当我们移动时,我们会看到离我们越近的物体,会移动的越快,越远的物体,比如远处的山会移动的很慢,而最远处的物体,比如太阳几乎不动,这个现象叫视差,而在游戏中模仿视差,可以让玩家感觉到游戏中的角 ...

  5. isKindOfClass isMemeberOfClass 的区分

    isKindOfClass If you use such constructs in your code, you might think it is alright to modify an ob ...

  6. Testbench结构篇

    对于standalone的block的verification: 采用结构化的Testbench:Testcase与Harness,BFM分别分离,来提高系统的可重用性.如图是一个典型结构: 其中所有 ...

  7. 网站建设中常用的JS代码段落

    1.屏蔽左右键 这个不介绍了. <script language="JavaScript"> document.oncontextmenu=new Function(& ...

  8. C++矩阵库 Eigen 快速入门

    最近需要用 C++ 做一些数值计算,之前一直采用Matlab 混合编程的方式处理矩阵运算,非常麻烦,直到发现了 Eigen 库,简直相见恨晚,好用哭了. Eigen 是一个基于C++模板的线性代数库, ...

  9. Python: os.listdir()

    os.listdir(): 返回一个列表,此列表包含参数路径下的的文件名或文件夹名.这个列表以字母为顺序. eg: >>>import os >>>dirs=os. ...

  10. 数据仓库原理<3>:数据仓库与ODS

    1. 引言 本篇主要讲述操作数据存储(ODS)系统产生的背景.定义.特点,以及它与数据仓库的区别. 在前两篇,笔者介绍了什么是数据仓库?为什么需要数据仓库?数据仓库系统的体系结构是什么?因此可能在读者 ...