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的更多相关文章

  1. 有理数的稠密性(The rational points are dense on the number axis.)

    每一个实数都能用有理数去逼近到任意精确的程度,这就是有理数的稠密性.The rational points are dense on the number axis.

  2. [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. ...

  3. 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 ...

  4. K closest points

    Find the K closest points to a target point in a 2D plane. class Point { public int x; public int y; ...

  5. 【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 ...

  6. 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. ...

  7. [LeetCode OJ] Max Points on a Line

    Max Points on a Line Submission Details 27 / 27 test cases passed. Status: Accepted Runtime: 472 ms ...

  8. [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 ...

  9. [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 ...

  10. [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 ...

随机推荐

  1. 3Dmax导出fbx文件缺失纹理问题

  2. 第七讲:HTML5中的canvas两个小球全然弹性碰撞

    <html> <head> <title>小球之间的碰撞(全然弹性碰撞)</title> <script src="../js/jsce ...

  3. 实现nodejs的promises库(基于promise.js改写)

    原promise.js库地址:https://github.com/stackp/promisejs promises是JavaScript实现优雅编程的一个非常不错的轻量级框架.该框架可以让你从杂乱 ...

  4. codevs 2541 幂运算(迭代加深搜索)

    /* 一开始想到了简单的深搜 维护当前可用的mi数组 然后回溯用哪个 不断更新新产生的mi 这样的问题是 由于mi不断产生 搜索规模扩大 不好 不好 下面是奇丑的WA掉的代码 做个反面教材 */ #i ...

  5. php 在web端读出pdf 与各种文件下载

    单纯的下载功能实现 <?php // 表示调用文本类型为pdf的应用 header('Content-type: application/pdf'); // 这句可以输出下载页面进行下载 hea ...

  6. jsp中的c标签

    核心标签库 引用: <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> ...

  7. Hql 执行CRUD

    //新增] @Test public void add(){ config = new Configuration(); sessionfactory = config.configure(" ...

  8. Android打开系统的Document文档图片选择

    打开Document UI 过滤图片 private void startAcitivty() { Intent intent = new Intent(); intent.setAction(&qu ...

  9. 你好,C++(10)这次的C++考试你过了没有?C++中表示逻辑判断的布尔数据类型

    3.4  布尔类型 在日常生活中,我们除了需要使用int类型的变量表示216路公交车:需要使用float类型的变量表示西红柿3.5元一斤,有时候还需要表示一种数据,那就是逻辑状态: “这次的C++考试 ...

  10. 【USACO 1.5.3】特殊的质数肋骨

    [题目描述]农民约翰的母牛总是生产出最好的肋骨.你能通过农民约翰和美国农业部标记在每根肋骨上的数字认出它们. 农民约翰确定他卖给买方的是真正的质数肋骨,是因为从右边开始切下肋骨,每次还剩下的肋骨上的数 ...