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 ...
随机推荐
- java web 学习 --第六天(Java三级考试)
第五天学习在这:http://www.cnblogs.com/tobecrazy/p/3458592.html session对象 当某个用户首次访问web应用系统时,jsp会自动创建出一个sessi ...
- ABAP 将SAP用户ID转换成用户名
FORM frm_coverted_name USING usrid TYPE sy-uname CHANGING name TYPE adrp-name ...
- 【leetcode】Factorial Trailing Zeroes(easy)
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
- iOS MRC ARC 内存管理
转自:http://www.jianshu.com/p/48665652e4e4 1. 什么是内存管理 程序在运行的过程中通常通过以下行为,来增加程序的的内存占用 创建一个OC对象 定义一个变量 调用 ...
- 解决Eclipse里的Maven工程pom.xml文件报:web.xml is missing and <failOnMissingWebXml> is set to true错误
打开eclipse准备进行开发时,发现项目上有个红星号,查看错误后发现报了一个:"web.xml is missing and <failOnMissingWebXml> is ...
- 假期(codevs 3622)
题目描述 Description 经过几个月辛勤的工作,FJ决定让奶牛放假.假期可以在1-N天内任意选择一段(需要连续),每一天都有一个享受指数W.但是奶牛的要求非常苛刻,假期不能短于P天,否则奶牛不 ...
- Git命令之从GitHub上下载开源项目
1,先在本地创建一个目录,作为本地仓库,如: 2,使用Git init 初始化仓库,git初始化完成后,会生成一个隐藏的git文件如: 3,clone Git项目,如: 4,这个项目就是合Github ...
- javaWeb四大域对象
servletContext Request HttpSession pageContext 详细参考:http://www.tuicool.com/articles/NJfyMrn
- 已有a,b两个链表,每个链表中的结点包括学号,成绩。要求把两个链表合并。按学号升序排列.
#include <stdio.h>#define SIZE sizeof(struct student)struct student{ long num; flo ...
- Google 如何修复 TrustManager 实施方式不安全的应用
引用谷歌市场的帮助说明:https://support.google.com/faqs/answer/6346016 本文面向的是发布的应用中 X509TrustManager 接口实施方式不安全的开 ...