#include<cstdio>
#include<cstdlib>
const int mod =;
const int inf = ~0u>>;
const int maxn = ;
int lim;
struct SplayTree {
. int sz[maxn];
. int ch[maxn][];
. int pre[maxn];
. int rt,top;
. inline void up(int x){
. sz[x] = cnt[x] + sz[ ch[x][] ] + sz[ ch[x][] ];
. }
. inline void Rotate(int x,int f){
. int y=pre[x];
. ch[y][!f] = ch[x][f];
. pre[ ch[x][f] ] = y;
. pre[x] = pre[y];
. if(pre[x]) ch[ pre[y] ][ ch[pre[y]][] == y ] =x;
. ch[x][f] = y;
. pre[y] = x;
. up(y);
. }
. inline void Splay(int x,int goal){//将x旋转到goal的下面
. while(pre[x] != goal){
. if(pre[pre[x]] == goal) Rotate(x , ch[pre[x]][] == x);
. else {
. int y=pre[x],z=pre[y];
. int f = (ch[z][]==y);
. if(ch[y][f] == x) Rotate(x,!f),Rotate(x,f);
. else Rotate(y,f),Rotate(x,f);
. }
. }
. up(x);
. if(goal==) rt=x;
. }
. inline void RTO(int k,int goal){//将第k位数旋转到goal的下面
. int x=rt;
. while(sz[ ch[x][] ] != k-) {
. if(k < sz[ ch[x][] ]+) x=ch[x][];
. else {
. k-=(sz[ ch[x][] ]+);
. x = ch[x][];
. }
. }
. Splay(x,goal);
. }
. inline void vist(int x){
. if(x){
. printf("结点%2d : 左儿子 %2d 右儿子 %2d %2d sz=%d\n",x,ch[x][],ch[x][],val[x],sz[x]);
. vist(ch[x][]);
. vist(ch[x][]);
. }
. }
. inline void Newnode(int &x,int c){
. x=++top;
. ch[x][] = ch[x][] = pre[x] = ;
. sz[x]=; cnt[x]=;
. val[x] = c;
. }
. inline void init(){
. ans=;type=-;
. ch[][]=ch[][]=pre[]=sz[]=;
. rt=top=; cnt[]=;
. Newnode(rt,-inf);
. Newnode(ch[rt][],inf);
. pre[top]=rt;
. sz[rt]=;
. }
. inline void Insert(int &x,int key,int f){
. if(!x) {
. Newnode(x,key);
. pre[x]=f;
. Splay(x,);
. return ;
. }
. if(key==val[x]){
. cnt[x]++;
. sz[x]++;
. return ;
. }else if(key<val[x]) {
. Insert(ch[x][],key,x);
. } else {
. Insert(ch[x][],key,x);
. }
. up(x);
. }
. void Del(){
. int t=rt;
. if(ch[rt][]) {
. rt=ch[rt][];
. RTO(,);
. ch[rt][]=ch[t][];
. if(ch[rt][]) pre[ch[rt][]]=rt;
. }
. else rt=ch[rt][];
. pre[rt]=;
. up(rt);
. }
. void findpre(int x,int key,int &ans){
. if(!x) return ;
. if(val[x] <= key){
. ans=x;
. findpre(ch[x][],key,ans);
. } else
. findpre(ch[x][],key,ans);
. }
. void findsucc(int x,int key,int &ans){
. if(!x) return ;
. if(val[x]>=key) {
. ans=x;
. findsucc(ch[x][],key,ans);
. } else
. findsucc(ch[x][],key,ans);
. }
. void solve() {
. int a,b,x,y;
. scanf("%d%d",&a,&b);
. if(a==type || sz[rt]==){
.
. Insert(rt,b,),type=a;
. //printf("type=%d\n",type);
. }
. else {
. findpre(rt,b,x);
. findsucc(rt,b,y);
. if(abs(val[x]-b) <= abs(val[y]-b)) {
. ans+=abs(val[x]-b);
. ans%=mod;
. Splay(x,);
. Del();
. }
. else {
. ans+=abs(val[y]-b);
. ans%=mod;
. Splay(y,);
. Del();
. }
. }
. //spt.vist(rt);
. }
. int cnt[maxn];
. int val[maxn];
. int type;
. int ans;
.}spt;
.int main()
.{
. int n;
. scanf("%d",&n);
. spt.init();
. while(n--) spt.solve();
. printf("%d\n",spt.ans);
. return ;
}

Splay!的更多相关文章

  1. 【BZOJ-3786】星系探索 Splay + DFS序

    3786: 星系探索 Time Limit: 40 Sec  Memory Limit: 256 MBSubmit: 647  Solved: 212[Submit][Status][Discuss] ...

  2. 【BZOJ】1500: [NOI2005]维修数列(splay+变态题)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1500 模板不打熟你确定考场上调试得出来? 首先有非常多的坑点...我遇到的第一个就是,如何pushu ...

  3. Splay基本操作

    我们以一道题来引入吧! 传送门 题目说的很清楚,我们的数据结构要支持:插入x数,删除x数,查询数的排名和排名为x的数,求一个数前驱后继. 似乎用啥现有的数据结构都很难做到在O(nlogn)的复杂度中把 ...

  4. 【学习笔记】splay入门(更新中)

    声明:本博客所有随笔都参照了网络资料或其他博客,仅为博主想加深理解而写,如有疑问欢迎与博主讨论✧。٩(ˊᗜˋ)و✧*。 前言 终于学习了 spaly \(splay\) !听说了很久,因为dalao总 ...

  5. wikioi 1514 and ZJOI2006 书架

    1514 书架 0人推荐 收藏 发题解 提交代码 报错 题目描述 输入描述 输出描述 样例输入 样例输出 提示 题目描述 Description 小 T有一个很大的书柜.这个书柜的构造有些独特,即书柜 ...

  6. ZJOI 2019 划水记

    作为一个极其蒟蒻的OIer,虽然没有省选资格但还是去见见世面. ZJOI2019一试是在浙江省镇海中学.听名字就很霸气. 学习OI的最后一年,记录下一些事情,即使最终走到最后也一无所获,也是一段美好的 ...

  7. 【bzoj4817】树点涂色 LCT+线段树+dfs序

    Description Bob有一棵n个点的有根树,其中1号点是根节点.Bob在每个点上涂了颜色,并且每个点上的颜色不同.定义一条路 径的权值是:这条路径上的点(包括起点和终点)共有多少种不同的颜色. ...

  8. csp-s2019 AFO记

    DAY 0 上午出发前大家都很颓废的样子. 我因为还没有实现刷完NOIP专题的所有题的目标而去憨比的学DDP. 最后还是不会,保卫王国是写不成了…… 该走了,学校领导来开了个欢送会,祝福我们从里WA到 ...

  9. [JZOJ5977] 【清华2019冬令营模拟12.15】堆

    题目 其中n,q≤500000n,q\leq 500000n,q≤500000 题目大意 让你维护一个堆.支持一下操作: 在某个点的下面加上另一个点,然后进行上浮操作. 询问某一点的权值. 思考历程 ...

随机推荐

  1. js制作带有遮罩弹出层实现登录小窗口

    要实现的效果如下 点击“登录”按钮后,弹出登录小窗口,并且有遮罩层(这个名词还是百度知道的,以前只知道效果,却不知道名字) 在没有点击“登录”按钮之前登录小窗口不显示,点击“登录”按钮后小窗口显示,并 ...

  2. App Previews操作经验

    App Previews操作经验 http://www.cocoachina.com/ios/20140924/9741.html http://www.cocoachina.com/bbs/read ...

  3. BLE-NRF51822教程18-overview

    转自:http://blog.csdn.NET/xgbing 蓝牙协议栈 nrf51822开发中,蓝牙协议栈和应用开发是分开的. (1)兼容蓝牙4.0低功耗协议栈基带层,L2CAP\AAT\SM\GA ...

  4. [LeetCode] Jump Game II(贪婪算法)

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  5. Java实验三报告

    一.  实验内容 (一)敏捷开发与XP 摘要:一项实践在XP环境中成功使用的依据通过XP的法则呈现,包括:快速反馈.假设简单性.递增更改.提倡更改.优质工作.XP软件开发的基石是XP的活动,包括:编码 ...

  6. Ant学习-001-ant 基础知识及windows环境配置

    一.Ant 概要基础知识 Apache Ant 是一个将软件编译.测试.部署等步骤联系在一起加以自动化的一个工具,大多用于Java环境中的软件开发,用以构建应用,或结合其他开源测试工具例如 git.T ...

  7. 【转】Android新组件Material Dialog,SwipeRefreshLayout,ListPopupWindow,PopupMenu等

    朝花夕拾----新组件的学习和使用 分类: Android UI2015-06-26 11:31 440人阅读 评论(0) 收藏 举报 uidialogMaterial   目录(?)[-] Mate ...

  8. django 部署到 apache

    安装完django之后,每次都需要通过命令来启动启动开发服务器.虽然调试和测试方便,但只能在本地运行,并且不能承受许多用户同时使用的负载.所以需要将Django部署到生产级的服务器,这里选择apach ...

  9. Quartz的配置文件quartz.properties详解

    配置 quartz.properties 文件 文件 quartz.properties 定义了 Quartz 应用运行时行为,还包含了许多能控制 Quartz 运转的属性.这个文件应该放在class ...

  10. HBASE架构解析(一)

    http://www.blogjava.net/DLevin/archive/2015/08/22/426877.html 前记 公司内部使用的是MapR版本的Hadoop生态系统,因而从MapR的官 ...