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. 深入剖析Java中的自动装箱和拆箱过程

    深入剖析Java中的装箱和拆箱 自动装箱和拆箱问题是Java中一个老生常谈的问题了,今天我们就来一些看一下装箱和拆箱中的若干问题.本文先讲述装箱和拆箱最基本的东西,再来看一下面试笔试中经常遇到的与装箱 ...

  2. iOS中ASI和AFN的区别

    一.底层实现 1> AFN的底层基于OC的NSURLConnection和NSURLSession 2> ASI的底层基于纯C语言的CFNetwork框架 3> ASI的运行性能 高 ...

  3. java关键字synchronized

    1.对于synchronized方法和synchronized块,一个线程访问时,其他线程可以访问此类的非synchronized方法或块,不能访问其他带synchronized的方法或块. 举例如下 ...

  4. Sublime 注册码

    ----- BEGIN LICENSE ----- Andrew Weber Single User License EA7E-855605 813A03DD 5E4AD9E6 6C0EEB94 BC ...

  5. Qt应用中检测内存泄露——VLD

    本文简要描述一下在Qt应用中使用VLD来检测内存泄露.本次测试环境:QtCreator2.3 + Qt4.7.4-vs2008 + VS2008 Express. 1.下载并安装:VLD-2.2: h ...

  6. 从零开始学习jQuery-------jQuery元素选择器(三)

    下面我们来介绍一下jQuery元素选择器,在Web开发中我们最常用的操作是获取元素,然后对获取的元素进行一系列的操作,jQuery根据获取页面元素的不同,可以将jQuery选择器分为四大类:基本选择器 ...

  7. [转] JavaScript中的字符串操作

    一.概述    字符串在JavaScript中几乎无处不在,在你处理用户的输入数据的时候,在读取或设置DOM对象的属性时,在操作cookie时,当然还有更 多....JavaScript的核心部分提供 ...

  8. NYOJ-520 最大素因子

    这个题基本上就两个知识点, 一个素数筛选法求素数,另一个是求最大公因子, 不过确定最大素数在素数表中的位置时,要用到二分的思想,不然会超时,下面是具体代码的实现; #include <stdio ...

  9. python练习程序_员工信息表_基本实例

    python实现增删改查操作员工信息文件,可进行模糊查询: http://edu.51cto.com/lesson/id-13276.html http://edu.51cto.com/lesson/ ...

  10. win7+SQL2008无法打开物理文件 操作系统错误 5:拒绝访问 SQL Sever

    今天在win7+SQL2008的环境下操作分离附加数据库,分离出去然后再附加,没有问题.但是一把.mdf文件拷到其它文件夹下就出错,错误如下:无法打开物理文件 "E:\db\MyDB.mdf ...