CodeForces 19D Points(线段树+map)
开始想不通,后来看网上说是set,就有一个想法是对每个x建一个set。。。然后又想直接建立两重的set就好,最后发现不行,自己想多了。。。
题意是给你三种操作:add (x y) 平面添加(x y)这个点
remove (x y)平面删除(x y)这个点
find (x y) 查找(x y)这个点严格的右上方中最左边的点,有多个就再找最下方的点,输出
其实想通了还是比较简单的,我的想法就是对于x先排序再对y排序,这样建一颗线段树,用处在于:添加和删除都可以当成单点更新,只需要记录最大值就好。find时当用map的upper_bound()找到大于x的位置时,可以直接自顶向下搜到范围内大于当前y值(最大值就在这儿用)得最靠前的位置,因为是排序了的,所以最靠前最小。但是范围太大得离散化,我用的是map离散化,另一个二维map找到当前点是树上的第几个点。
注意这儿当没有添加操作时,不能建树,因为这样就会出现 Create(1,0,1)。。。一直爆空间,我还以为map被卡了
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<string>
#include<cstdio>
#include<cstring>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define eps 1E-8
/*注意可能会有输出-0.000*/
#define Sgn(x) (x<-eps? -1 :x<eps? 0:1)//x为两个浮点数差的比较,注意返回整型
#define Cvs(x) (x > 0.0 ? x+eps : x-eps)//浮点数转化
#define zero(x) (((x)>0?(x):-(x))<eps)//判断是否等于0
#define mul(a,b) (a<<b)
#define dir(a,b) (a>>b)
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int ,int > pii;
const int Inf=<<;
const double Pi=acos(-1.0);
const int Max=;
const int Max2=<<;
char str[Max][];
int xx1[Max],yy1[Max],nnow;
map<int,int> mpos;//找到第一个大于需要值的最左边的位置
map<pii,int> mp;//离散化
map<pii,int>::iterator it;
map<int,int>::iterator iit;
int segtr[Max2],tem[Max2],ans,flag;
int nmax(int a,int b)
{
return a>b?a:b;
}
void Upnow(int now,int next)
{
segtr[now]=nmax(segtr[next],segtr[next|]);
return;
}
void Create(int sta,int enn,int now)
{
if(sta==enn)
{
segtr[now]=-;
return;
}
int mid=dir(sta+enn,);
int next=mul(now,);
Create(sta,mid,next);
Create(mid+,enn,next|);
Upnow(now,next);
return;
}
void Update(int sta,int enn,int now,int x,int y)
{
if(sta==enn&&sta==x)
{
segtr[now]=y;
nnow=now;
return;
}
int mid=dir(sta+enn,);
int next=mul(now,);
if(mid>=x)
Update(sta,mid,next,x,y);
else
Update(mid+,enn,next|,x,y);
Upnow(now,next);
return;
}
void Query(int sta,int enn,int now,int x,int y)//找到范围内大于y的最前面的值(基本可以看做最小值)
{
if(sta==enn)
{
ans=now;
flag=;
return;
}
int mid=dir(sta+enn,);
int next=mul(now,);
if(flag&&mid>=x&&segtr[next]>y)//这儿判断保证了的效率
Query(sta,mid,next,x,y);
if(flag&&segtr[next|]>y)
Query(mid+,enn,next|,x,y);
return;
}
int main()
{
int n,coun;
while(~scanf("%d",&n))
{
nnow=;
coun=;
mpos.clear();
mp.clear();
for(int i=;i<n;i++)
{
scanf("%s %d %d",str[i],&xx1[i],&yy1[i]);
if(str[i][] == 'a')
{
mp[make_pair(xx1[i],yy1[i])]=;//二维map
}
}
for(it=mp.begin();it!=mp.end();++it)
{
it->second=coun++;//二维map存应该是树上的第几个节点
if(!mpos.count(it->first.first))
mpos[it->first.first]=coun-;
}
coun--;
if(coun)//一定要注意count>0啊
Create(,coun,);
for(int i=;i<n;i++)
{
if(str[i][] == 'a')
{
Update(,coun,,mp[make_pair(xx1[i],yy1[i])],yy1[i]);
tem[nnow]=xx1[i];
}
else if(str[i][] == 'r')
{
Update(,coun,,mp[make_pair(xx1[i],yy1[i])],-);
tem[nnow]=xx1[i];
}
else
{
ans=-;
flag=;
iit=mpos.upper_bound(xx1[i]);
if(iit==mpos.end())
{
printf("-1\n");
continue;
}
Query(,coun,,iit->second,yy1[i]);
if(ans==-)
printf("-1\n");
else
printf("%d %d\n",tem[ans],segtr[ans]);
}
}
}
return ;
}
CodeForces 19D Points(线段树+map)的更多相关文章
- CodeForces 19D Points (线段树+set)
D. Points time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...
- 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 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 787D. Legacy 线段树建模+最短路
D. Legacy time limit per test:2 seconds memory limit per test:256 megabytes input:standard input out ...
- Almost Regular Bracket Sequence CodeForces - 1095E (线段树,单点更新,区间查询维护括号序列)
Almost Regular Bracket Sequence CodeForces - 1095E You are given a bracket sequence ss consisting of ...
- Sereja and Brackets CodeForces - 380C (线段树+分治思路)
Sereja and Brackets 题目链接: CodeForces - 380C Sereja has a bracket sequence s1, s2, ..., *s**n, or, in ...
- CodeForces 91B Queue (线段树,区间最值)
http://codeforces.com/problemset/problem/91/B B. Queue time limit per test: 2 seconds memory limit p ...
随机推荐
- linux expect
1.首先确定是否安装expect /home/root> which expect /usr/bin/expect 如果没有安装,先安装一下 安装方法: 请参考 http://www.cnblo ...
- Icon Font的转换
Icon Font是用于网页的纯色图标,这里引用一张网络图片: 由于体积小,易维护等特点,IconFont应用非常广泛. 这里推荐一个转换器,通过Upload一个后缀ttf的字体文件,可以反解出文件下 ...
- Effective C++ -----条款24:若所有参数皆需类型转换,请为此采用non-member函数
如果你需要为某个函数的所有参数(包括被this指针所指的那个隐喻参数)进行类型转换,那么这个函数必须是个non-member.
- Match:Power Strings(POJ 2406)
字符串前缀的阶 题目大意:求前缀的阶 和POJ1961是一样的,KMP的Next数组的应用,不要用STL,不要一个一个读入字符(IO永远是最慢的) #include <iostream> ...
- codeforces 507B. Amr and Pins 解题报告
题目链接:http://codeforces.com/problemset/problem/507/B 题目意思:给出圆的半径,以及圆心坐标和最终圆心要到达的坐标位置.问最少步数是多少.移动见下图.( ...
- 最牛逼android上的图表库MpChart(三) 条形图
最牛逼android上的图表库MpChart三 条形图 BarChart条形图介绍 BarChart条形图实例 BarChart效果 最牛逼android上的图表库MpChart(三) 条形图 最近工 ...
- 【leetcode】Palindrome Partitioning II(hard) ☆
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- 60. Permutation Sequence
题目: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of t ...
- swift选择类或结构体
按照通用的准则,当符合一条或多条以下条件时,请考虑构建结构体: 结构体的主要目的是用来封装少量相关简单数据值. 有理由预计一个结构体实例在赋值或传递时,封装的数据将会被拷贝而不是被引用. ? 任何在结 ...
- 在R语言中无法设置CRAN镜像问题
很大的可能是因为使用的浏览器不是IE浏览器的问题,因为CRAN的镜像需要用IE浏览器来打开. 只需要按照下面设置即可: 1.打开IE-->设置-->Internet选项-->高级 2 ...