CF19D 线段树+STL各种应用
来源:互联网 发布:对大数据的认识与评价 编辑:程序博客网 时间:2024/06/12 19:54
http://codeforces.com/problemset/problem/19/D
Description
Pete and Bob invented a new interesting game. Bob takes a sheet of paper and locates a Cartesian coordinate system on it as follows: point (0, 0) is located in the bottom-left corner, Ox axis is directed right, Oy axis is directed up. Pete gives Bob requests of three types:
- add x y — on the sheet of paper Bob marks a point with coordinates (x, y). For each request of this type it's guaranteed that point(x, y) is not yet marked on Bob's sheet at the time of the request.
- remove x y — on the sheet of paper Bob erases the previously marked point with coordinates (x, y). For each request of this type it's guaranteed that point (x, y) is already marked on Bob's sheet at the time of the request.
- find x y — on the sheet of paper Bob finds all the marked points, lying strictly above and strictly to the right of point (x, y). Among these points Bob chooses the leftmost one, if it is not unique, he chooses the bottommost one, and gives its coordinates to Pete.
Bob managed to answer the requests, when they were 10, 100 or 1000, but when their amount grew up to 2·105, Bob failed to cope. Now he needs a program that will answer all Pete's requests. Help Bob, please!
Input
The first input line contains number n (1 ≤ n ≤ 2·105) — amount of requests. Then there follow n lines — descriptions of the requests.add x y describes the request to add a point, remove x y — the request to erase a point, find x y — the request to find the bottom-left point. All the coordinates in the input file are non-negative and don't exceed 109.
Output
For each request of type find x y output in a separate line the answer to it — coordinates of the bottommost among the leftmost marked points, lying strictly above and to the right of point (x, y). If there are no points strictly above and to the right of point (x, y), output -1.
Sample Input
7add 1 1add 3 4find 0 0remove 1 1find 0 0add 1 1find 0 0
1 13 41 1
13add 5 5add 5 6add 5 7add 6 5add 6 6add 6 7add 7 5add 7 6add 7 7find 6 6remove 7 7find 6 6find 4 4
7 7-15 5
/**CF 19D 线段树+STL题目大意:在平面直角坐标系的第一象限里进行一系列的点的删除,添加,查询操作。查询是返回(x,y)有上方最靠左最靠下的点的坐标,若不存在返回-1解题思路:主要有利用线段树的单点更新和单点查询,对于所有x横坐标相同的点放入一个set中,确定了x之后直接upper_bound就是对应点的纵坐标*/#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <set>using namespace std;const int maxn=200005;struct note{ int mark,x,y;} e[maxn];int n,num[maxn];char a[10];set <int>s[maxn];struct node{ int l,r; int max_y;} tree[4*maxn];void push_up(int root){ tree[root].max_y=max(tree[root<<1].max_y,tree[root<<1|1].max_y);}void build(int l,int r,int root){ tree[root].l=l; tree[root].r=r; tree[root].max_y=-1; if(l==r) return; int mid=(l+r)>>1; build(l,mid,root<<1); build(mid+1,r,root<<1|1);}void update(int x,int root){ if(x==tree[root].l&&x==tree[root].r) { if(s[x].size()) tree[root].max_y=*(--s[x].end()); else tree[root].max_y=-1; return; } int mid=(tree[root].l+tree[root].r)>>1; if(x<=mid) update(x,root<<1); else update(x,root<<1|1); push_up(root);}int query( int x,int y,int root ){ if(tree[root].r<=x) return -1; if(tree[root].max_y<=y) return -1; if(tree[root].l==tree[root].r) return tree[root].l; int t=query(x,y,root<<1); if(t==-1) t=query(x,y,root<<1|1); return t;}int main(){ while(~scanf("%d",&n)) { for(int i=1; i<=n; i++) { scanf("%s%d%d",a,&e[i].x,&e[i].y); if(a[0]=='a') e[i].mark=1; else if(a[0]=='r') e[i].mark=2; else e[i].mark=3; s[i].clear(); num[i]=e[i].x; } sort(num+1,num+1+n); int m = unique(num+1 , num+1+n)-(num+1);///去重,返回最后一个有效点的下一个迭代器的地址 build(1,m,1); for(int i=1; i<=n; i++) { int x=upper_bound(num+1,num+1+m,e[i].x)-(num+1); int y=e[i].y; if(e[i].mark==1) { s[x].insert(y); update(x,1); } else if(e[i].mark==2) { s[x].erase(y); update(x,1); } else { int ans=query(x,y,1); if(ans==-1) puts("-1"); else { printf("%d %d\n",num[ans],*s[ans].upper_bound(y)); } } } } return 0;}
- CF19D 线段树+STL各种应用
- 线段树应用:线段覆盖
- 线段树 各种模板(详解)
- 线段树的各种板子
- 线段树的各种类型
- 线段树应用
- 线段树的应用
- 线段树及其应用
- 线段树应用
- bzoj2957(线段树应用)
- 线段树应用
- 线段树的应用
- Codeforecs 69E. Subsegments STL/线段树
- URAL 1542. Autocompletion (线段树+STL)
- hdu 4417 Super Mario(线段树+STL)
- poj2777-线段树应用(线段覆盖)
- 各种树的应用
- hdu 2871 线段树(各种操作)
- Java之美[从菜鸟到高手演练]之JDK动态代理的实现及原理
- 【数据结构】哈夫曼树实现编码译码
- BindingNavigator控件
- Tslib和Qt 4.8.4与在开发板上的移植
- 如何将PPT输出为高精度的图片
- CF19D 线段树+STL各种应用
- Android Eclipse安装插件 No repository found containing问题
- php上下箭头排序
- BindingSource组件
- 对C++中指针运算的认识
- 异步内存、直接复制及流处理复制对比
- excel向程序发送命令时出现问题
- unity小项目主角打击怪物
- 这是第一篇的标题