POJ 3225 线段树区间更新(两种更新方式)
http://blog.csdn.net/niuox/article/details/9664487
这道题明显是线段树,根据题意可以知道:
(用0和1表示是否包含区间,-1表示该区间内既有包含又有不包含)
U:把区间[l,r]覆盖成1
I:把[-∞,l)(r,∞]覆盖成0
D:把区间[l,r]覆盖成0
C:把[-∞,l)(r,∞]覆盖成0 , 且[l,r]区间0/1互换
S:[l,r]区间0/1互换
这里有两个地方和区间更新不一样
一个是会更新选中区间以外的所有区域,可以这样子解决:
void update(char val,int L,int R,int l,int r,int rt)
{
...
int mid = (l + r)>>;
if (L <= mid)
{
update(val,L,R,l,mid,rt<<);
}
else if(val == 'I' || val == 'C')
{
a[rt<<] = col[rt<<] = ;
}
if(R > mid)
{
update(val,L,R,mid+,r,rt<<|);
}
else if(val == 'I' || val == 'C')
{
a[rt<<|] = col[rt<<|] = ;
}
}
if判断左边是否小于mid,如果不是,那么更新它的左边就是我们想要的选中区域外了,右边同理。
第二个是有两种更新方式,覆盖和异或
这种问题一般需要两种标记,但这里其中一种是覆盖,所以一种标记可以用值自身表示,-1表示杂,非-1表示纯。一个标记数组就行。
然后还要理清两种标记使用的方式
很明显我们可以知道这个性质:
当一个区间被覆盖后,不管之前有没有异或标记都没有意义了
所以当一个节点得到覆盖标记时把异或标记清空
第三种是开闭区间,这里可以通过把所有数乘2处理(偶数表示端点,奇数表示两端点间的区间)。
#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <cctype>
#include <vector>
#include <iterator>
#include <set>
#include <map>
#include <sstream>
using namespace std; #define mem(a,b) memset(a,b,sizeof(a))
#define pf printf
#define sf scanf
#define spf sprintf
#define pb push_back
#define debug printf("!\n")
#define MAXN 65535*2
#define MAX(a,b) a>b?a:b
#define blank pf("\n")
#define LL long long
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define pqueue priority_queue
#define INF 0x3f3f3f3f int n,m; int a[MAXN<<],col[MAXN<<],ans; bool vis[MAXN]; void FXOR(int x)
{
if(a[x]!=-) a[x]^=;
else col[x]^=;
} void PushDown(int rt)
{
if(a[rt] != -)
{
a[rt<<] = a[rt<<|] = a[rt];
col[rt<<] = col[rt<<|] = ;
a[rt] = -;
}
if(col[rt])
{
FXOR(rt<<);
FXOR(rt<<|);
col[rt] = ;
}
} void update(char val,int L,int R,int l,int r,int rt)
{
if(L <= l && r <= R)
{
if(val == 'U')
{
a[rt] = ;
col[rt] = ;
}
else if(val == 'D')
{
a[rt] = col[rt] = ;
}
else if(val == 'C' || val == 'S')
{
FXOR(rt);
}
return;
}
PushDown(rt);
int mid = (l + r)>>;
if (L <= mid)
{
update(val,L,R,l,mid,rt<<);
}
else if(val == 'I' || val == 'C')
{
a[rt<<] = col[rt<<] = ;
}
if(R > mid)
{
update(val,L,R,mid+,r,rt<<|);
}
else if(val == 'I' || val == 'C')
{
a[rt<<|] = col[rt<<|] = ;
}
} void query(int l,int r,int rt)
{
if(a[rt] == )
{
for(int i = l;i<=r;i++) vis[i] = true;
return;
}
else if(a[rt] == ) return;
if(l == r) return;
PushDown(rt);
int mid = (l + r)>>;
query(l,mid,rt<<);
query(mid+,r,rt<<|);
} int main()
{
int r,t;
char op,lchar,rchar;
a[] = col[] = ;
while(~sf("%c %c%d,%d%c\n",&op,&lchar,&r,&t,&rchar))
{
r<<=;
t<<=;
if(lchar == '(') r++;
if(rchar == ')') t--;
update(op,r,t,,MAXN,);
}
query(,MAXN,);
int s = -,e;
bool flag = false;
for(int i=;i<=MAXN;i++)
{
if(vis[i])
{
if(s == -) s = i;
e = i;
}
else
{
if(s!=-)
{
if(flag) pf(" ");
pf("%c%d,%d%c",s&?'(':'[',s>>,(e+)>>,e&?')':']');
s = -;
flag = true;
}
}
}
if(!flag) printf("empty set");
return ; }
POJ 3225 线段树区间更新(两种更新方式)的更多相关文章
- POJ 3225 (线段树 区间更新) Help with Intervals
这道题搞了好久,其实坑点挺多.. 网上找了许多题解,发现思路其实都差不多,所以就不在重复了. 推荐一篇比较好的题解,请戳这. 另外,如果因为可能要更新多次,但最终查询只需要一次,所以没有写pushup ...
- HUD.2795 Billboard ( 线段树 区间最值 单点更新 单点查询 建树技巧)
HUD.2795 Billboard ( 线段树 区间最值 单点更新 单点查询 建树技巧) 题意分析 题目大意:一个h*w的公告牌,要在其上贴公告. 输入的是1*wi的w值,这些是公告的尺寸. 贴公告 ...
- POJ 3667 线段树区间合并
http://www.cnblogs.com/scau20110726/archive/2013/05/07/3065418.html 用线段树,首先要定义好线段树的节点信息,一般看到一个问题,很难很 ...
- poj 2528 线段树区间修改+离散化
Mayor's posters POJ 2528 传送门 线段树区间修改加离散化 #include <cstdio> #include <iostream> #include ...
- Mayor's posters POJ - 2528 线段树区间覆盖
//线段树区间覆盖 #include<cstdio> #include<cstring> #include<iostream> #include<algori ...
- 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 3667 线段树区间合并裸题
题意:给一个n和m,表示n个房间,m次操作,操作类型有2种,一种把求连续未租出的房间数有d个的最小的最左边的房间号,另一个操作时把从x到x+d-1的房间号收回. 建立线段树,值为1表示未租出,0为租出 ...
随机推荐
- 题解 P1434 【滑雪】
题目链接 此题运用功能强大的 ~~暴力搜索~~ 记忆化搜索才是重点!!! 然而,这是一道经典的DP问题 如果我们用$dis[i][j]$来表示坐标为$(i,j)$时的高度 $cnt[i][j]$ 是我 ...
- win8.1 开启企业模式
1,Win+R Gpedit.msc 2, 3, 4,F12 多了一项企业模式
- 将form转为ajax提交的js代码
参考网络代码基础上进行修改,调试通过. 在html中插入下面的代码: 函数ajaxSubmit是submit的ajax形式. 注意:这里面使用到了jquery库 //<!--将form中的值转换 ...
- 关于使用self.title文字不居中的解决办法
最放发现,使用Segue在对视图切换,左上角的一般都是<Back 的一个Button控键或者是上一个视图的<title .因为上一个视图的title名字太长,导致当前视图的title被挤压 ...
- COCO2018 目标检测
刚浏览了一下coco数据集官网,认真看了一下18年的目标检测任务,简单记录一下. coco2018目标检测挑战赛只进行实例分割的评比,虽然仍然可以输出bbox,但是不可以提交到比赛的服务器,原因是官方 ...
- 007 Android 单击事件、toast使用
第一种按钮点击事件(最常用): button=findViewById(R.id.button); button2=findViewById(R.id.button2); button.setOnCl ...
- GCD(最大公约数)和LCM(最小公倍数)的求法
GCD(最大公约数) (1)辗转相除法(欧几里得算法)(常用) 将两个数a, b相除,如果余数c不等于0,就把b的值给a,c的值给b,直到c等于0,此时最大公约数就是b (2)更相减损术 将两个书中较 ...
- python之函数(一)
python有很多内置函数,可以直接调用.比如type(), len(), range(),max(), min(), filter().内置函数我们就先不多提,我们主要介绍自定义函数. 1. 函数的 ...
- IIS环境下上传文件失败
跟随学习代码练习 php 上传文件,一开始是点击按钮后没有反应,不知道是否成功,使用 var_dump($_FILES) 查看,发现空空如也.遂百度一下,发现基本代码应如下 <form acti ...
- poj1862
一.题意:两个物体m1.m2相撞后会变成一个物体,这个物体的重量会变成2*sqrt(m1*m2).有n个物体,假设只会发生两两相撞,求最后剩下的最小重量. 二.思路:简单的贪心.越大的数开越多的次方, ...