Codeforce 19D Points 线段树+离散化
来源:互联网 发布:刺马真相知乎 编辑:程序博客网 时间:2024/05/16 09:46
题意描述:有三种操作“add x y”往平面上添加(x,y)这个点,"remove x y",将平面上已经存在的点(x,y)删除,“find x y”找出平面上坐标严格大于(x,y)的点,如果有多个点找x最小的,再找y最小的。
分析;将x离散化后,记录每个x对应的最大y,以及x对应的所有y。线段树附加值为[l,r]内最大的y.找到满足条件的x,y即可。一开始不知道remove时怎么处理x对应的所有的y,还以为自己全想错了,然后看了题解知道set就可以做到了。 注意离散化处理的时候的sort,erase,size
code://居然有60+test。。。
#include<iostream>#include<cstdio>#include<cstdlib>#include<map>#include<set>#include<algorithm>#include<vector>using namespace std;//const int maxh=1e9; //一开始想要记录每个x对应的最小y 后来发现不对const int maxn=2e5+10;int max(int a,int b){if(a>b)return a;return b;}#define LL(o) (o<<1)#define RR(o) (o<<1|1)set<int> h[maxn];bool cmp(int a,int b){ return a<b;}struct Node{int l,r;int maxh;};struct Point{int x,y;};vector<int> num; //离散化这块必须要用 需熟练掌握Node node[maxn*4];map<int,int> p; void build(int o,int l,int r){ node[o].l=l; node[o].r=r; node[o].maxh=-1; if(l!=r){ int mid=l+(r-l)/2; build(LL(o),l,mid); build(RR(o),mid+1,r); }}void update(int p,int v,int o,int type){ if(type==1){//插入 int l=node[o].l,r=node[o].r; int mid=l+(r-l)/2; node[o].maxh=max(node[o].maxh,v); if(l==r) h[l].insert(v); else{ if(p<=mid)update(p,v,LL(o),type); else update(p,v,RR(o),type); } } if(type==0){ int l=node[o].l,r=node[o].r; int mid=l+(r-l)/2; if(l==r){ h[l].erase(v);// 一开始写的优先队列== 后来发现不能只能加不能去掉。。 if(!h[l].empty())node[o].maxh=*(--h[l].end()); else node[o].maxh=-1; } else{if(p<=mid) update(p,v,LL(o),type); else update(p,v,RR(o),type); node[o].maxh=max(node[LL(o)].maxh,node[RR(o)].maxh); } }}Point querry(int wid,int height,int o){ int l=node[o].l,r=node[o].r; Point ans; if(node[o].maxh<=height||r<=wid){ ans.x=-1;ans.y=-1;return ans;} if(l==r){ ans.x=num[l],ans.y=*(h[l].lower_bound(height+1)); return ans;}//set的lower_bound() ans=querry(wid,height,LL(o)); if (ans.x!=-1)return ans;//一开始没写这个。全部-1-1-1-1 else return querry(wid,height,RR(o));}int ww[maxn],height[maxn];char s[maxn][15];int main(){int n;scanf("%d",&n);for(int i=0;i<n;i++){ scanf("%s%d%d",s[i],&ww[i],&height[i]); num.push_back(ww[i]);}//离散化 sort(num.begin(),num.end()); num.erase(unique(num.begin(),num.end()),num.end()); int len=(int)num.size()-1;for(int i=0;i<=len;i++)h[i].clear();build(1,0,len);for(int i=0;i<=len;i++)p[num[i]]=i;for(int i=0;i<n;i++){ if(s[i][0]=='a') update(p[ww[i]],height[i],1,1); if(s[i][0]=='r') update(p[ww[i]],height[i],1,0); Point ans; if(s[i][0]=='f') { ans=querry(p[ww[i]],height[i],1);//一开始写成了num[ww[i]]。。。醉。。 if(ans.x!=-1)printf("%d %d\n",ans.x,ans.y); else printf("-1\n"); }}return 0;}
0 0
- Codeforce 19D Points 线段树+离散化
- Codeforces Beta Round #19 D. Points 线段树+离散化离散化
- CodeForces 19D Points(离散化+线段树+单点更新)
- CodeForces 19D Points(线段树单点修改+离散化)
- Codeforces 19D. Points ( 非递归线段树套SBT + 离散化 )
- CodeforcesBeta Round #19 D. Points 离线线段树 单点更新 离散化
- Codeforces 19D(线段树+离散化)
- CF 19D Points(线段树)
- Codeforces 19D Points 线段树+set
- CF 19D Points(线段树+set)
- [CodeforcesBeta Round #19 D. Points] (线段树)
- Points - CodeForces 19 D 线段树
- 【CodeForces】19D Points 线段树+set
- CodeforcesBeta Round #19 D. Points (线段树)
- CodeForces 19D Points (线段树+set)
- [线段树+离散化]LightOJ 1089 - Points in Segments (II)
- CodeForces 19D 离散化+线段树+set
- coderforces round 19D线段树+离散化处理
- R网络爬虫之批量下载
- Linux 下用 dd 命令生成一个指定大小的虚拟块设备文件
- JAVA中的String三兄弟
- 教单身程序猿如何new一个对象
- POJ 3675 Telescope
- Codeforce 19D Points 线段树+离散化
- 在两台电脑之间通过网线、局域网快速传东西的几种方法
- 抓交通肇事犯
- 如果你也面试01-C语言中static的作用
- 【Maven】Maven Plugin示例:自己动手编写Maven插件
- Android 报“Change to android.widget.ImageView, Fix Build Path, Edit XML”的错误
- 如何去除字符串两端的空格
- Java构造方法
- Java基础——网络编程(上)之UDP