#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. buffer overflow

    Computer Systems A Programmer's Perspective Second Edition We have seen that C does not perform any ...

  2. java动态联编

    JAVA中联编有两种,一种是动态联编,一种是静态联编. 动态联编:也叫多态联编或者是迟后联编,因为到底要调用哪一个函数,在编译时不能确定,而要推迟到运行中确定.也就是说,要等到程序运行时,确定了指针所 ...

  3. 【php学习】图片操作

    前两天要对一张图片进行处理,其实很简单,就是在图片上加上字符串,一个图片而已,但是自己如同得了短暂性失忆似的,图片操作的函数一个都想不起来.所以就抽空整理了一下图片操作函数. 图片处理三步走: 创建画 ...

  4. Git版本控制

    官方文档:http://git-scm.com/book/en/v2 github :https://guides.github.com/activities/hello-world/ How to ...

  5. docker squid---but git proxy should specify by git config --global http.proxy http:...

    Usage of loopback devices is strongly discouraged for production use. Either use `--storage-opt dm.t ...

  6. json 增删改 加 排序

    <script type="text/javascript"> var json = { "age":24, "name":&q ...

  7. sqlserver 获取当前操作的数据库名称

    Select Name From Master..SysDataBases Where DbId=(Select Dbid From Master..SysProcesses Where Spid = ...

  8. Lazarus for Raspbian安装

    春节前看到树莓派 2代开始销售,第一时间在淘宝下单购买,无奈春节期间放假,要到3月份才可能收到,只能用QEMU模拟器先熟悉树莓系统.对从turbo Pascal开始的人来讲,如果能在树莓系统使用Pas ...

  9. QT多线程及通过事件进行通信(通过自定义事件,然后QApplication::postEvent给主界面,我之前用的是信号槽)

    可以通过QThread实现跨平台的多线程开发,Qt库负责在特定平台上的特定多线程实现.要采用QThread进行多线程开发,首先需要包含头文件: #include <QThread> 然后需 ...

  10. 帝国CMS备忘

    一. 2级导航: 类似下图这种导航: 实现方式如: 1. 定义一个标签模板,记住ID,具体内容如: 页面模板内容: <li><a href=”[!—bclassurl—]”>[ ...