Points
CF#19D:http://codeforces.com/contest/19/problem/D
题意:给你一个点,add x,y表示向集合中添加一个点,remove x,y,表示删除集合中的一个点,find x,y,表示查询比x,y,严格大的点,没有输出-1.
题解;这一题是一道好题,我从中学到了很多东西。首先,这一题的正解是线段树+set。首先按照x值建树,set[i]维护的是x值是i的所有y的集合。同时线段树还维护一个最大值maxn。首先,是离散化。这里用map和vector巧妙的实现了,map[xx[i]]=i;即可。其次是set,当查询dao一个满足条件的x值的时候,要查询y值的时候,可以使用lower_bound来实现。还有就是,线段树的查询,与往常的查询不太一样,以前都是查询某个点的值,这里是查询比两个值大的区间端点,不是一个。最后就是,用vecotr去重的写法:xx.erase(unique(xx.begin(),xx.end()),xx.end());
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
using namespace std;
int n;
map<int,int>Map;
const int N=*1e5+;
vector<int>xx;
struct action{
int x;
int y;
char str[];
void add(){
xx.push_back(x);
}
}OP[N];
struct Node{
int l,r;
int maxn;
inline int mid(){
return (l+r)/;
}
}num[N*];
void pushup(int rt){
num[rt].maxn=max(num[rt<<].maxn,num[rt<<|].maxn);
}
set<int>yy[N];
void build(int l,int r,int rt){
num[rt].l=l;
num[rt].r=r;
num[rt].maxn=-;
if(l==r){
yy[l].clear();
return;
}
int mid=(l+r)/;
build(l,mid,rt<<);
build(mid+,r,rt<<|);
}
void insert(int pos,int y,int rt){
if(num[rt].l==num[rt].r){
yy[pos].insert(y);
num[rt].maxn=*(--yy[pos].end());
return;
}
int mid=num[rt].mid();
if(mid>=pos)insert(pos,y,rt<<);
else insert(pos,y,rt<<|);
pushup(rt);
}
void remove(int pos,int y,int rt){
if(num[rt].l==num[rt].r){
yy[pos].erase(y);
if(yy[pos].size()==)num[rt].maxn=-;
else
num[rt].maxn=*(--yy[pos].end());
return;
}
int mid=num[rt].mid();
if(mid>=pos)remove(pos,y,rt<<);
else remove(pos,y,rt<<|);
pushup(rt);
}
Node query(int y,int rt,int x){
if(num[rt].maxn<y||num[rt].r<x){
Node t1;
t1.l=-;
return t1;
}
if(num[rt].l==num[rt].r){
Node tt;
tt.l=num[rt].l;
tt.r=*yy[num[rt].l].lower_bound(y);
return tt;
}
Node tp=query(y,rt<<,x);
if(tp.l!=-)return tp;
return query(y,rt<<|,x);
} int main(){
while(~scanf("%d",&n)){
xx.clear();Map.clear();
for(int i=;i<n;i++){
scanf("%s%d%d",OP[i].str,&OP[i].x,&OP[i].y);
OP[i].add();
}
sort(xx.begin(),xx.end());
xx.erase(unique(xx.begin(),xx.end()),xx.end());
int len=xx.size();
for(int i=;i<len;i++)
Map[xx[i]]=i;
build(,len-,);
for(int i=;i<n;i++){
if(OP[i].str[]=='a')
insert(Map[OP[i].x],OP[i].y,);
else if(OP[i].str[]=='r')
remove(Map[OP[i].x],OP[i].y,);
else {
Node ans=query(OP[i].y+,,Map[OP[i].x]+);
if(ans.l==-)printf("-1\n");
else
printf("%d %d\n",xx[ans.l],ans.r);
}
}
}
}
Points的更多相关文章
- 有理数的稠密性(The rational points are dense on the number axis.)
每一个实数都能用有理数去逼近到任意精确的程度,这就是有理数的稠密性.The rational points are dense on the number axis.
- [LeetCode] Max Points on a Line 共线点个数
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...
- LeetCode:Max Points on a Line
题目链接 Given n points on a 2D plane, find the maximum number of points that lie on the same straight l ...
- K closest points
Find the K closest points to a target point in a 2D plane. class Point { public int x; public int y; ...
- 【leetcode】Max Points on a Line
Max Points on a Line 题目描述: Given n points on a 2D plane, find the maximum number of points that lie ...
- Max Points on a Line
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...
- [LeetCode OJ] Max Points on a Line
Max Points on a Line Submission Details 27 / 27 test cases passed. Status: Accepted Runtime: 472 ms ...
- [UCSD白板题] Points and Segments
Problem Introduction The goal in this problem is given a set of segments on a line and a set of poin ...
- [UCSD白板题] Covering Segments by Points
Problem Introduction You are given a set of segments on a line and your goal is to mark as few point ...
- [javascript svg fill stroke stroke-width points polygon属性讲解] svg fill stroke stroke-width points polygon绘制多边形属性并且演示polyline和polygon区别讲解
<!DOCTYPE html> <html lang='zh-cn'> <head> <title>Insert you title</title ...
随机推荐
- selenium webdriver启动Chrome浏览器后无法输入网址的解决办法
通过selenium webdriver启动Chrome浏览器,脚本如下: from selenium import webdriver browser = webdriver.Chrome() br ...
- 3D游戏引擎一 win32编程
Windows程序一般都等待用户进行一些操作,然后响应并採取行动. 一般来说.对win32的程序的操作都会转换为系统事件队列中的消息,如按键消息WM_KEYDOWN,WM_MOUSECLICK等传递键 ...
- 三星笔记本R428安装xp win7双系统,切换系统重启才能进入系统解决办法。
三星笔记本 XP win7 双系统切换重启解决方法 三星笔记本有个奇怪的现象,就是装有XP和win7双系统 xp切换到win7.进系统是会重启一次,并且bios回复光驱为第一启动项,win7切换 ...
- LeetCode:Permutations(求全排列)
Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...
- java内存不足
-Xmx1024m -Xms1024m -XX:PermSize=128m -XX:MaxPermSize=512m ------------------------- 亲测可用
- C#“简单加密文本器”的实现
本示例只能加密英文文本,使用的算法为异或算法.源代码:http://pan.baidu.com/share/link?shareid=3241348313&uk=1761850335(本示例属 ...
- AutoBackupForApps
This sample demonstrates how to selectively disable Automatic Backups in Android M, either by adjust ...
- java 项目request.getParameter("")接收不到值
如果发现这个方法 以前能接收到参数,现在不能接收到参数的情况下 很有问题出来tocat 或许jak 的问题上, 换个低版本可能就好了
- asp.net 微信公众平台我也来搞一搞(看看和大家的有什么不一样)
去年10月份左右接触的微信,今年又要在上面做新的改动(好烦人,为什么我这么懒),小菜用了2天的工作日,做了一个微信解析器,方便大家调用. 好处: 1.屌丝们不用再去做微信验证. 2.屌丝们不用去解析x ...
- Android-SVN
服务器启动svn服务 svnserve -d -r /home/wbp/svn/actia/ 1 .svn 重新定位location , 改变新仓库的uuid , 今天操作SVN Client 发现 ...