浅谈\(splay\):https://www.cnblogs.com/AKMer/p/9979592.html

浅谈\(fhq\)_\(treap\):https://www.cnblogs.com/AKMer/p/9981274.html

题目传送门:http://poj.org/problem?id=3580

对于\(REVOLVE\)操作,就相当于把\([r-t+1,r]\)剪切下来粘贴到\([l-1,l]\)中间,其余的操作都是平衡树较为模板的操作了。记得把零号点的最小值赋成\(inf\),要不然空儿子更新父亲信息就会出错。下传标记记得不要传给空儿子,否则会加爆。

时间复杂度:\(O((n+m)logn)\)

空间复杂度:\(O(n)\)

\(splay\)代码如下:

  1. #include <cstdio>
  2. #include <iostream>
  3. #include <algorithm>
  4. using namespace std;
  5. const int maxn=2e5+5,inf=2e9;
  6. int n,m;
  7. char s[10];
  8. int a[maxn];
  9. int read() {
  10. int x=0,f=1;char ch=getchar();
  11. for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
  12. for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
  13. return x*f;
  14. }
  15. struct Splay {
  16. int tot,root;
  17. bool rev[maxn];
  18. int add[maxn],val[maxn],mn[maxn];
  19. int siz[maxn],fa[maxn],son[maxn][2];
  20. int newnode(int v) {
  21. siz[++tot]=1;
  22. val[tot]=mn[tot]=v;
  23. return tot;
  24. }
  25. void update(int p) {
  26. siz[p]=siz[son[p][0]]+1+siz[son[p][1]];
  27. mn[p]=min(min(mn[son[p][0]],mn[son[p][1]]),val[p]);
  28. }
  29. void build(int l,int r,int lst) {
  30. if(r<l)return;
  31. if(l==r) {
  32. siz[l]=1,val[l]=mn[l]=a[l];
  33. fa[l]=lst,son[lst][l>lst]=l;
  34. return;
  35. }
  36. int mid=(l+r)>>1;
  37. val[mid]=a[mid];
  38. fa[mid]=lst,son[lst][mid>lst]=mid;
  39. build(l,mid-1,mid),build(mid+1,r,mid);
  40. update(mid);
  41. }
  42. void prepare() {
  43. mn[0]=inf,tot=n+2,build(1,n+2,0),root=(n+3)>>1;
  44. }
  45. void make_rev_tag(int p) {
  46. if(!p)return;
  47. rev[p]^=1,swap(son[p][0],son[p][1]);
  48. }
  49. void make_add_tag(int p,int v) {
  50. if(!p)return;
  51. val[p]+=v,add[p]+=v,mn[p]+=v;
  52. }
  53. void push_down(int p) {
  54. if(rev[p]) {
  55. make_rev_tag(son[p][0]);
  56. make_rev_tag(son[p][1]);
  57. rev[p]=0;
  58. }
  59. if(add[p]) {
  60. make_add_tag(son[p][0],add[p]);
  61. make_add_tag(son[p][1],add[p]);
  62. add[p]=0;
  63. }
  64. }
  65. int find(int u,int rk) {
  66. push_down(u);
  67. if(siz[son[u][0]]+1==rk)return u;
  68. if(siz[son[u][0]]>=rk)return find(son[u][0],rk);
  69. return find(son[u][1],rk-siz[son[u][0]]-1);
  70. }
  71. int t(int u) {
  72. return son[fa[u]][1]==u;
  73. }
  74. void rotate(int u) {
  75. int ret=t(u),f=fa[u],s=son[u][ret^1];
  76. son[f][ret]=s;if(s)fa[s]=f;son[u][ret^1]=f;
  77. fa[u]=fa[f];if(fa[f])son[fa[f]][t(f)]=u;
  78. fa[f]=u;update(f),update(u);
  79. }
  80. void splay(int goal,int u) {
  81. int tmp=fa[goal];
  82. while(fa[u]!=tmp) {
  83. if(fa[fa[u]]!=tmp) {
  84. if(t(fa[u])==t(u))rotate(fa[u]);
  85. else rotate(u);
  86. }
  87. rotate(u);
  88. }
  89. if(!tmp)root=u;
  90. }
  91. void ADD(int l,int r,int v) {
  92. int u1=find(root,l-1),u2=find(root,r+1);
  93. splay(root,u1),splay(son[u1][1],u2);
  94. make_add_tag(son[u2][0],v);
  95. update(u2),update(u1);
  96. }
  97. void REVERSE(int l,int r) {
  98. int u1=find(root,l-1),u2=find(root,r+1);
  99. splay(root,u1),splay(son[u1][1],u2);
  100. make_rev_tag(son[u2][0]);
  101. }
  102. int cut(int l,int r) {
  103. int u1=find(root,l-1),u2=find(root,r+1);
  104. splay(root,u1),splay(son[u1][1],u2);
  105. int tmp=son[u2][0];
  106. son[u2][0]=fa[tmp]=0;
  107. update(u2),update(u1);
  108. return tmp;
  109. }
  110. void paste(int pos,int node) {
  111. int u1=find(root,pos-1),u2=find(root,pos);
  112. splay(root,u1),splay(son[u1][1],u2);
  113. fa[node]=u2,son[u2][0]=node;
  114. update(u2),update(u1);
  115. }
  116. int query(int l,int r) {
  117. int u1=find(root,l-1),u2=find(root,r+1);
  118. splay(root,u1),splay(son[u1][1],u2);
  119. return mn[son[u2][0]];
  120. }
  121. }T;
  122. int main() {
  123. n=read();
  124. for(int i=2;i<=n+1;i++)
  125. a[i]=read();
  126. a[1]=a[n+2]=inf;
  127. T.prepare();m=read();
  128. for(int i=1;i<=m;i++) {
  129. scanf("%s",s+1);
  130. if(s[1]=='A') {
  131. int l=read()+1,r=read()+1,v=read();
  132. T.ADD(l,r,v);
  133. }
  134. if(s[1]=='R') {
  135. int l=read()+1,r=read()+1;
  136. if(s[4]=='E')T.REVERSE(l,r);
  137. else {
  138. int t=read()%(r-l+1);
  139. int tmp=T.cut(r-t+1,r);
  140. T.paste(l,tmp);
  141. }
  142. }
  143. if(s[1]=='I') {
  144. int pos=read()+1,v=read();
  145. T.paste(pos+1,T.newnode(v));
  146. }
  147. if(s[1]=='D') {
  148. int pos=read()+1;
  149. T.cut(pos,pos);
  150. }
  151. if(s[1]=='M') {
  152. int l=read()+1,r=read()+1;
  153. printf("%d\n",T.query(l,r));
  154. }
  155. }
  156. return 0;
  157. }

\(fhq\)_\(treap\)版代码如下:

  1. #include <ctime>
  2. #include <cstdio>
  3. #include <algorithm>
  4. using namespace std;
  5. typedef pair<int,int> pii;
  6. const int maxn=2e5+5,inf=2e9;
  7. int n,m;
  8. char s[10];
  9. int a[maxn];
  10. int read() {
  11. int x=0,f=1;char ch=getchar();
  12. for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
  13. for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
  14. return x*f;
  15. }
  16. struct fhq_treap {
  17. int tot,root;
  18. bool rev[maxn];
  19. int val[maxn],mn[maxn],add[maxn];
  20. int siz[maxn],fix[maxn],son[maxn][2];
  21. int newnode(int v) {
  22. siz[++tot]=1;
  23. fix[tot]=rand();
  24. val[tot]=mn[tot]=v;
  25. return tot;
  26. }
  27. void update(int p) {
  28. siz[p]=siz[son[p][0]]+1+siz[son[p][1]];
  29. mn[p]=min(min(mn[son[p][0]],mn[son[p][1]]),val[p]);
  30. }
  31. void build(int l,int r,int lst) {
  32. if(r<l)return;
  33. if(l==r) {
  34. siz[l]=1,fix[l]=rand();
  35. son[lst][l>lst]=l;
  36. val[l]=mn[l]=a[l];
  37. return;
  38. }
  39. int mid=(l+r)>>1;
  40. val[mid]=a[mid],fix[mid]=rand();
  41. son[lst][mid>lst]=mid;
  42. build(l,mid-1,mid),build(mid+1,r,mid);
  43. update(mid);
  44. }
  45. void prepare() {
  46. mn[0]=inf,tot=n,build(1,n,0),root=(n+1)>>1;
  47. }
  48. void make_add_tag(int u,int v) {
  49. if(!u)return;
  50. val[u]+=v,add[u]+=v,mn[u]+=v;
  51. }
  52. void make_rev_tag(int u) {
  53. if(!u)return;
  54. rev[u]^=1,swap(son[u][0],son[u][1]);
  55. }
  56. void push_down(int u) {
  57. if(add[u]) {
  58. make_add_tag(son[u][0],add[u]);
  59. make_add_tag(son[u][1],add[u]);
  60. add[u]=0;
  61. }
  62. if(rev[u]) {
  63. make_rev_tag(son[u][0]);
  64. make_rev_tag(son[u][1]);
  65. rev[u]=0;
  66. }
  67. }
  68. pii split(int u,int rk) {
  69. if(!rk)return make_pair(0,u);
  70. if(rk==siz[u])return make_pair(u,0);
  71. push_down(u);
  72. if(siz[son[u][0]]>=rk) {
  73. pii tmp=split(son[u][0],rk);
  74. son[u][0]=tmp.second,update(u);
  75. return make_pair(tmp.first,u);
  76. }
  77. else {
  78. pii tmp=split(son[u][1],rk-siz[son[u][0]]-1);
  79. son[u][1]=tmp.first,update(u);
  80. return make_pair(u,tmp.second);
  81. }
  82. }
  83. int merge(int a,int b) {
  84. if(!a||!b)return a+b;
  85. push_down(a),push_down(b);
  86. if(fix[a]>fix[b])return son[a][1]=merge(son[a][1],b),update(a),a;
  87. else return son[b][0]=merge(a,son[b][0]),update(b),b;
  88. }
  89. void ADD(int l,int r,int v) {
  90. pii tmp1=split(root,r);
  91. pii tmp2=split(tmp1.first,l-1);
  92. make_add_tag(tmp2.second,v);
  93. root=merge(merge(tmp2.first,tmp2.second),tmp1.second);
  94. }
  95. void REVERSE(int l,int r) {
  96. pii tmp1=split(root,r);
  97. pii tmp2=split(tmp1.first,l-1);
  98. make_rev_tag(tmp2.second);
  99. root=merge(merge(tmp2.first,tmp2.second),tmp1.second);
  100. }
  101. int cut(int l,int r) {
  102. pii tmp1=split(root,r);
  103. pii tmp2=split(tmp1.first,l-1);
  104. root=merge(tmp2.first,tmp1.second);
  105. return tmp2.second;
  106. }
  107. void paste(int pos,int node) {
  108. pii tmp=split(root,pos-1);
  109. root=merge(merge(tmp.first,node),tmp.second);
  110. }
  111. int query(int l,int r) {
  112. pii tmp1=split(root,r);
  113. pii tmp2=split(tmp1.first,l-1);
  114. int res=mn[tmp2.second];
  115. root=merge(merge(tmp2.first,tmp2.second),tmp1.second);
  116. return res;
  117. }
  118. }T;
  119. int main() {
  120. srand(time(0)),n=read();
  121. for(int i=1;i<=n;i++)
  122. a[i]=read();
  123. T.prepare();m=read();
  124. for(int i=1;i<=m;i++) {
  125. scanf("%s",s+1);
  126. if(s[1]=='A') {
  127. int l=read(),r=read(),v=read();
  128. T.ADD(l,r,v);
  129. }
  130. if(s[1]=='R') {
  131. int l=read(),r=read();
  132. if(s[4]=='E')T.REVERSE(l,r);
  133. else {
  134. int t=read()%(r-l+1);
  135. int tmp=T.cut(r-t+1,r);
  136. T.paste(l,tmp);
  137. }
  138. }
  139. if(s[1]=='I') {
  140. int pos=read(),v=read();
  141. T.paste(pos+1,T.newnode(v));
  142. }
  143. if(s[1]=='D') {
  144. int pos=read();
  145. T.cut(pos,pos);
  146. }
  147. if(s[1]=='M') {
  148. int l=read(),r=read();
  149. printf("%d\n",T.query(l,r));
  150. }
  151. }
  152. return 0;
  153. }

POJ3580:SuperMemo的更多相关文章

  1. POJ 3580:SuperMemo(Splay)

    http://poj.org/problem?id=3580 题意:有6种操作,其中有两种之前没做过,就是Revolve操作和Min操作.Revolve一开始想着一个一个删一个一个插,觉得太暴力了,后 ...

  2. 【无旋式treap】例题

    [bzoj3223]文艺平衡树 Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[ ...

  3. 【无旋 treap】例题

    [bzoj3223]文艺平衡树 Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[ ...

  4. java web 开发三剑客 -------电子书

    Internet,人们通常称为因特网,是当今世界上覆盖面最大和应用最广泛的网络.根据英语构词法,Internet是Inter + net,Inter-作为前缀在英语中表示“在一起,交互”,由此可知In ...

  5. 所有selenium相关的库

    通过爬虫 获取 官方文档库 如果想获取 相应的库 修改对应配置即可 代码如下 from urllib.parse import urljoin import requests from lxml im ...

  6. 平衡树(Splay):Splaytree POJ 3580 SuperMemo

    SuperMemo         Description Your friend, Jackson is invited to a TV show called SuperMemo in which ...

  7. poj3580 SuperMemo (Splay+区间内向一个方向移动)

    Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 13550   Accepted: 4248 Case Time Limit: ...

  8. 【POJ3580】【splay版】SuperMemo

    Description Your friend, Jackson is invited to a TV show called SuperMemo in which the participant i ...

  9. 【POJ3580】【块状链表】SuperMemo

    Description Your friend, Jackson is invited to a TV show called SuperMemo in which the participant i ...

随机推荐

  1. openwrt patch

    一: 这几天使用一款电信的4G网卡,发现了一些问题,所以决定打个pitch来解决问题,顺便把patch的生成与使用学习一下 二:安装patch的管理工具quilt 1. sudo apt-get in ...

  2. 资源重复 uac.res resource kept(转)

    一般按照网上流传的方法制作UAC.RES放到DELPHI程序里面来就可以出现盾牌.但是某些DELPHI的项目在添加了UAC.RES后编译会报错,例如: [DCC Error] E2161 Warnin ...

  3. AWR元数据的迁移或导入到其它数据库

    我们能够将AWR元数据迁移(导入)到其它数据库.低版本号的导入到高版本号,再用高版本号的数据库生成AWR报告,也能使用一些新特性,如 SQL ordered by Physical Reads (Un ...

  4. 你的JVM还好吗?GC初步诊断

    你的JVM还好吗?GC初步诊断   阿飞的博客   JVM的GC机制绝对是很多程序员的福音,它让Java程序员省去了自己回收垃圾的烦恼.从而可以把大部分时间专注业务身上,大大提高了业务开发速度,让产品 ...

  5. 22 nginx配置与集群

    一:编译nginx ,并配置 Cd /app/pcre-8.12 ./configure Make && make install Cd nginx-1.2.7 ./configure ...

  6. ASP.NET MVC 相关的社群与讨论区

    ASP.NET MVC 官方论坛  http://forums.asp.net/1146.aspx 台湾微软MSDN论坛 --- ASP.NET 与 AJAX(ASP.NET AND AJAX)讨论区 ...

  7. jquery实现重置

    $('#reset').click(function(){ $('#info_frm')[0].reset(); });

  8. 跳过权限检查,强制修改mysql密码

    windows: 1,停止MYSQL服务,CMD打开DOS窗口,输入 net stop mysql 2,在CMD命令行窗口,进入MYSQL安装目录 比如E:\Program Files\MySQL\M ...

  9. 矩阵十题【六】 poj3070 Fibonacci

    题目链接:http://poj.org/problem? id=3070 题目大意:给定n和10000,求第n个Fibonacci数mod 10000 的值,n不超过2^31. 结果保留四位数字. 非 ...

  10. Method invoke 方法

    这个问题要看明白源码才能解决