[BZOJ1861] [Zjoi2006]Book 书架

2024-01-09 12:48
文章标签 book zjoi2006 书架 bzoj1861

本文主要是介绍[BZOJ1861] [Zjoi2006]Book 书架,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

传送门

http://www.lydsy.com/JudgeOnline/problem.php?id=1861

题目大意

支持操作
1.Top S——表示把编号为S的书放在最上面
2.Bottom S——表示把编号为S的书放在最下面
3.Insert S T——T∈{-1,0,1},若编号为S的书上面有X本书,则这条命令表示把这本书放回去后它的上面有X+T本书
4.Ask S——询问编号为S的书的上面目前有多少本书
5.Query S——询问从上面数起的第S本书的编号

constmaxn=100005;
varw:array[-1..maxn,1..5]of longint;x:array[-1..maxn]of longint;i,j,k:longint;n,m,a,b,sum,root:longint;cha,cha1:char;
procedure print(a:longint);
beginif w[a,1]<>-1 then print(w[a,1]);write(w[a,5],' ');if w[a,2]<>-1 then print(w[a,2]);if a=root then writeln;
end;procedure rotate(a,kind:longint);
var b,unkind:longint;
beginb:=w[a,3]; unkind:=kind xor 3;w[a,4]:=w[b,4]; dec(w[b,4],1+w[w[a,kind],4]);w[w[a,unkind],3]:=b; w[b,kind]:=w[a,unkind];w[a,unkind]:=b; w[a,3]:=w[b,3]; w[b,3]:=a;if w[a,3]<>-1thenif w[w[a,3],1]=bthen w[w[a,3],1]:=aelse w[w[a,3],2]:=a;
end;procedure splay(a,goal:longint);
var b,kind,unkind:longint;
beginwhile w[a,3]<>goal dobeginb:=w[a,3]; if w[b,1]=a then kind:=1 else kind:=2; unkind:=kind xor 3;if w[b,3]=goal then rotate(a,kind)elseif w[w[b,3],kind]=bthen begin rotate(b,kind); rotate(a,kind); endelse begin rotate(a,kind); rotate(a,unkind); end;end;if goal=-1 then root:=a;
end;procedure init(a:longint);
begininc(sum); x[a]:=sum;w[sum,1]:=-1; w[sum,2]:=-1; w[sum,3]:=root; w[sum,4]:=1; w[sum,5]:=a; w[root,2]:=sum; inc(w[root,4]);if a=n+1 then w[sum,5]:=-maxlongint;splay(sum,-1);
end;function getmax(a:longint):longint;
var tt:longint;
begintt:=a;while w[tt,2]<>-1 dott:=w[tt,2];exit(tt);
end;procedure top(a:longint);
var tt,fa:longint;
beginsplay(x[a],-1);splay(getmax(w[root,1]),root);w[w[root,1],2]:=w[root,2]; root:=w[root,1]; w[w[root,2],3]:=root; w[root,3]:=-1; w[root,4]:=w[w[root,1],4]+1+w[w[root,2],4];splay(x[0],-1);tt:=w[root,2]; inc(w[root,4]);while tt<>-1 dobegin inc(w[tt,4]); fa:=tt; tt:=w[tt,1]; end;w[x[a],1]:=-1; w[x[a],2]:=-1; w[x[a],3]:=fa; w[x[a],4]:=1; w[x[a],5]:=a; w[fa,1]:=x[a]; splay(x[a],-1);
end;procedure bottom(a:longint);
var tt,fa:longint;
beginsplay(x[a],-1);splay(getmax(w[root,1]),root);w[w[root,1],2]:=w[root,2]; root:=w[root,1]; w[w[root,2],3]:=root; w[root,3]:=-1; w[root,4]:=w[w[root,1],4]+1+w[w[root,2],4];splay(x[n+1],-1);tt:=w[root,1]; inc(w[root,4]);while tt<>-1 dobegin inc(w[tt,4]); fa:=tt; tt:=w[tt,2]; end;w[x[a],1]:=-1; w[x[a],2]:=-1; w[x[a],3]:=fa; w[x[a],4]:=1; w[x[a],5]:=a; w[fa,2]:=x[a]; splay(x[a],-1);
end;procedure ask(a:longint);
beginsplay(x[a],-1);writeln(w[w[root,1],4]-1);
end;procedure query(a:longint);
var tt:longint;
begininc(a); tt:=root;while (w[w[tt,1],4]>=a)or(a>=w[w[tt,1],4]+2) doif a<=w[w[tt,1],4]then tt:=w[tt,1]else begin dec(a,w[w[tt,1],4]+1); tt:=w[tt,2]; end;writeln(w[tt,5]);
end;procedure insert(a,b:longint);
var tt,fa,c:longint;
beginif b=0 then exit;splay(x[a],-1); c:=w[w[root,1],4]+b+1;splay(getmax(w[root,1]),root);w[w[root,1],2]:=w[root,2]; root:=w[root,1]; w[w[root,2],3]:=root; w[root,3]:=-1; w[root,4]:=w[w[root,1],4]+1+w[w[root,2],4];tt:=root;while (w[w[tt,1],4]>=c-1)or(c-1>=w[w[tt,1],4]+2) doif c-1<=w[w[tt,1],4]then tt:=w[tt,1]else begin dec(c,w[w[tt,1],4]+1); tt:=w[tt,2]; end;splay(tt,-1);tt:=w[root,2]; inc(w[root,4]);while tt<>-1 dobegin inc(w[tt,4]); fa:=tt; tt:=w[tt,1]; end;w[x[a],1]:=-1; w[x[a],2]:=-1; w[x[a],3]:=fa; w[x[a],4]:=1; w[x[a],5]:=a; w[fa,1]:=x[a];
end;beginreadln(n,m); sum:=1; root:=1;w[1,1]:=-1; w[1,2]:=-1; w[1,3]:=-1; w[1,4]:=1; w[1,5]:=-maxlongint; x[0]:=1;for i:=1 to n dobegin read(a); init(a); end;init(n+1);readln;//print(root);for i:=1 to m dobeginread(cha);if (cha='B')or(cha='I')then for j:=1 to 5 do read(cha1)elseif (cha='Q')then for j:=1 to 4 do read(cha1)else for j:=1 to 2 do read(cha1);case cha of'T':begin readln(a); top(a); end;'B':begin readln(a); bottom(a); end;'I':begin readln(a,b); insert(a,b); end;'A':begin readln(a); ask(a); end;'Q':begin readln(a); query(a); end;end;//print(root);end;
end.

这篇关于[BZOJ1861] [Zjoi2006]Book 书架的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/587164

相关文章

【ATU Book-i.MX8系列 - TFLite 进阶】 NXP i.MX8M Plus 实现高效 Mobilenet SSD 物体检测

NXP i.MX8M Plus 实现高效 Mobilenet SSD 物体检测 一、概述 在 边缘运算(Edge Computing) 领域中,轻量级的模型扮演着举足轻重的角色。因此,如何在有限硬体资源下实现电脑视觉(Computer vision) 应用是个极具挑战性的课题。特别是在效能与准确度之间寻求平衡。在本节中,我们将深入剖析 深度学习(Deep Learning) 领域中一个备受关注

[linux-005]一个xelatex的book模板

% xelatex book 模板,编译命令:xelatex cjmst.tex,结果是pdf文件。\documentclass[openright,twoside, a4paper, 12pt]{book}\usepackage[UTF8]{ctex}\usepackage{fancyhdr}\pagestyle{fancy}\begin{document}% 书名和作者信息\titl

android书架,可以拖动的图书

需要实现这样的功能:         1)书架的风格可以切换;         2)书籍在整个书架上可以随意拖动,书架可以上下滚动;         3)书籍只能放在书架层上;         4)点击书籍可以动画放大书籍; 依次来解决问题,1)书架的风格可以切换,可以定义几个xml界面

The book

Deep Learning for Coders with Fastai and PyTorch: AI Applications Without a PhD is the book that forms the basis for this course. We recommend reading the book as you complete the course. There’s

出自上帝之手的精妙算法 - Algorithm from THE BOOK (2)

接着 出自上帝之手的精妙算法 - Algorithm from THE BOOK (1) 51. Fixed Parameterized Algorithm for Vertex Cover      http://www.sciencedirect.com/science/article/pii/S0020019097002135 52. Rabin-Kar

出自上帝之手的精妙算法 - Algorithm from THE BOOK (1)

《Proofs from THE BOOK》是一本非常出名的书籍, 收录了不少精妙的证明。         类似地, 在cstheory.stackexchange.com上有人召集大家讨论假如上帝有一本书收集精妙的算法,那么,那本书应该包含一些什么算法呢? "Proof from THE BOOK" 中译本翻译为“来自圣经的证明” 或者 “数学天书中的

一个全栈SpringBoot项目-Book Social Network

一个全栈SpringBoot项目-Book Social Network BSN是一个会员之间交换图书的社交网络平台。图书社交网络是一个全栈应用程序,使用户能够管理他们的图书收藏并与图书爱好者社区互动。它提供的功能包括用户注册、安全电子邮件验证、图书管理(包括创建、更新、共享和归档)、图书借阅(检查可用性)、图书归还功能以及图书归还批准。该应用程序使用 JWT 令牌确保安全性,并遵循 REST

Mac Book M3 Max 64G 如何拿捏 Ollama 本地大模型 Llama3

文章目录 简介安装终端安装界面安装 llama2 7B 测试命令帮助简单问候处理命令退出总结 llama3 8B 测试命令帮助简单问候角色扮演文学模仿文章质量考题问答旅行攻略代码分析命令处理总结 llama 3 70B 测试文学模仿旅行攻略(chatgpt 3.5)(llama 3 70B) 总结 模型库 参考: https://huggingface.co/meta-ll

计算机网络—— book

文章目录 一、概述1.1互联网的核心部分1.电路交换的主要特点2.分组交换的主要特点 1.2.计算机网络的性能1.速率2.带宽3.吞吐量4.时延5.利用率 1.3.计算机网络体系结构协议与划分层次具有五层协议的体系结构(1)应用层 (application layer)(2)运输层 (transport layer)(3)网络层 (network layer)(4)数据链路层 (data l