codeforces Beta Round #19 D. Point (线段树 + set)
来源:互联网 发布:qq群推广淘宝店 编辑:程序博客网 时间:2024/05/18 01:32
题目大意:
对平面上的点进行操作。
add x y 在 (x,y )上加一个点。
remove x y 移除 (x,y)上的点。
find x y 求出在(x,y)右上角离他最近的点,优先级是靠左,靠下。
思路分析:
find 操作 比较麻烦。
要保证x大的同时还要确保x最小,而且该x上还要有点。
这样要找大的时候要小的,就是在线段树上选择性的进入左子树还是右子树。
所以核心就是,用set维护叶子节点。
然后查找的时候去叶子节点找,如果这个叶子节点有蛮子的 x y 就输出,否则回溯去另外一个子树。
#include <cstdio>#include <iostream>#include <cstring>#include <algorithm>#include <set>#define lson num<<1,s,mid#define rson num<<1|1,mid+1,e#define maxn 400005using namespace std;set <int> cq[maxn];int cnt[maxn<<2];int mmax[maxn<<2];struct node{ char type[10]; int x,y;} save[maxn];int x[maxn<<1];void Insert(int num,int s,int e,int pos,int val,bool flg){ if(s==e) { if(flg) { cnt[num]++; cq[s].insert(val); mmax[num]=max(mmax[num],*(--cq[s].end())); } else { cnt[num]--; cq[s].erase(val); if(!cq[s].empty())mmax[num]=max(mmax[num],*(--cq[s].end())); else mmax[num]=0; } return; } int mid=(s+e)>>1; if(pos<=mid)Insert(lson,pos,val,flg); else Insert(rson,pos,val,flg); cnt[num]=cnt[num<<1]+cnt[num<<1|1]; mmax[num]=max(mmax[num<<1],mmax[num<<1|1]);}int ansx,ansy;bool query(int num,int s,int e,int l,int r,int pos,int val){ int mid=(s+e)>>1; if(l<=s && r>=e) { if(s==e) { set<int>::iterator it = cq[s].upper_bound(val); if(it==cq[s].end()) { return false; } else { ansx=s; ansy=*it; return true; } } else { if(mmax[num<<1]>val) { if(query(lson,l,r,pos,val))return true; } if(mmax[num<<1|1]>val) { if(query(rson,l,r,pos,val))return true; } return false; } } if(r<=mid) { if(query(lson,l,r,pos,val))return true; } else if(l>mid) { if(query(rson,l,r,pos,val))return true; } else { if(query(lson,l,mid,pos,val))return true; if(query(rson,mid+1,r,pos,val))return true; } return false;}int main(){ int n; memset(cnt,0,sizeof cnt); memset(mmax,0,sizeof mmax); scanf("%d",&n); int top=1; for(int i=1; i<=n; i++) { scanf("%s%d%d",save[i].type,&save[i].x,&save[i].y); x[top++]=save[i].x; x[top++]=save[i].y; } sort(x+1,x+top); int m = unique(x+1,x+top)-x; for(int i=1; i<=n; i++) { if(save[i].type[0]=='a') { int l=lower_bound(x+1,x+m,save[i].x)-x; Insert(1,1,m,l,save[i].y,1); } else if(save[i].type[0]=='r') { int l=lower_bound(x+1,x+m,save[i].x)-x; Insert(1,1,m,l,save[i].y,0); } else { int l=upper_bound(x+1,x+m,save[i].x)-x; if(query(1,1,m,l,m,l,save[i].y))printf("%d %d\n",x[ansx],ansy); else printf("-1\n"); } } return 0;}
0 0
- codeforces Beta Round #19 D. Point (线段树 + set)
- Codeforces Beta Round #19 D线段树
- Codeforces Beta Round #19D 线段树
- Codeforces Beta Round #12 D. Ball (线段树)
- Codeforces Beta Round #19 D. Points 线段树+离散化离散化
- Codeforces 19D Points 线段树+set
- 【CodeForces】19D Points 线段树+set
- codeforces 19D 线段树+set
- CodeForces 19D Points (线段树+set)
- Codeforces Beta Round #19 D. Points
- Codeforces Beta Round #19 D Points
- Codeforces Beta Round #19 D. Points
- Codeforces Beta Round #19D. Points
- Codeforces Beta Round #43 (ACM-ICPC Rules), problem: (D) Parking Lot 线段树 区间合并
- Codeforces Beta Round #51 D
- Codeforces Round #209 (Div. 2)-C. Prime Number(set)和D. Pair of Numbers(线段树)
- Codeforces Round #310 (Div. 1)B,C,D(set+线段树)
- Codeforces 19 D. Points(线段树&set好题)
- 50道Java习题之12
- 主动使用触发的初始化操作
- [iOS]iOS AudioSession详解 Category选择 听筒扬声器切换
- POJ1002解题报告
- 谈谈云计算虚拟化中lxc的安装
- codeforces Beta Round #19 D. Point (线段树 + set)
- 正则验证
- qt 处理文字解析异常(本地)
- 使用SQL Developer将SQL Server 2005数据库移植到Oracle 11g操作文档
- spring3.x 与 quartz2.x (JobStoreTX cluster)集群
- Ios-坐标系统(详解UIView的frame、bounds跟center属性[图])
- 为NGUI panel 添加 sorting layer
- MFC sqlserver等操作
- SSL握手过程