CF19D 線段樹+STL各種應用
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
7 add 1 1 add 3 4 find 0 0 remove 1 1 find 0 0 add 1 1 find 0 0
1 1 3 4 1 1
13 add 5 5 add 5 6 add 5 7 add 6 5 add 6 6 add 6 7 add 7 5 add 7 6 add 7 7 find 6 6 remove 7 7 find 6 6 find 4 4
7 7 -1 5 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;
}
相關文章
- 線段樹差分及其應用
- 線~段~樹
- 線段樹
- 線段樹模板
- 線段樹--RMQMQ
- 01 線段樹
- 線段樹 hate it
- 【模版】線段樹
- ut.cpp 最大線段並減線段交 [線段樹]
- 利用抓包工具從Windows 10 應用商店裡下載各種離線應用Windows
- 權值線段樹
- 線段樹筆記筆記
- Segment Tree(線段樹)
- 線段樹入門
- 李超線段樹
- 線段樹進階
- 洛谷題單指南-線段樹-P3373 【模板】線段樹 2
- 線段樹擴充套件套件
- 第二課——線段樹
- 線段樹簡單思路
- 深入理解線段樹
- 線段樹(毒瘤)總結
- POJ 3667 Hotel 線段樹
- poj 2667 hotel 線段樹
- 線段樹(超詳解)
- 線段樹 transformation——hdu 4578ORM
- 懶標記線段樹
- 可持久化線段樹持久化
- 線段樹模板總結
- 線段樹 - 多組圖帶你從頭到尾徹底理解線段樹
- 資料結構之樹( 線段樹,字典樹)資料結構
- 棧和佇列實際應用對迴文數字 各種樹的學習佇列
- hdu 1698 線段樹 一段更新染色
- 軟體開發各個階段用到的各種圖
- 線段樹也能是 Trie 樹 題解
- 線段樹從零開始
- 動態開點線段樹
- [18/03/24] 線段樹模板
- 資料結構-線段樹資料結構