【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 ...
随机推荐
- OJ 1188 全排列---康托展开
题目描述 求n的从小到大第m个全排列(n≤20). 输入 n和m 输出 输出第m个全排列,两个数之间有一空格. 样例输入 3 2 样例输出 1 3 2 #include<cstdio> # ...
- Linux 的shell 字符串截取很有用。有八种方法。
一 Linux 的字符串截取很有用.有八种方法. 假设有变量 var=http://www.linuxidc.com/123.htm 1 # 号截取,删除左边字符,保留右边字符. echo ${va ...
- Dan计划:重新定义人生的10000个小时
一. 1985年,芝加哥大学的Benjamin Bloom教授,出版了一本重要著作<如何培养天才>(Developing Talent in Young People). 他研究的是,如何 ...
- Sphinx 增量索引更新
是基于PHP API调用,而不是基于sphinxSE.现在看来sphinxSE比API调用更简单的多,因为之前没有想过sphinxSE,现在先把API的弄明白.涉及到的:sphinx 数据源的设置,简 ...
- jQuery 效果函数
jQuery 效果函数 方法 描述 animate() 对被选元素应用“自定义”的动画 clearQueue() 对被选元素移除所有排队的函数(仍未运行的) delay() 对被选元素的所有排队函数( ...
- C++复数四则运算的实现
程序主要实现复数的加减乘,数乘,取共轭功能. 将所有函数都定义为了成员函数. 使用库函数atof将字符串转换为浮点型数据. 函数主要难点在于处理输入.由于需要判断输入是选择退出还是继续,所以用字符串来 ...
- PYTHON实现HTTP摘要认证(DIGEST AUTHENTICATION)
参考: http://blog.csdn.net/kiwi_coder/article/details/28677651 http://blog.csdn.net/gl1987807/article/ ...
- 2013 ACM/ICPC 长春网络赛F题
题意:两个人轮流说数字,第一个人可以说区间[1~k]中的一个,之后每次每人都可以说一个比前一个人所说数字大一点的数字,相邻两次数字只差在区间[1~k].谁先>=N,谁输.问最后是第一个人赢还是第 ...
- ARM 处理器的几个相关术语
生产ARM的厂商很多,自然ARM处理器的名字就五花八门.但是,它们有些共同点,那就是:架构和核心. 架构这个概念太宽不太懂,一般不同的架构会有不同的指令集,在不同的架构下面还可以有多种核心.核心就是指 ...
- PHP数组的使用方法小结
数组就是一组数据的集合,把一系列数据组织起来,形成一个可操作的整体.数组的每个实体都包含两项:键和值. 一.什么是数组数组就是一组数据的集合,把一系列数据组织起来,形成一个可操作的整体.数组的每个实体 ...