POJ 3225 (线段树 区间更新) Help with Intervals
这道题搞了好久,其实坑点挺多。。
网上找了许多题解,发现思路其实都差不多,所以就不在重复了。
推荐一篇比较好的题解,请戳这。
另外,如果因为可能要更新多次,但最终查询只需要一次,所以没有写pushup函数,仅有一个pushdown。
#include <cstdio> const int maxn = ;
//const int maxn = 10;
int qL, qR, op;
int setv[maxn << ], xorv[maxn << ];
bool in[maxn + ]; void pushdown(int o)
{
int lc = o*, rc = o*+;
if(setv[o] >= )
{
setv[lc] = setv[rc] = setv[o];
setv[o] = -;
xorv[lc] = xorv[rc] = ;
}
if(xorv[o])
{
if(setv[lc] >= ) setv[lc] ^= ; else xorv[lc] ^= ;
if(setv[rc] >= ) setv[rc] ^= ; else xorv[rc] ^= ;
xorv[o] = ;
}
} void update(int o, int L, int R, int qL, int qR)
{
if(qL <= L && qR >= R)
{
if(op == ) { setv[o] = xorv[o] = ; }
else if(op == ) { setv[o] = ; xorv[o] = ; }
else if(op == ) { if(setv[o] >= ) setv[o] ^= ; else xorv[o] ^= ; }
return;
}
int M = (L + R) / ;
pushdown(o);
if(qL <= M) update(o*, L, M, qL, qR);
if(qR > M) update(o*+, M+, R, qL, qR);
} void query(int o, int L, int R)
{
if(setv[o] >= )
{
if(setv[o] == ) for(int i = L; i <= R; i++) in[i] = true;
return;
}
pushdown(o);
if(L == R) return;
int M = (L + R) / ;
query(o*, L, M);
query(o*+, M+, R);
} char cmd, _left, _right; int main()
{
//freopen("in.txt", "r", stdin); while(scanf("%c %c%d,%d%c\n", &cmd, &_left, &qL, &qR, &_right) >= )
{
qL <<= ; qR <<= ;
if(_left == '(') qL++; if(_right == ')') qR--;
if(qL > qR && (cmd == 'I' || cmd == 'C')) { setv[] = ; xorv[] = ; continue; }
if(cmd == 'U') { op = ; update(, , maxn, qL, qR); }
else if(cmd == 'I')
{
op = ;
if(qL - >= ) update(, , maxn, , qL-);
if(qR + <= maxn) update(, , maxn, qR+, maxn);
}
else if(cmd == 'D') { op = ; update(, , maxn, qL, qR); }
else if(cmd == 'C')
{
op = ;
if(qL - >= ) update(, , maxn, , qL-);
if(qR + <= maxn) update(, , maxn, qR+, maxn);
op = ;
update(, , maxn, qL, qR);
}
else if(cmd == 'S') { op = ; update(, , maxn, qL, qR); }
}
query(, , maxn);
bool print = false;
int s = -, e;
for(int i = ; i <= maxn; i++)
{
if(in[i])
{
if(s == -) s = i;
e = i;
}
else if(s != -)
{
if(print) printf(" ");
print = true;
printf("%c%d,%d%c", s&?'(':'[', s>>, (e+)>>, e&?')':']');
s = -;
}
} if(!print) puts("empty set"); return ;
}
代码君
POJ 3225 (线段树 区间更新) Help with Intervals的更多相关文章
- POJ 3225 线段树区间更新(两种更新方式)
http://blog.csdn.net/niuox/article/details/9664487 这道题明显是线段树,根据题意可以知道: (用0和1表示是否包含区间,-1表示该区间内既有包含又有不 ...
- poj 3468 线段树区间更新/查询
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...
- hdu 1698+poj 3468 (线段树 区间更新)
http://acm.hdu.edu.cn/showproblem.php?pid=1698 这个题意翻译起来有点猥琐啊,还是和谐一点吧 和涂颜色差不多,区间初始都为1,然后操作都是将x到y改为z,注 ...
- poj 2777 线段树 区间更新+位运算
题意:有一个长板子,分成多段,有两种操作,第一种是C给从a到b那段染一种颜色c,另一种是P询问a到b有多少种不同的颜色.Sample Input2 2 4 板长 颜色数目 询问数目C 1 1 2P ...
- POJ 2528 Mayor's posters (线段树区间更新+离散化)
题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值 ...
- POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)
POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...
- POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)
POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...
- POJ 2528 Mayor's posters 【区间离散化+线段树区间更新&&查询变形】
任意门:http://poj.org/problem?id=2528 Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total S ...
- 【POJ 2777】 Count Color(线段树区间更新与查询)
[POJ 2777] Count Color(线段树区间更新与查询) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4094 ...
随机推荐
- winform中的时间轴控件
我现在做的项目遇到一个需求,就是有没有类似的控件: 我要实现的功能是:播放录像. 某个时间段内假如有2个录像,这个坐标表示的是时间,假如我现在拖动时间轴,拖到第一个录像里面开始播放第一个录像,拖到2个 ...
- 图解JavaScript执行环境结构
JavaScript引擎在开始编译代码的时候,会对JavaScript代码进行一次预编译,生成一个执行环境,比如如下代码: window.onload=function(){ function sub ...
- poj 3469
Dual Core CPU Time Limit: 15000MS Memory Limit: 131072K Total Submissions: 18120 Accepted: 7818 ...
- iOS-CALayer图片淡入淡出动画
]; } - (.f; CABasicAnimation *boundsAnimation = [CABasicAnimation animationWithKeyPath:, , ...
- POJ 2039
#include<iostream> #include<stdio.h> #include<string> #define MAXN 20 using namesp ...
- **Apache Options指令详解
http://www.365mini.com/page/apache-options-directive.htm Options指令是Apache配置文件中一个比较常见也比较重要的指令,Options ...
- lintcode:二叉树的所有路径
二叉树的所有路径 给一棵二叉树,找出从根节点到叶子节点的所有路径. 样例 给出下面这棵二叉树: 1 / \ 2 3 \ 5 所有根到叶子的路径为: [ "1->2->5" ...
- key-value数据库
http://blog.csdn.net/byane/article/details/6928519 传统的文件系统中,需要维护目录的层次结构,使用dentry,inode,directory等复杂结 ...
- Item2 + zsh
转自 http://11ten.gitcafe.io/book-a/iTerm2/index.html iTerm2的主要特点: 开源免费. 兼容性比默认Terminal更好.对于经常要远程使用的情况 ...
- [iOS问题归总]SourceTree+osChina版本管理出现的问题
1.commit失败(1) 解决办法: 把爆红的路径的文件删除掉, 重新commit 2. push时候, 有100MB限制 解决办法: Git只允许上传最大100MB的文件,如果超过,则会被serv ...