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 ...
随机推荐
- iframe与父页面中JS执行顺序控制
同事遇到了一个问题: 父页面中有几个iframe,初衷是父页面的JS通过AJAX获取数据,然后用于初始化iframe页面,可以结果却是有的iframe页面却不能获得数据. [问题根源] 父页面在加载i ...
- appium点击屏幕(手势)
在android测试过程中,会遇到要点击一下屏幕的需求. 在appium旧版本使用下面代码点击android屏幕,没有报错.Map tap = new HashMap(); tap.put(" ...
- Mac内建Apache开机启动
取消: sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist 添加: sudo launchctl ...
- MVC 无法将类型“System.Collections.Generic.List<AnonymousType#1>”隐式转换为“System.Collections.Generic.IList<Mvc3Modeltest.Models.Movie>”。存在一个显式转换(是否缺少强制转换?))
1.问题: 2.解决方案:强制指定类型. 解决之.
- undo损坏故障恢复(二)ORA-01092,ORA-00604,ORA-01110
undo 故障诊断与恢复(二) 今天是2013-09-01,目前困扰我将近一周的问题,终于解决了,我非常感谢帮助我的朋友,也非常感谢管我要钱然后替我解决问题的朋友(我没采用).这更激发了我一定要解决这 ...
- [PWA] 16. Clean IDB
When we save items to IndexDB, we need to think about clean the items or keep those in a short list. ...
- 三星笔记本R428安装xp win7双系统,切换系统重启才能进入系统解决办法。
三星笔记本 XP win7 双系统切换重启解决方法 三星笔记本有个奇怪的现象,就是装有XP和win7双系统 xp切换到win7.进系统是会重启一次,并且bios回复光驱为第一启动项,win7切换 ...
- cocos2d-x项目过程记录(纹理和内存优化方面)
1.参考资料:Cocos2d-x纹理优化的一些方案 cocos2d-x如何优化内存的应用 iOS和android游戏纹理优化和内存优化(cocos2d-x) 2.加载贴图集纹理 CCSpriteF ...
- iOS UI布局调试工具
查看ios软件的ui布局有三种: 1.DCIntrospect 这种方式是开源的,我从github上clone下来后运行demo,运行遇到了问题:Xcode cannot run using t ...
- Creating a Background Service ——IntentService
The IntentService class provides a straightforward structure for running an operation on a single ba ...