CodeForces 19D Points
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 题解:
线段树维护
#include <bits/stdc++.h> using namespace std;
const int maxn = 2e5 + ;
typedef pair < int ,int > dii;
struct operation
{
int x , y , tp ;
}op[maxn];
char str[];
int c,n,QueryY; vector < int > vi;
set < int > :: iterator it , it2; struct Segmenttree
{
typedef int SgTreeDataType;
struct treenode
{
int L , R ;
SgTreeDataType maxy;
set < int > s;
void updata(SgTreeDataType v){
s.insert(v);
maxy=max(maxy,v);
}
void remove(SgTreeDataType v){
s.erase(v);
if(s.empty()) maxy=;
else{
it=s.end();it--;
maxy=*it;
}
}
}; treenode tree[maxn * ]; inline void push_up(int o){
int lson = o << , rson = o << | ;
tree[o].maxy=max(tree[lson].maxy,tree[rson].maxy);
} void build(int L , int R , int o){
tree[o].L = L , tree[o].R = R,tree[o].maxy=;tree[o].s.empty();
if (R > L){
int mid = (L+R) >> ;
build(L,mid,o<<);
build(mid+,R,o<<|);
}
} void updata(int p,SgTreeDataType v,int o){
int L = tree[o].L , R = tree[o].R;
if (L==R) tree[o].updata(v);
else{
int mid = (L+R)>>;
if (p <= mid) updata(p,v,o<<);
else updata(p,v,o<<|);
push_up(o);
}
} void remove(int p,SgTreeDataType v,int o){
int L = tree[o].L , R = tree[o].R;
if (L==R) tree[o].remove(v);
else{
int mid = (L+R)>>;
if (p <= mid) remove(p,v,o<<);
else remove(p,v,o<<|);
push_up(o);
}
} dii query(int p,int o){
int L = tree[o].L , R = tree[o].R;
if (L>=p){
int lson = o << , rson = o << | ;
if(tree[o].maxy > QueryY){
if(L==R){
it = tree[o].s.upper_bound(QueryY);
return make_pair( L , *it);
}else{
if(tree[lson].maxy > QueryY) return query(p , lson);
else if(tree[rson].maxy > QueryY) return query(p , rson);
}
}else return make_pair(-,-);
}
else{
int mid = (L+R)>>;
dii res(-,-);
if (p <= mid) res = query(p,o<<);
if(~res.first) return res;
return query(p,o<<|);
}
}
}Sgtree; inline int GetRank(int x){
return lower_bound( vi.begin() , vi.begin() + c , x ) - vi.begin();
} int main(int argc,char *argv[]){
scanf("%d",&n);
for(int i = ; i <= n ; ++ i){
int x , y;
scanf("%s%d%d",str,&op[i].x,&op[i].y);
if(str[]=='a') op[i].tp = ;
else if(str[]=='r') op[i].tp = ;
else op[i].tp = ;
vi.push_back(op[i].x);
}
sort( vi.begin() , vi.end() );
c = unique( vi.begin() , vi.end() ) - vi.begin();
Sgtree.build( , c - , );
for(int i = ; i <= n ; ++ i){
op[i].x = GetRank( op[i].x );
if(op[i].tp==) Sgtree.updata(op[i].x,op[i].y,);
else if(op[i].tp==) Sgtree.remove(op[i].x,op[i].y,);
else{
if(op[i].x == c - ){
printf("-1\n");
continue;
}
QueryY = op[i].y;
dii ans = Sgtree.query(op[i].x + , );
if(ans.first==-) printf("-1\n");
else printf("%d %d\n",vi[ans.first],ans.second);
}
}
return ;
}
CodeForces 19D Points的更多相关文章
- CodeForces 19D Points (线段树+set)
D. Points time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...
- 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) ...
- codeforces 19D D. Points 树套树
D. Points Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/19/problem/D De ...
- Codeforces Beta Round #19D(Points)线段树
D. Points time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...
- codeforces 872E. Points, Lines and Ready-made Titles
http://codeforces.com/contest/872/problem/E E. Points, Lines and Ready-made Titles time limit per te ...
- codeforces 251A Points on Line(二分or单调队列)
Description Little Petya likes points a lot. Recently his mom has presented him n points lying on th ...
- CodeForces 577E Points on Plane(莫队思维题)
题目描述 On a plane are nn points ( x_{i}xi , y_{i}yi ) with integer coordinates between 00 and 10^{6} ...
- 『ACM C++』 Codeforces | 1066A - Points in Segments
大一生活真 特么 ”丰富多彩“ ,多彩到我要忙到哭泣,身为班长,很多班级的事情需要管理,也是,什么东西都得体验学一学,从学生会主席.团委团总支.社团社长都体验过一番了,现在差个班长也没试过,就来体验了 ...
随机推荐
- [置顶] 宏途_LCD调试流程.
今天在调试宏途的LCD屏时,开始是开机屏幕不亮,背光都不亮,可能板子已经损坏,一般通过测试电流电压简单验证,(注:硬件引脚没焊好也会引起读lcd id出现错误!!!)出现这个问题一般是因为引脚没焊好, ...
- brent ozar的sqlserver dba训练课程翻译——第二章:手动恢复数据库
备份的唯一原因 备份的唯一原因是我们可以还原 当我第一次成为sqlserver数据库管理员,只要备份工作都能成功运行,我就会觉得一切都很好.我会查看sqlserver代理,保证那些作业都在运行,然 ...
- SYNATXAHIGHLIGHTER IN WLW HAS PROBLEMS
System.Reflection.TargetInvocationException: 调用的目标发生了异常. ---> System.ArgumentException: 字体“Consol ...
- 如何用SVN进行个人版本管理
事实上SVN的确是我用过的最好的源码管理工具,虽然我用过的这类工具并不多,只有VSS.CVS和SVN,其它像PVCS. TeamSource.ClearCase之类的只有耳闻,因为它们都是商业产品,并 ...
- EditText设置可以编辑和不可编辑状态
1.首先想到在xml中设置android:editable="false",但是如果想在代码中动态设置可编辑状态,没有找到对应的函数 2.然后尝试使用editText.setFoc ...
- Swift学习——A Swift Tour 函数
Functions and Closures 函数和封闭性(闭包) Functions 函数的使用 Swift中的函数定义和OC中有明显的差别了,使用func定义函数,在括号里定义參数和类型,用 ...
- Java JDBC连接SQL Server2005错误:通过port 1433 连接到主机 localhost 的 TCP/IP 连接失败
错误原因例如以下: Exception in thread "main" org.hibernate.exception.JDBCConnectionException: Cann ...
- SUSE 在Intel举行"Rule The Stack"的竞赛中获得 "Openstack安装最高速"奖
有关"Rule The Stack": https://communities.intel.com/community/itpeernetwork/datastack/blog/2 ...
- mysql报错"ERROR 1206 (HY000): The total number of locks exceeds the lock table size"的解决方法
1. 问题背景 InnoDB是新版MySQL(v5.5及以后)默认的存储引擎,之前版本的默认引擎为MyISAM,因此,低于5.5版本的mysql配置文件.my.cnf中,关于InnoD ...
- 常用分组函数count-avg-sum-max-min
分组函数也称多行函数,用于对一组数据进行运算,针对一组数据(取自于多行记录的相同字段)只返回一个结果,例如计算公司全体员工的工资总和.最高工资.最低工资.各部门的员工平均工资(按部门分组)等.由于分组 ...