CodeForces 19D Points (线段树+set)
2 seconds
256 megabytes
standard input
standard output
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!
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.
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.
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 这道题目对时间卡的比较狠,超时了好多次 题目的意思是在二维坐标系上找一个比指定点大的点, 一维用线段树,在线段树上套set. 题目的数据是1e9,我们可以离散化,也可以用动态线段树 这里用动态线段树,不用离散化。 其次,一开始用线段树,节点上的信息是这个区间X坐标被标记的数目。 在查询的时候,可以查询第一个比指定点x大的x坐标,然后再在这个x坐标节点上的set里面找符合条件的y坐标 这样的效率是n的,相当于顺序的一个一个点找过去。 后来线段树维护的是这个区间所有被标记的x坐标对应的y坐标的最大值。 整体效率n*logn*logn#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <string.h>
#include <vector>
#include <queue>
#include <set> using namespace std;
typedef long long int LL;
const int maxn=2*1e5;
const int INF=0x7FFFFFFF;
int p;
int rt[maxn+5];
int ls[maxn*35+5];
int rs[maxn*35+5];
int sum[maxn*35+5];
set<int> a[maxn*35+5];
int n,m;
set<int>::iterator it;
int newnode()
{
ls[p]=rs[p]=0;
sum[p]=-1;
return p++;
}
int nn;
void pushup(int node)
{
if(!ls[node]&&rs[node])
sum[node]=sum[rs[node]];
else if(!rs[node]&&ls[node])
sum[node]=sum[ls[node]];
else if(ls[node]&&rs[node])
sum[node]=max(sum[ls[node]],sum[rs[node]]);
else
sum[node]=-1;
}
void update(int &node,int l,int r,int tag,int tag2,int flag)
{
if(!node) node=newnode();
if(l==r)
{
if(flag)
{ a[node].insert(tag2);
if(a[node].empty()) sum[node]=-1;
else
{
it=a[node].end();
it--;
sum[node]=*(it);
}
}
else
{
a[node].erase(tag2);
if(a[node].empty()) sum[node]=-1;
else
{
it=a[node].end();
it--;
sum[node]=*(it);
}
} return;
}
int mid=(l+r)>>1;
if(tag<=mid) update(ls[node],l,mid,tag,tag2,flag);
else update(rs[node],mid+1,r,tag,tag2,flag);
pushup(node);
}
int query(int node,int l,int r,int tag,int y)
{
if(!node) return -1;
if(y>=sum[node]) return -1; if(l==r)
{ nn=*a[node].upper_bound(y);
return l;
}
int ret;
int mid=(l+r)>>1;
if(tag>=mid) ret=query(rs[node],mid+1,r,tag,y);
else
{
ret=query(ls[node],l,mid,tag,y);
if(ret==-1)
ret=query(rs[node],mid+1,r,tag,y);
}
return ret;
}
int main()
{
scanf("%d",&n);
char c[10];
p=1;
int root=0;
int x,y;
int r=1e9;
for(int i=1;i<=n;i++)
{
scanf("%s",c);
scanf("%d%d",&x,&y);
if(c[0]=='a')
{
update(root,0,r,x,y,1);
}
else if(c[0]=='r')
{
update(root,0,r,x,y,0);
}
else if(c[0]=='f')
{
nn=INF;
int xx=query(root,0,r,x,y);
if(xx==-1||nn==INF)
{
printf("-1\n");
continue;
}
printf("%d %d\n",xx,nn);
}
}
return 0;
}
CodeForces 19D Points (线段树+set)的更多相关文章
- Codeforces Beta Round #19D(Points)线段树
D. Points time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...
- CF 19D - Points 线段树套平衡树
题目在这: 给出三种操作: 1.增加点(x,y) 2.删除点(x,y) 3.询问在点(x,y)右上方的点,如果有相同,输出最左边的,如果还有相同,输出最低的那个点 分析: 线段树套平衡树. 我们先离散 ...
- CodeForces 19D Points(线段树+map)
开始想不通,后来看网上说是set,就有一个想法是对每个x建一个set...然后又想直接建立两重的set就好,最后发现不行,自己想多了... 题意是给你三种操作:add (x y) 平面添加(x y) ...
- Codeforces 1140F Extending Set of Points 线段树 + 按秩合并并查集 (看题解)
Extending Set of Points 我们能发现, 如果把x轴y轴看成点, 那么答案就是在各个连通块里面的x轴的个数乘以y轴的个数之和. 然后就变成了一个并查集的问题, 但是这个题目里面有撤 ...
- Codeforces 1140F Extending Set of Points (线段树分治+并查集)
这题有以下几个步骤 1.离线处理出每个点的作用范围 2.根据线段树得出作用范围 3.根据分治把每个范围内的点记录和处理 #include<bits/stdc++.h> using name ...
- CodeForces 19D Points
Pete and Bob invented a new interesting game. Bob takes a sheet of paper and locates a Cartesian coo ...
- Vasya and a Tree CodeForces - 1076E(线段树+dfs)
I - Vasya and a Tree CodeForces - 1076E 其实参考完别人的思路,写完程序交上去,还是没理解啥意思..昨晚再仔细想了想.终于弄明白了(有可能不对 题意是有一棵树n个 ...
- Codeforces 787D. Legacy 线段树建模+最短路
D. Legacy time limit per test:2 seconds memory limit per test:256 megabytes input:standard input out ...
- [hdu4347]The Closest M Points(线段树形式kd-tree)
解题关键:kdtree模板题,距离某点最近的m个点. #include<cstdio> #include<cstring> #include<algorithm> ...
随机推荐
- 第三百零六节,Django框架,models.py模块,数据库操作——创建表、数据类型、索引、admin后台,补充Django目录说明以及全局配置文件配置
Django框架,models.py模块,数据库操作——创建表.数据类型.索引.admin后台,补充Django目录说明以及全局配置文件配置 数据库配置 django默认支持sqlite,mysql, ...
- javascript 哈夫曼树构造
function Node(data) { this.data = data; this.left = null; this.right = null; } Array.prototype.creat ...
- hadoop2.7.1单机和伪集群的搭建-0
内容中包含 base64string 图片造成字符过多,拒绝显示
- 学习 TList 类的实现[7]
总结目前 TMyList 已具备的功能(3 个方法.3 个属性): Add: 添加; Delete: 删除; Clear: 清空;Count: 元素总数;Capacity: 已存在的所有元素位置数;L ...
- DeDeCMS织梦的采集教程
http://www.tuicool.com/articles/VziaEz dede 第一步.我们打开织梦后台点击采集——采集节点管理——增加新节点 第二步.新增节点-配置网址索引 填写要采集 ...
- 【java】 java设计模式(3):单例模式(Singleton)
单例对象(Singleton)是一种常用的设计模式.在Java应用中,单例对象能保证在一个JVM中,该对象只有一个实例存在.这样的模式有几个好处: 1.某些类创建比较频繁,对于一些大型的对象,这是一笔 ...
- Makefile--隐含规则自动推dao(一)
[版权声明:转载请保留出处:周学伟:http://www.cnblogs.com/zxouxuewei/] 上一节的Makefile勉强可用,但还写的比较繁琐,不够简洁.对每一个.c源文件,都需要写一 ...
- Tomcat之JSP运行原理之小试牛刀
最近空闲看了下JSP/Servlet,以前只知道用JSP,但是对其运行原理知之甚少,今在此做些笔记,以备查阅. 首先简要描述下其运行过程,然后结合Tomcat源码作简要分析. JSP运行过程: 第一步 ...
- haffman哈夫曼编码的实现
<span style="font-size:18px;">/* 1.在一棵二叉树中,我们定义从A节点到B节点所经过的分支序列为从A节点到B节点的路径: 定义从A节点到 ...
- X-code 描述文件的位置
不管是真机测试还是打包的过程中,都需要描述文件.在桌面上,按快捷键“commd+Shift+G”,就会显示一个要填的文件路径,如下图: 找到描述文件的路径: ~/Library/MobileDevic ...