题意:支持插入/删除点 $(x,y)$,查询一个点右上方横坐标与之最接近的点坐标.

我们可以对于每一个操作过的横坐标都开一个 $set$,然后再开一个平衡树,维护每个横坐标上最大的纵坐标.

然后查询点 $(x,y)$ 时就在平衡树查一下第一个横坐标大于 $x$,且最大值大于 $y$ 的就行了.

$splay$ 中有一些细节需要注意一下.

#include <set>
#include <map>
#include <cstdio>
#include <algorithm>
#define N 200005
#define inf 1200000005
#define lson p[x].ch[0]
#define rson p[x].ch[1]
#define setIO(s) freopen(s".in","r",stdin)
using namespace std;
int tot,cnt,pp,root;
set<int>S[N];
map<int,int>idx,sp;
set<int>::iterator it;
struct data
{
int ch[2],f,maxx,val,id,size;
}p[N];
int newnode() { return ++tot; }
int get(int x) { return p[p[x].f].ch[1]==x; }
void pushup(int x)
{
p[x].maxx=p[x].val;
p[x].maxx=max(p[x].val,max(p[lson].maxx,p[rson].maxx));
p[x].size=p[lson].size+p[rson].size+1;
}
void rotate(int x)
{
int old=p[x].f,fold=p[old].f,which=get(x);
p[old].ch[which]=p[x].ch[which^1],p[p[old].ch[which]].f=old;
p[x].ch[which^1]=old,p[old].f=x,p[x].f=fold;
if(fold) p[fold].ch[p[fold].ch[1]==old]=x;
pushup(old),pushup(x);
}
void splay(int x,int &tar)
{
int fa,u=p[tar].f;
for(;(fa=p[x].f)!=u;rotate(x)) if(p[fa].f!=u) rotate(get(fa)==get(x)?fa:x);
tar=x;
}
void insert(int &x,int ff,int id,int v)
{
if(!x)
{
x=newnode();
p[x].id=id,p[x].val=v;
if(!sp[id]) sp[id]=++pp;
p[x].f=ff;
pushup(x);
}
else
{
insert(p[x].ch[id>p[x].id],x,id,v);
pushup(x);
}
}
int getr(int x)
{
while(rson) x=rson;
return x;
}
int getpre(int v)
{
int x=root,pre=root;
while(x)
{
if(p[x].id<=v) pre=x, x=rson;
else x=lson;
}
return pre;
}
int find(int x,int d)
{
if(p[lson].maxx>d) return find(lson,d);
else if(p[x].val>d) return p[x].id;
else return find(rson,d);
}
int main()
{
// setIO("input");
int i,j,n;
scanf("%d",&n);
p[0].maxx=-inf;
insert(root,0,-inf,-inf);
insert(root,0, inf,-inf);
for(i=1;i<=n;++i)
{
char str[10];
scanf("%s",str);
if(str[0]=='a')
{
int x,y;
scanf("%d%d",&x,&y);
if(!idx[x]) idx[x]=++cnt;
S[idx[x]].insert(-y);
int xx=-(*S[idx[x]].begin());
if(sp[x])
{
splay(sp[x], root);
p[root].val=xx;
pushup(root);
}
else
{
insert(root,0,x,y);
splay(tot,root);
// printf("%d\n",p[root].ch[1]);
}
}
if(str[0]=='r')
{
int x,y;
scanf("%d%d",&x,&y);
S[idx[x]].erase(-y);
if(S[idx[x]].empty())
{
splay(sp[x],root);
int L=getr(p[root].ch[0]);
int R=p[root].ch[1];
splay(L,p[root].ch[0]);
p[L].f=0, p[L].ch[1]=R, p[R].f=root=L;
pushup(L);
idx[x]=sp[x]=0;
}
else
{
splay(sp[x],root);
p[root].val=-(*S[idx[x]].begin());
pushup(root);
}
}
if(str[0]=='f')
{
int x,y;
scanf("%d%d",&x,&y);
int L=getpre(x);
splay(L,root);
if(p[p[root].ch[1]].maxx<=y) printf("-1\n");
else
{
int R=p[root].ch[1];
int xx=find(R,y);
int yy=idx[xx];
it=S[yy].lower_bound(-y);
if(it==S[yy].end()) it--;
while(*it>=-y) it--;
printf("%d %d\n",xx,-*it);
}
}
}
return 0;
}

  

CF19D Points 平衡树的更多相关文章

  1. [hdu4347]The Closest M Points(平衡树式kdtree)

    解题关键:模板题(结合起来了) #include<iostream> #include<cstdio> #include<cstring> #include< ...

  2. CF 19D - Points 线段树套平衡树

    题目在这: 给出三种操作: 1.增加点(x,y) 2.删除点(x,y) 3.询问在点(x,y)右上方的点,如果有相同,输出最左边的,如果还有相同,输出最低的那个点 分析: 线段树套平衡树. 我们先离散 ...

  3. CF 19D Points 【线段树+平衡树】

    在平面上进行三种操作: 1.add x y:在平面上添加一个点(x,y) 2.remove x y:将平面上的点(x,y)删除 3.find x y:在平面上寻找一个点,使这个点的横坐标大于x,纵坐标 ...

  4. 【线段树】【CF19D】 Points

    传送门 Description 在一个笛卡尔坐标系中,定义三种操作: \(add(x,y)\),将点\((x,y)\)标记在坐标系上 \(find(x,y)\),查询点\((x,y)\)严格右上方中, ...

  5. BZOJ 2733 HNOI 2012 永无乡 平衡树启示式合并

    题目大意:有一些岛屿,一開始由一些无向边连接. 后来也有不断的无向边增加,每个岛屿有个一独一无二的重要度,问随意时刻的与一个岛屿联通的全部岛中重要度第k大的岛的编号是什么. 思路:首先连通性一定要用并 ...

  6. [模板] 平衡树: Splay, 非旋Treap, 替罪羊树

    简介 二叉搜索树, 可以维护一个集合/序列, 同时维护节点的 \(size\), 因此可以支持 insert(v), delete(v), kth(p,k), rank(v)等操作. 另外, prev ...

  7. BZOJ 2809 APIO 2012 dispatching 平衡树启示式合并

    题目大意:给出一棵树,每个节点有两个值,各自是这个忍者的薪水和忍者的领导力.客户的惬意程度是这个点的领导力乘可以取得人数.前提是取的人的薪水总和不超过总的钱数. 思路:仅仅能在子树中操作.贪心的想,我 ...

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

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

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

随机推荐

  1. vue中$router与$route的区别

    $.router是VueRouter的实例,相当于一个全局的路由器对象.包含很多属性和子对象,例如history对象 $.route表示当前正在跳转的路由对象.可以通过$.route获取到name,p ...

  2. 2.6_Database Interface JDBC及驱动类型

    JAVA语言参考ODBC,设计专用的数据库连接规范JDBC(JAVA Database Connectivity).目标是让Java开发人员在编写数据库应用程序时,可以有统一的接口,不依赖特定数据库A ...

  3. 有关Nodejs的一些插件介绍

    var child_process = require('child_process');这个可以执行cmd的命令 child_process.exec(cmdLine, function(error ...

  4. js 移动端之监听软键盘弹出收起

    js 移动端关于页面布局,如果底部有position:fixed的盒子,又有input,当软键盘弹出收起都会影响页面布局.这时候Android可以监听resize事件,代码如下,而ios没有相关事件. ...

  5. Jmeter学习笔记(十五)——常用的4种参数化方式

    一.Jmeter参数化概念 当使用JMeter进行测试时,测试数据的准备是一项重要的工作.若要求每次迭代的数据不一样时,则需进行参数化,然后从参数化的文件中来读取测试数据. 参数化是自动化测试脚本的一 ...

  6. Java编程规范(命名规则)

    1.目的 编程规范是对编程的一种约定,主要作用是增强代码的可读性和可维护性,便于代码重用. 2.命名规则 首先要求程序中的各个要素都遵守命名规则,然后在编码中严格按照编码格式编写代码.命名规则包括以下 ...

  7. Java内存模型JMM简单分析

    参考博文:http://blog.csdn.net/suifeng3051/article/details/52611310 http://www.cnblogs.com/nexiyi/p/java_ ...

  8. MySQL Index--NOT IN和不等于两类操作无法走索引?

    经常被问,NOT IN和<>操作就无法走索引? 真想只有一个:具体问题具体分析,没有前提的问题都是耍流氓. 准备测试数据: ## 删除测试表 DROP TABLE IF EXISTS tb ...

  9. spark 机器学习 决策树 原理(一)

    1.什么是决策树 决策树(decision tree)是一个树结构(可以是二叉树或者非二叉树).决策树分为分类树和回归树两种,分类树对离散变量做决策树,回归树对连续变量做决策树. 其中每个非叶节点表示 ...

  10. Flask之WTfroms组件

    一.WTfroms简介 WTForms插件是类似于django的form组件的插件,可以帮我们写标签,校验数据等. 二.安装与使用 安装: pip install WTForms 使用: from w ...