codeForce-19D Points (点更新+离散化)
题目大意:在二维坐标系的x正半轴,y正半轴和第一象限内,有三种操作:
1、add x,y (添加点<x,y>);
2、remove x,y(移除点<x,y>);
3、find x,y(查找在点<x,y>的绝对右上角的第一个点);
并且,只能移除已添加的点,一个点在移除之前不能重复添加。
题目分析:将横坐标离散化,线段树的每个叶子节点维护对应横坐标上的最大纵坐标。非叶子节点维护对应区间内的最大纵坐标。
代码如下:
# include<cstdio>
# include<algorithm>
# include<map>
# include<set>
# include<iostream>
using namespace std;
# define mid (l+(r-l)/2) const int N=200000; set<int>s[N+5];
char cd[N+5];
int a[N+5],b[N+5];
char str[7];
int x[N+5];
int tr[N*4+5]; void pushUp(int rt)
{
tr[rt]=max(tr[rt<<1],tr[rt<<1|1]);
} void build(int rt,int l,int r)
{
tr[rt]=-1;
if(l==r) return ;
build(rt<<1,l,mid);
build(rt<<1|1,mid+1,r);
} void update(int rt,int l,int r,int x)
{
if(l==r){
if(s[l].empty()) tr[rt]=-1;
else tr[rt]=*(--s[l].end());
}else{
if(x<=mid) update(rt<<1,l,mid,x);
else update(rt<<1|1,mid+1,r,x);
pushUp(rt);
}
} int query(int rt,int l,int r,int x,int val)
{
if(tr[rt]<=val) return -1;
if(l==r){
return l;
}else{
if(x<=mid){
int k=query(rt<<1,l,mid,x,val);
if(k!=-1) return k;
}
return query(rt<<1|1,mid+1,r,x,val);
}
} int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i=0;i<n;++i){
scanf("%s%d%d",str,a+i,b+i);
cd[i]=str[0];
x[i]=a[i];
}
sort(x,x+n);
int m=unique(x,x+n)-x;
for(int i=0;i<=m;++i)
s[i].clear(); for(int i=0;i<n;++i){
int pos=lower_bound(x,x+m,a[i])-x;
if(cd[i]=='a'){
s[pos].insert(b[i]);
update(1,0,m,pos);
}else if(cd[i]=='r'){
s[pos].erase(b[i]);
update(1,0,m,pos);
}else{
int l=query(1,0,m,pos+1,b[i]);
if(l==-1) printf("-1\n");
else{
///int r=*upper_bound(s[l].begin(),s[l].end(),b[i]);
int r=*s[l].upper_bound(b[i]); ///如果改成上面的就超时了。
printf("%d %d\n",x[l],r);
}
}
}
}
return 0;
}
codeForce-19D Points (点更新+离散化)的更多相关文章
- POJ-2528 Mayor's posters (线段树区间更新+离散化)
题目分析:线段树区间更新+离散化 代码如下: # include<iostream> # include<cstdio> # include<queue> # in ...
- CodeForces 19D Points(离散化+线段树+单点更新)
题目链接: huangjing 题意:给了三种操作 1:add(x,y)将这个点增加二维坐标系 2:remove(x,y)将这个点从二维坐标系移除. 3:find(x,y)就是找到在(x,y)右上方的 ...
- CodeForces 19D Points(线段树+map)
开始想不通,后来看网上说是set,就有一个想法是对每个x建一个set...然后又想直接建立两重的set就好,最后发现不行,自己想多了... 题意是给你三种操作:add (x y) 平面添加(x y) ...
- POJ 2528 Mayor's posters (线段树区间更新+离散化)
题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值 ...
- CF 19D - Points 线段树套平衡树
题目在这: 给出三种操作: 1.增加点(x,y) 2.删除点(x,y) 3.询问在点(x,y)右上方的点,如果有相同,输出最左边的,如果还有相同,输出最低的那个点 分析: 线段树套平衡树. 我们先离散 ...
- POJ2528 Mayor's posters(线段树&区间更新+离散化)题解
题意:给一个区间,表示这个区间贴了一张海报,后贴的会覆盖前面的,问最后能看到几张海报. 思路: 之前就不会离散化,先讲一下离散化:这里离散化的原理是:先把每个端点值都放到一个数组中并除重+排序,我们就 ...
- CodeForces 19D Points (线段树+set)
D. Points time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...
- POJ2528:Mayor's posters(线段树区间更新+离散化)
Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral electio ...
- ACM-线段树区间更新+离散化
区间更新与单点更新最大的不同就在于Lazy思想: http://blog.sina.com.cn/s/blog_a2dce6b30101l8bi.html 可以看这篇文章,讲得比较清楚 在具体使用上, ...
随机推荐
- sql遍历树
oracle有直接的sql来遍历一颗树的子节点和父节点 遍历一个节点的所有子节点(classid的值就是该节点的值) select * from organization_ a start with ...
- Visual studio 2013的安装和单元测试
vs2013安装过程: 简单的单元测试: 1.创建新的c#类库 2.创建单元测试 3.测试结果 单元测试结束
- SharePoint 2013 Nintex Workflow 工作流帮助(十二)
博客地址 http://blog.csdn.net/foxdave 工作流动作 31. Create task(User interaction分组,企业版才有) 该操作用于在Microsoft Ex ...
- 团队博客——Sprint计划会议1
每日Scrum:第一天 会议时间:4.14.晚八点半 会议地点:基础教学楼一楼大厅 小组成员:郭庆樑,林彦汝,张金 认领人—使团队成员分工合作,保持团队的积极性. ID 名称(NAME) 重要性(IM ...
- PAT 08-2 求矩阵的局部最大值
这题挺简单的,但,每日一篇.说两点:第一,我的粗心导致我这题花了大把的时间去找错误,看到4个测试用例对了三个,我以为是那块的边界条件没考虑到,又或者是存在隐蔽的逻辑或语法错误,通过与别人程序的反复对比 ...
- iOS开发之修改动画对象的元素属性
步骤:1.使用single view application创建新的项目 2.在.h文件中使用UIimageview创建两个图片实例对象,使用UIDynamicAnimator创建动画对象 3.在.m ...
- ubuntu下的wps不能使用中文.
首先如果wps不能用中文的话应该是 excell ppt word 都不能用 . 我的办法需要改三个文件 . 先后打开这三个文件 . xpower@xpower-CW65S:~$ sudo vim / ...
- BZOJ 1853 幸运数字
需要优化一波常数. 以及刚才那个版本是错的. #include<iostream> #include<cstdio> #include<cstring> #incl ...
- BZOJ 3165 Segment
同上题. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm&g ...
- PHP中的数组(一)
一.数组的概述 1.数组的本质:管理和操作一组变量 2.数组是复合类型 3.数组中可以存储任意长度的数据,也可以存储任意类型的数据 4.数组就可以完成其它语言数据结构的功能(链 ...