【wikioi】1191 数轴染色(线段树+水题)
http://wikioi.com/problem/1191/
太水的线段树了,敲了10分钟就敲完了,但是听说还有一种并查集的做法?不明觉厉。
#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) {
if(add[x]) {
s[lc]=0; s[rc]=0;
add[lc]=1; add[rc]=1;
add[x]=0;
}
}
void build(const int &l, const int &r, const int &x) {
if(l==r) { s[x]=1; return; }
int m=MID;
build(lson); build(rson);
pushup(x);
}
void update(const int &l, const int &r, const int &x) {
if(L<=l && r<=R) { add[x]=1; s[x]=0; return; }
pushdown(x);
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);
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();
build(1, n, 1);
while(m--) {
read(L); read(R);
update(1, n, 1);
L=1, R=n;
printf("%d\n", query(1, n, 1));
}
return 0;
}
题目描述 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
【wikioi】1191 数轴染色(线段树+水题)的更多相关文章
- POJ 3468 A Simple Problem with Integers(线段树水题)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 135904 ...
- hdu 1754 I Hate It(线段树水题)
>>点击进入原题测试<< 思路:线段树水题,可以手敲 #include<string> #include<iostream> #include<a ...
- codeforces 339C Xenia and Bit Operations(线段树水题)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Xenia and Bit Operations Xenia the beginn ...
- P1198 最大数 线段树水题
这道题模拟一下可以过,但是我们发现线段树也可以安全水过...... 写的线段树只需要滋磁单点修改,区间求max即可 我一开始犯了一个很SB的错误:每次插入修改了t,然后疯狂爆0到怀疑人生... 而且我 ...
- 【codevs1191】数轴染色 线段树 区间修改+固定区间查询
[codevs1191]数轴染色 2014年2月15日4317 题目描述 Description 在一条数轴上有N个点,分别是1-N.一开始所有的点都被染成黑色.接着我们进行M次操作,第i次操作将[L ...
- hdu - 1394 Minimum Inversion Number(线段树水题)
http://acm.hdu.edu.cn/showproblem.php?pid=1394 很基础的线段树. 先查询在更新,如果后面的数比前面的数小肯定会查询到前面已经更新过的值,这时候返回的sum ...
- codevs 1690 开关灯 线段树水题
没什么好说的,标记put表示开关是否开着. #include<cstdio> #include<cstring> #include<algorithm> using ...
- [ACM_数据结构] Color the ball [线段树水题][数组开大]
Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气球b依次 ...
- hdu 1754 线段树 水题 单点更新 区间查询
I Hate It Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
随机推荐
- Linux rsync 命令详解
服务器之间常常要保持些文件或目录的一致,比如一些大的软件下载网站,它们通常使用多台服务器来提供下载服务.当一台服务器上的文件更新后,其它的服务器 也需要更新,而且 在更新的时候应该是只对新增或是修改过 ...
- PHP面向对象三大特点学习(充分理解抽象、封装、继承、多态)
PHP面向对象三大特点学习 学习目标:充分理解抽象.封装.继承.多态 面象对向的三大特点:封装性.继承性.多态性 首先简单理解一下抽象:我们在前面定义一个类的时候,实际上就是把一类事物共有的属性和 ...
- neutron 中 flat vlan gre vxlan的区别
In a flat network, everyone shares the same network segment. For example, say 2 tenants are sharing ...
- 深入了解PooledConnectionFactory CachingConnectionFactory Sin
深入理解PooledConnectionFactory CachingConnectionFactory SingleConnectionFactory PooledConnectionFactory ...
- mybatis处理集合、循环、数组和in查询等语句的使用
在Mybatis的xml配置中使用集合,主要是用到了foreach动态语句. foreach的参数: foreach元素的属性主要有 item,index,collection,open,separa ...
- HTML控件ID和NAME属性及在CS页面获得.ASPX页面中HTML控件的值
<转载>来自网络 一.ID是在客户端脚本里用!NAME是用于获取提交表单的某表单域信息,在form里面,如果不指定Name的话,就不会发送到服务器端,所以有name属性的控件,必须指定na ...
- dom 中事件
阻止表单提交: function aa(){ return false; } function bb(event){ event.preventDefault(); } 事件不再派发: <!DO ...
- 21.左旋转字符串[LeftRotateString]
[题目] 定义字符串的左旋转操作:把字符串前面的若干个字符移动到字符串的尾部.如把字符串AB1234 左旋转2位得到字符串1234AB.请实现字符串左旋转的函数.要求时间对长度为n的字符串操作的复杂度 ...
- windows2003批量添加和导出所有ip
批量添加IP 在cmd命令行下运行: FOR /L %i IN (130,1,190) DO netsh interface ip add address "本地连接" 192.1 ...
- WPF 将PPT,Word转成图片
在Office下,PowerPoint可以直接把每张幻灯片转成图片,而Word不能直接保存图片.所以只能通过先转换成xps文件,然后再转成图片. 一.PPT 保存为图片 /// <summary ...