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 ...
随机推荐
- ffmpeg-20160731-bin.7z
ESC 退出 0 进度条开关 1 屏幕原始大小 2 屏幕1/2大小 3 屏幕1/3大小 4 屏幕1/4大小 S 下一帧 [ -2秒 ] +2秒 ; -1秒 ' +1秒 下一个帧 -> -5秒 f ...
- ffmpeg-20160512-git-bin
ESC 退出 0 进度条开关 1 屏幕原始大小 2 屏幕1/2大小 3 屏幕1/3大小 4 屏幕1/4大小 S 下一帧 [ -2秒 ] +2秒 ; -1秒 ' +1秒 下一个帧 -> -5秒 f ...
- Java for LeetCode 234 Palindrome Linked List
解题思路: O(1)的空间复杂度,意味着不能通过开一个List来解决问题.我们可以把List分成前后两个部分,后半部分通过指针的相互赋值进行翻转即可. JAVA实现如下: public static ...
- 针对SYN洪水攻击的防御措施
可以运用sysctl命令进行配置,由于本命令参数较多,这里只简单记录几个比较常用的参数: 1.tcp_max_syn_backlog 这个参数指定了后备队列可维持的TCP半开连接的数目,如果该值设定很 ...
- Struts2应用流程注解
当Web容器收到请求(HttpServletReques t)它将请求传递给一个标准的的过滤链包括(ActionContextCleanUp)过滤器. 经过Other filters(SiteMe ...
- 经典排序算法 - 冒泡排序Bubble sort
原理是临近的数字两两进行比较,按照从小到大或者从大到小的顺序进行交换, 这样一趟过去后,最大或最小的数字被交换到了最后一位, 然后再从头开始进行两两比较交换,直到倒数第二位时结束,其余类似看例子 例子 ...
- 查询局域网内在线电脑IP
COLOR 0A CLS @ECHO Off Title 查询局域网内在线电脑IP :send @ECHO off&setlocal enabledelayedexpansion ECHO 正 ...
- RST_n的问题
有一个灰常郁闷的问题... module CLK_Generater( input CLOCK_100, i ...
- Python中读取csv文件内容方法
gg 224@126.com 85 男 dd 123@126.com 52 女 fgf 125@126.com 23 女 csv文件内容如上图,首先导入csv包,调用csv中的方法reader()创建 ...
- 整理 iOS 9 适配中出现的坑(图文)(转)
作者:董铂然 本文主要是说一些iOS9适配中出现的坑,如果只是要单纯的了解iOS9新特性可以看瞄神的开发者所需要知道的 iOS 9 SDK 新特性.9月17日凌晨,苹果给用户推送了iOS9正式版,随着 ...