题意:支持插入/删除点 $(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. Spring MVC传输对象属性

    今天搬砖时遇到一个问题,前端使用JSP+form传输数据,后台使用Spring MVC接收,但是接收到的对象属性一直是null,找了好久才发现原因,代码如下 前端代码   后端代码   需要注意一点 ...

  2. JavaScript Web API 全选反选案例

    全选反选 全选和反选功能,在开发中可以说是应用得非常多的,以下通过案例分解,学习如何使用JS实现全选反选功能. 该功能可分为如下三大步骤: 1.全选 1.1 获取父checkbox,注册点击事件 1. ...

  3. UOJ348 WC2018 州区划分 状压DP、欧拉回路、子集卷积

    传送门 应该都会判欧拉回路吧(雾 考虑状压DP:设\(W_i\)表示集合\(i\)的点的权值和,\(route_i\)表示点集\(i\)的导出子图中是否存在欧拉回路,\(f_i\)表示前若干个城市包含 ...

  4. QSDK与OPENWRT区别

    QSDK与OPENWRT区别 来源 https://www.jianshu.com/p/178ae18b2570 QSDK是一种在openwrt的基础上,加入了高通atheros芯片相关资料的一种环境 ...

  5. python3基础之“小练习(1)”

    (一)打印3个不同的字符 # a=int("123") # b="123" # c=1.2 # print(type(a),a) # print(type(b) ...

  6. 装饰器带类参数 & 一个函数应用多个装饰器

    装饰器:不改变原函数的基础上,给函数增加功能的方式,称为装饰器 即:为已经存在的对象添加额外的功能 装饰器其实就是一个闭包,把一个函数当做参数后返回一个替代版的函数 decos.py:(装饰器的参数类 ...

  7. Part_two:Redis之发布订阅

    Redis发布订阅 发布订阅的命令 PUBLISH channel msg 将信息 message 发送到指定的频道 channel SUBSCRIBE channel [channel ...] 订 ...

  8. MFC_选择目录对话框_选择文件对话框_指定目录遍历文件

    选择目录对话框 void C资源共享吧视频广告清理工具Dlg::OnBnClickedCls() { // 清空编辑框内容 m_Edit.SetWindowTextW(L""); ...

  9. QTableWidget数据表格

    void setRowHeight(int row, int height); //行高 void setVerticalHeaderLabels(const QStringList &lab ...

  10. ceph 接入OpenStack

    创建对应的pool: ceph osd pool create volumes 512 ceph osd pool create images 512 ceph osd pool create vms ...