bzoj 3595
Splay 每个节点维护一个区间。
/**************************************************************
Problem: 3595
User: idy002
Language: C++
Result: Accepted
Time:5428 ms
Memory:56020 kb
****************************************************************/ #include <cstdio>
#include <map>
#define N 2000000
using namespace std; map<int,int> atob, btoa;
map<int,int> rng_id; struct Splay {
int son[N][], pre[N], xlf[N], xrg[N], siz[N], root, ntot; int newnode( int p, int lf, int rg ) {
if( lf>rg ) return ;
int nd = ++ntot;
son[nd][] = son[nd][] = ;
pre[nd] = p;
xlf[nd] = lf;
xrg[nd] = rg;
siz[nd] = rg-lf+;
return nd;
}
void update( int nd ) {
siz[nd] = siz[son[nd][]]+siz[son[nd][]]+(xrg[nd]-xlf[nd]+);
}
void rotate( int nd, int d ) {
int p=pre[nd];
int s=son[nd][!d];
int ss=son[s][d]; son[nd][!d] = ss;
son[s][d] = nd;
if( p ) son[p][nd==son[p][]]=s;
else root=s; pre[nd] = s;
pre[s] = p;
if( ss ) pre[ss] = nd; update( nd );
update( s );
}
void splay( int nd, int top= ) {
while( pre[nd]!=top ) {
int p=pre[nd];
int nl=nd==son[p][];
if( pre[p]==top ) {
rotate( p, nl );
} else {
int pp=pre[p];
int pl=p==son[pp][];
if( nl==pl ) {
rotate( pp, pl );
rotate( p, nl );
} else {
rotate( p, nl );
rotate( pp, pl );
}
}
}
}
void init( int lf, int rg ) {
ntot = ;
root = newnode( , lf, rg );
rng_id[rg] = root;
}
void make_one( int nd ) {
int lnd, rnd;
splay( nd );
lnd = son[nd][];
rnd = son[nd][];
while( son[lnd][] ) lnd=son[lnd][];
while( son[rnd][] ) rnd=son[rnd][];
if( lnd && rnd ) {
splay( lnd );
splay( rnd, lnd );
} else if( lnd ) {
splay( lnd );
} else if( rnd ) {
splay( rnd );
}
}
void split( int nd, int pos ) {
if( xlf[nd]==xrg[nd] ) return;
make_one( nd );
int lnd, rnd;
lnd = newnode( , xlf[nd], pos- );
rnd = newnode( , pos+, xrg[nd] );
son[nd][] = lnd;
son[nd][] = rnd;
if( lnd ) {
pre[lnd] = nd;
rng_id[pos-] = lnd;
}
if( rnd ) {
pre[rnd] = nd;
rng_id[xrg[nd]] = rnd;
}
rng_id[pos] = nd;
xlf[nd] = xrg[nd] = pos;
update( nd );
splay( nd );
}
void erase( int nd ) {
make_one( nd );
int p=pre[nd];
pre[nd] = ;
if( p ) {
son[p][ nd==son[p][] ] = ;
pre[nd] = ;
update( p );
splay( p );
} else {
root = ;
pre[nd] = ;
}
}
void push_front( int nn ) {
if( !root ) {
root = nn;
return;
}
int nd=root;
while( son[nd][] ) nd=son[nd][];
son[nd][] = nn;
pre[nn] = nd;
splay( nn );
}
void push_back( int nn ) {
if( !root ) {
root = nn;
return;
}
int nd=root;
while( son[nd][] ) nd=son[nd][];
son[nd][] = nn;
pre[nn] = nd;
splay(nn);
}
int rank( int nd ) {
int rt=siz[son[nd][]]+;
int nnd=nd;
while( pre[nd] ) {
int p=pre[nd];
if( nd==son[p][] ) rt += siz[son[p][]]+(xrg[p]-xlf[p]+);
nd=p;
}
splay(nnd);
return rt;
}
int nth( int k ) {
int nd=root;
while() {
int ls=siz[son[nd][]];
int lcs=ls+(xrg[nd]-xlf[nd]+);
if( k<=ls ) {
nd = son[nd][];
} else if( k<=lcs ) {
int rt = xlf[nd]+k-ls-;
splay( nd );
return rt;
} else {
k -= lcs;
nd = son[nd][];
}
}
}
}T; int n, m;
int main() {
scanf( "%d%d", &n, &m );
T.init( , n );
int la = ;
for( int i=; i<=m; i++ ) {
int opt, x, y;
scanf( "%d%d", &opt, &x );
x -= la;
if( opt== ) {
scanf( "%d", &y );
y -= la;
int pos = btoa.count(x) ? btoa[x] : x;
atob[pos] = y;
btoa[y] = pos;
int nd= rng_id.lower_bound( pos )->second;
T.split( nd, pos );
printf( "%d\n", la=T.rank(nd) );
} else if( opt== ) {
int pos = btoa.count(x) ? btoa[x] : x;
int nd=rng_id.lower_bound( pos )->second;
T.split( nd, pos );
printf( "%d\n", la=T.rank(nd) );
T.erase( nd );
T.push_front( nd );
} else if( opt== ) {
int pos = btoa.count(x) ? btoa[x] : x;
int nd = rng_id.lower_bound( pos )->second;
T.split( nd, pos );
printf( "%d\n", la=T.rank(nd) );
T.erase( nd );
T.push_back( nd );
} else {
int pos=T.nth(x);
int b = atob.count(pos) ? atob[pos] : pos;
printf( "%d\n", la=b );
}
}
}
bzoj 3595的更多相关文章
- BZOJ 3595: [Scoi2014]方伯伯的Oj SBT+可持久化Treap
3595: [Scoi2014]方伯伯的Oj Time Limit: 6 Sec Memory Limit: 256 MBSubmit: 102 Solved: 54[Submit][Status ...
- 【bzoj 3595】: [Scoi2014]方伯伯的Oj
传送门&& 原题解 蒟蒻终于做到一道方伯伯的题了…… 调了一个上午一直TLE(发现自己打了好久的splay板子竟然是错的这种丢人事情我就不说了) 很明显,要建两棵树,$T1$维护排名, ...
- BZOJ 3595: [Scoi2014]方伯伯的Oj Splay + 动态裂点 + 卡常
Description 方伯伯正在做他的Oj.现在他在处理Oj上的用户排名问题. Oj上注册了n个用户,编号为1-”,一开始他们按照编号排名.方伯伯会按照心情对这些用户做以下四种操作,修改用户的排名和 ...
- BZOJ 2127: happiness [最小割]
2127: happiness Time Limit: 51 Sec Memory Limit: 259 MBSubmit: 1815 Solved: 878[Submit][Status][Di ...
- BZOJ 3275: Number
3275: Number Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 874 Solved: 371[Submit][Status][Discus ...
- BZOJ 2879: [Noi2012]美食节
2879: [Noi2012]美食节 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 1834 Solved: 969[Submit][Status] ...
- bzoj 4610 Ceiling Functi
bzoj 4610 Ceiling Functi Description bzoj上的描述有问题 给出\(n\)个长度为\(k\)的数列,将每个数列构成一个二叉搜索树,问有多少颗形态不同的树. Inp ...
- BZOJ 题目整理
bzoj 500题纪念 总结一发题目吧,挑几道题整理一下,(方便拖板子) 1039:每条线段与前一条线段之间的长度的比例和夹角不会因平移.旋转.放缩而改变,所以将每条轨迹改为比例和夹角的序列,复制一份 ...
- 【sdoi2013】森林 BZOJ 3123
Input 第一行包含一个正整数testcase,表示当前测试数据的测试点编号.保证1≤testcase≤20. 第二行包含三个整数N,M,T,分别表示节点数.初始边数.操作数.第三行包含N个非负整数 ...
随机推荐
- HDU 1172 猜数字 (模拟)
题目链接 Problem Description 猜数字游戏是gameboy最喜欢的游戏之一.游戏的规则是这样的:计算机随机产生一个四位数,然后玩家猜这个四位数是什么.每猜一个数,计算机都会告诉玩家猜 ...
- [转载]PM管理技巧
产品经理的沟通策略 2016年10月11日/分类: 文章 /编辑: Amy 产品经理处于沟通枢纽的位置,工作中需要跟各种岗位的人打交道,比如:领导.开发.运营.客户.用户.合作伙伴… 沟通能力是产 ...
- password passphrase passcode 的区别
In general, passphrases are long passwords and passcodes are numeric-only passwords.
- linux 下 genymotion 模拟器不能安装app
提示: "应用未安装" 解决方法: 下载: Genymotion-ARM-Translation_v1.1.zip 进入genymotion 的tools用adb传进去: ./ad ...
- gunicorn之日志详细配置
gunicorn的日志配置 gunicorn的日志配置相关的常用参数有4个,分别是accesslog,access_log_format,errorlog,loglevel. accesslog:用户 ...
- 75.VS2013和opencv3.1.0开发环境配置
首先要做的就是 开发环境配置,具体过程如下: Step 1:OpenCV环境变量配置 我的电脑--->属性--->高级系统设置--->高级--->环境变量--->系统变量 ...
- spring源码解析--事务篇(前篇)
对于每一个JAVA程序员,spring应该是再熟悉不过的框架了,它的功能有多强大我就不多说了,既然他有这么强大的功能,是如何实现的呢?这个就需要从他的原理去了解,而最直接了解原理的方式莫过于源码.当然 ...
- 20165301 2017-2018-2 《Java程序设计》第五周学习总结
20165301 2017-2018-2 <Java程序设计>第五周学习总结 教材学习内容总结 第七章:内部类与异常类 内部类 在一个类中定义另一个类 非内部类不可以是static类 匿名 ...
- 【数据挖掘基础算法】KNN最近邻分类算法
算法简介: 通过计算待预测样本和已知分类号的训练样本之间的距离来判断该样本属于某个已知分类号的概率.并选取概率最大的分类号来作为待预测样本的分类号 懒惰分类算法,其模型的建立直到待预测实例进行预测时才 ...
- prototype 与 __proto__
原文:http://rockyuse.iteye.com/blog/1426510 说到prototype,就不得不先说下new的过程. 我们先看看这样一段代码: 1 <script type= ...