#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. P2672 推销员

    贪心,水题 #include <bits/stdc++.h> using namespace std; const int maxn = 1000000; struct house { i ...

  2. 用c++builder读取一个一行有多行变量的文件

    文件内容如下: C DXDY.INP FILE, IN FREE FORMAT ACROSS COLUMNS for  83658 Active CellsC 2013-5-25   上午 10:43 ...

  3. [转]如何编写和应用Java的自定义异常类

    编写自定义异常类实际上是继承一个API标准异常类,用新定义的异常处理信息覆盖原有信息的过程.常用的编写自定义异常类的模式如下:   public class CustomException exten ...

  4. oracle 嵌套表

    --自定义对象 CREATE OR REPLACE TYPE Fas_checksheetinfo_line_obj AS OBJECT(  CSID_ID           VARCHAR2(32 ...

  5. java new synchronized

    java provides the synchronized keyword for synchronizing thread access to critical sections. Because ...

  6. sqlserver 简单的事物用法

    SELECT * FROM Interface_UserPort BEGIN TRY BEGIN TRAN Tran_2012_12_25 ,) --raiserror 50005N'抛出错误' CO ...

  7. php--http与https的区别

    在URL前加https://前缀表明是用SSL加密的.你的电脑与服务器之间收发的信息传输将更加安全. Web服务器启用SSL需要获得一个服务器证书并将该证书与要使用SSL的服务器绑定. http和ht ...

  8. PHP--浏览器禁用cookie后,怎么使用session

    sessionid是存储在cookie中的,解决方案如下: Session URL重写,保证在客户端禁用或不支持COOKIE时,仍然可以使用Session session机制.session机制是一种 ...

  9. linux下连接本地的navicate

    1.进mysql cd mysql; cd bin; ./mysql -uroot -proot; show databases;#可以没有 #修改权限 1.GRANT ALL PRIVILEGES ...

  10. JS的基础类型与引用类型

    两种类型: ECMAScript变量包含两种不同类型的值:基本类型值.引用类型值: 基本类型值:指的是保存在栈内存中的简单数据段: 引用类型值:指的是那些保存在堆内存中的对象,意思是,变量中保存的实际 ...