bzoj 3595

 

Splay 每个节点维护一个区间。

  1 /**************************************************************
  2     Problem: 3595
  3     User: idy002
  4     Language: C++
  5     Result: Accepted
  6     Time:5428 ms
  7     Memory:56020 kb
  8 ****************************************************************/
  9  
 10 #include <cstdio>
 11 #include <map>
 12 #define N 2000000
 13 using namespace std;
 14  
 15 map<int,int> atob, btoa;
 16 map<int,int> rng_id;
 17  
 18 struct Splay {
 19     int son[N][2], pre[N], xlf[N], xrg[N], siz[N], root, ntot;
 20  
 21     int newnode( int p, int lf, int rg ) {
 22         if( lf>rg ) return 0;
 23         int nd = ++ntot;
 24         son[nd][0] = son[nd][1] = 0;
 25         pre[nd] = p;
 26         xlf[nd] = lf;
 27         xrg[nd] = rg;
 28         siz[nd] = rg-lf+1;
 29         return nd;
 30     }
 31     void update( int nd ) {
 32         siz[nd] = siz[son[nd][0]]+siz[son[nd][1]]+(xrg[nd]-xlf[nd]+1);
 33     }
 34     void rotate( int nd, int d ) {
 35         int p=pre[nd];
 36         int s=son[nd][!d];
 37         int ss=son[s][d];
 38  
 39         son[nd][!d] = ss;
 40         son[s][d] = nd;
 41         if( p ) son[p][nd==son[p][1]]=s;
 42         else root=s;
 43  
 44         pre[nd] = s;
 45         pre[s] = p;
 46         if( ss ) pre[ss] = nd;
 47  
 48         update( nd );
 49         update( s );
 50     }
 51     void splay( int nd, int top=0 ) {
 52         while( pre[nd]!=top ) {
 53             int p=pre[nd];
 54             int nl=nd==son[p][0];
 55             if( pre[p]==top ) {
 56                 rotate( p, nl );
 57             } else {
 58                 int pp=pre[p];
 59                 int pl=p==son[pp][0];
 60                 if( nl==pl ) {
 61                     rotate( pp, pl );
 62                     rotate( p, nl );
 63                 } else {
 64                     rotate( p, nl );
 65                     rotate( pp, pl );
 66                 }
 67             }
 68         }
 69     }
 70     void init( int lf, int rg ) {
 71         ntot = 0;
 72         root = newnode( 0, lf, rg );
 73         rng_id[rg] = root;
 74     }
 75     void make_one( int nd ) {
 76         int lnd, rnd;
 77         splay( nd );
 78         lnd = son[nd][0];
 79         rnd = son[nd][1];
 80         while( son[lnd][1] ) lnd=son[lnd][1];
 81         while( son[rnd][0] ) rnd=son[rnd][0];
 82         if( lnd && rnd ) {
 83             splay( lnd );
 84             splay( rnd, lnd );
 85         } else if( lnd ) {
 86             splay( lnd );
 87         } else if( rnd ) {
 88             splay( rnd );
 89         }
 90     }
 91     void split( int nd, int pos ) {
 92         if( xlf[nd]==xrg[nd] ) return;
 93         make_one( nd );
 94         int lnd, rnd;
 95         lnd = newnode( 0, xlf[nd], pos-1 );
 96         rnd = newnode( 0, pos+1, xrg[nd] );
 97         son[nd][0] = lnd;
 98         son[nd][1] = rnd;
 99         if( lnd ) {
100             pre[lnd] = nd;
101             rng_id[pos-1] = lnd;
102         }
103         if( rnd ) {
104             pre[rnd] = nd;
105             rng_id[xrg[nd]] = rnd;
106         }
107         rng_id[pos] = nd;
108         xlf[nd] = xrg[nd] = pos;
109         update( nd );
110         splay( nd );
111     }
112     void erase( int nd ) {
113         make_one( nd );
114         int p=pre[nd];
115         pre[nd] = 0;
116         if( p ) {
117             son[p][ nd==son[p][1] ] = 0;
118             pre[nd] = 0;
119             update( p );
120             splay( p );
121         } else {
122             root = 0;
123             pre[nd] = 0;
124         }
125     }
126     void push_front( int nn ) {
127         if( !root ) {
128             root = nn;
129             return;
130         }
131         int nd=root;
132         while( son[nd][0] ) nd=son[nd][0];
133         son[nd][0] = nn;
134         pre[nn] = nd;
135         splay( nn );
136     }
137     void push_back( int nn ) {
138         if( !root ) {
139             root = nn;
140             return;
141         }
142         int nd=root;
143         while( son[nd][1] ) nd=son[nd][1];
144         son[nd][1] = nn;
145         pre[nn] = nd;
146         splay(nn);
147     }
148     int rank( int nd ) {
149         int rt=siz[son[nd][0]]+1;
150         int nnd=nd;
151         while( pre[nd] ) {
152             int p=pre[nd];
153             if( nd==son[p][1] ) rt += siz[son[p][0]]+(xrg[p]-xlf[p]+1);
154             nd=p;
155         }
156         splay(nnd);
157         return rt;
158     }
159     int nth( int k ) {
160         int nd=root;
161         while(1) {
162             int ls=siz[son[nd][0]];
163             int lcs=ls+(xrg[nd]-xlf[nd]+1);
164             if( k<=ls ) {
165                 nd = son[nd][0];
166             } else if( k<=lcs ) {
167                 int rt = xlf[nd]+k-ls-1;
168                 splay( nd );
169                 return rt;
170             } else {
171                 k -= lcs;
172                 nd = son[nd][1];
173             }
174         }
175     }
176 }T;
177  
178 int n, m;
179 int main() {
180     scanf( "%d%d", &n, &m );
181     T.init( 1, n );
182     int la = 0;
183     for( int i=1; i<=m; i++ ) {
184         int opt, x, y;
185         scanf( "%d%d", &opt, &x );
186         x -= la;
187         if( opt==1 ) {
188             scanf( "%d", &y );
189             y -= la;
190             int pos = btoa.count(x) ? btoa[x] : x;
191             atob[pos] = y;
192             btoa[y] = pos;
193             int nd= rng_id.lower_bound( pos )->second;
194             T.split( nd, pos );
195             printf( "%d\n", la=T.rank(nd) );
196         } else if( opt==2 ) {
197             int pos = btoa.count(x) ? btoa[x] : x;
198             int nd=rng_id.lower_bound( pos )->second;
199             T.split( nd, pos );
200             printf( "%d\n", la=T.rank(nd) );
201             T.erase( nd );
202             T.push_front( nd );
203         } else if( opt==3 ) {
204             int pos = btoa.count(x) ? btoa[x] : x;
205             int nd = rng_id.lower_bound( pos )->second;
206             T.split( nd, pos );
207             printf( "%d\n", la=T.rank(nd) );
208             T.erase( nd );
209             T.push_back( nd );
210         } else {
211             int pos=T.nth(x);
212             int b = atob.count(pos) ? atob[pos] : pos;
213             printf( "%d\n", la=b );
214         }
215     }
216 }
View Code

 

转载于:https://www.cnblogs.com/idy002/p/4376112.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/420065.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

记录 | 源码编译Arm CPU版FFmpeg

源码编译Arm CPU 版 FFmpeg 1. 安装依赖 包括&#xff1a; ● C/C 编译器 ● GNU make 工具 ● pkg-config ● yasm 汇编器 ● zlib 和 bzip2 压缩库 ● libssl 开发库 ● libx264、libx265、libvpx 和 libopus 开发库 sudo update sudo apt-get install build-essential mak…

前端学习(1420):ajax的post请求

// 引用expess框架 const express require(express); // 处理路径 const path require(path);const bodyParser require(body-parser);// 创建网站服务器 const app express(); app.use(bodyParser.urlencoded()); app.get(/first, (req, res) > {res.send(hello geyao)…

matlab画柱状图斜线,matlab柱状图斜线填充

针对这种情况,MATLAB提供了若干特殊图形绘 制函数。接下来主要介绍特殊图形的绘制方法,主 要图形包括:条形图、区域图、饼状图、柱状图、 离散图、罗盘图、羽毛图、......针对这种情况,MATLAB提供了若干特殊图形绘 制函数。接下来主要介绍特殊图形的绘制方法,主 要图形包括:条形…

程序员要学会读源代码

在“沟通”这个复杂的领域里&#xff0c;写出能让人类领会并理解的连贯段落比敲出几行让解释器或编译器不致于“呕吐”的软件代码要难得多。 这就是为什么——就软件开发而言——所有的文档大概都是很差劲的。而且&#xff0c;由于为人写作比为机器写作要困难得多&#xff0c;文…

前端学习(1421):ajax请求参数的格式类型

ajax.js // 引用expess框架 const express require(express); // 处理路径 const path require(path);const bodyParser require(body-parser);// 创建网站服务器 const app express(); app.use(bodyParser.json()); app.get(/first, (req, res) > {res.send(hello gey…

matlab如何解不等式,如何用MATLAB求解不等式组的所有可能解

太多了吧&#xff1a;No. a b c d1 4 86 17 652 13 96 20 873 5 97 56 544 4 32 14 225 0 91 75 …

sed替换

1. sed可以替换给定的文本中的字符串&#xff0c;可以利用正则表达式进行匹配$ sed s/pattern/replace_string/ file或者$ cat file | sed s/pattern/replace_string/ file使用-i选项&#xff0c;可以将替换的结果应用于原文件&#xff0c;也可以借助重定向来保存文件&#xff…

mysql的中文乱码url,MySQL 中文显示乱码

MySQL 中文显示乱码MySQL 中文显示乱码如果你遇到乱码问题&#xff0c;可以从下面几个问题逐步检查&#xff1a;(1)检查你的文件存储编码是否和 meta 声明的一致&#xff0c;假如你的文件是按照 utf-8 编码存储&#xff0c;但是 meta 却声明为 gb2312 &#xff0c;将导致乱码。…

前端学习(1422):ajax获取服务器端的响应

// 引用expess框架 const express require(express); // 处理路径 const path require(path);const bodyParser require(body-parser);// 创建网站服务器 const app express(); app.use(bodyParser.json()); app.get(/first, (req, res) > {res.send(hello geyao) }) ap…

asp.net 网站开发流程总结

由于这学期要做asp.net的网站开发&#xff0c;导师让我们在前期做详细的计划说明&#xff0c;时间安排。由于网站开发流程不知道&#xff0c;以及需要学什么指示都是盲懂&#xff0c;所以计划安排需在了解大致流程之后才能做出来&#xff0c;一下是询问同学和在网上查阅&#x…

php v9 如何获取超级管理员权限,Windows8.1如何获取超级管理员权限

Win8.1系统下默认管理员权限是有一定权限的&#xff0c;系统内部分文件是无法通过管理员权限进行操作的&#xff0c;需要获取更高级别的超级管理员权限才可&#xff0c;下面我们主要介绍下Win8.1如何利用注册表来获取超级管理员权限。操作步骤&#xff1a;1、首先建立记事本&am…

前端学习(1423):ajax错误处理

ajax.js // 引用expess框架 const express require(express); // 处理路径 const path require(path);const bodyParser require(body-parser);// 创建网站服务器 const app express(); app.use(bodyParser.json()); app.get(/first, (req, res) > {res.send(hello gey…

delphi 中几种多线程操作方式

在了解多线程之前我们先了解一下进程和线程的关系 一个程序至少有一个主进程,一个进程至少有一个线程。 主线程又程为UI线程。 进程和线程的主要差别在于它们是不同的操作系统资源管理方式。进程有独立的地址空间&#xff0c;一个进程崩溃后&#xff0c;在保护模式下不会对其它…

如何 循环 字母 php,PHP-php循环打印a-z字母的疑惑

读一下文档吧&#xff1a;docPHP follows Perls convention when dealing with arithmetic operations on character variables and not Cs. For example, in PHP and Perl $a Z; $a; turns $a into AA, while in C a Z; a; turns a into [ (ASCII value of Z is 90, ASCII v…

前端学习(1424):ajax低版本兼容问题

ajax.js // 引用expess框架 const express require(express); // 处理路径 const path require(path);const bodyParser require(body-parser); const fs require(fs);// 创建网站服务器 const app express(); app.use(bodyParser.json()); app.get(/first, (req, res) &…

4 int.parse方法调用问题和同行评审

1 关于int.parse 的方法调用问题 我们知道int.parse用于数据类型的转换&#xff0c;int.parse(object obj)这个可以将填入的参数obj转换为int的数据类型。 我们在使用int.parse()时回遇到一系列问题。 例如&#xff1a;int.parse(null)会返回异常&#xff0c;另外int.Parse(&qu…

php zip下载损坏,php – 从zip中提取时损坏图像

我尝试使用curl从同一服务器中的一个虚拟主机下载一个zip文件到另一个虚拟主机. Zip文件包含* .php和* .jpg文件.问题是&#xff1a;有时JPG文件会损坏,如下所示&#xff1a;这是我的代码&#xff1a;$out fopen(ABSPATH./templates/default.zip,w);$ch curl_init();curl_set…

前端学习(1425):同步异步概述

// 引用expess框架 const express require(express); // 处理路径 const path require(path);const bodyParser require(body-parser); const fs require(fs);// 创建网站服务器 const app express(); app.use(bodyParser.json()); app.get(/first, (req, res) > {res.…

在 Swift 语言中更好的处理 JSON 数据:SwiftyJSON

SwiftyJSON能够让在Swift语言中更加简便处理JSON数据。 With SwiftyJSON all you have to do is: ?1234let json JSONValue(dataFromNetworking)if let userName json[0]["user"]["name"].string{//Now you got your value}And dont worry about the Op…

前端学习(1426):ajax封装

ajax.js // 引用expess框架 const express require(express); // 处理路径 const path require(path);const bodyParser require(body-parser); const fs require(fs);// 创建网站服务器 const app express(); app.use(bodyParser.json()); app.get(/first, (req, res) &…