SuperMemo
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 11384   Accepted: 3572
Case Time Limit: 2000MS

Description

Your friend, Jackson is invited to a TV show called SuperMemo in which the participant is told to play a memorizing game. At first, the host tells the participant a sequence of numbers, {A1A2, ... An}.
Then the host performs a series of operations and queries on the sequence which consists:

  1. ADD x y D: Add D to each number in sub-sequence {Ax ... Ay}. For example, performing "ADD 2 4 1" on {1, 2, 3, 4, 5} results in {1, 3, 4, 5, 5}
  2. REVERSE x y: reverse the sub-sequence {Ax ... Ay}. For example, performing "REVERSE 2 4" on {1, 2, 3, 4, 5} results in {1, 4, 3, 2, 5}
  3. REVOLVE x y T: rotate sub-sequence {Ax ... AyT times. For example, performing "REVOLVE 2 4 2" on {1, 2, 3, 4, 5} results in {1, 3, 4, 2, 5}
  4. INSERT x P: insert P after Ax. For example, performing "INSERT 2 4" on {1, 2, 3, 4, 5} results in {1, 2, 4, 3, 4, 5}
  5. DELETE x: delete Ax. For example, performing "DELETE 2" on {1, 2, 3, 4, 5} results in {1, 3, 4, 5}
  6. MIN x y: query the participant what is the minimum number in sub-sequence {Ax ... Ay}. For example, the correct answer to "MIN 2 4" on {1, 2, 3, 4, 5} is 2

To make the show more interesting, the participant is granted a chance to turn to someone else that means when Jackson feels difficult in answering a query he may call you for help. You task is to watch the TV show and write a program giving the correct
answer to each query in order to assist Jackson whenever he calls.

Input

The first line contains (≤ 100000).

The following n lines describe the sequence.

Then follows M (≤ 100000), the numbers of operations and queries.

The following M lines describe the operations and queries.

Output

For each "MIN" query, output the correct answer.

Sample Input

  1. 5
  2. 1
  3. 2
  4. 3
  5. 4
  6. 5
  7. 2
  8. ADD 2 4 1
  9. MIN 4 5

Sample Output

  1. 5

Source

Splay裸题

推荐

url=GdjJ2-V5rYr7EZJpQz711d7wB6NLYL0oC5I797o6KXHMQxyvYAoRYVn8QmFFNWKob_ixgrIfIuveHT52HlBzdqmATaFwTBeD6H7sEyt7uaK">《运用伸展树解决数列维护问题》

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<cstdlib>
  4. #include<algorithm>
  5. #include<functional>
  6. #include<iostream>
  7. #include<cmath>
  8. #include<cctype>
  9. #include<ctime>
  10. using namespace std;
  11. #define For(i,n) for(int i=1;i<=n;i++)
  12. #define Fork(i,k,n) for(int i=k;i<=n;i++)
  13. #define Rep(i,n) for(int i=0;i<n;i++)
  14. #define ForD(i,n) for(int i=n;i;i--)
  15. #define RepD(i,n) for(int i=n;i>=0;i--)
  16. #define Forp(x) for(int p=pre[x];p;p=next[p])
  17. #define Forpiter(x) for(int &p=iter[x];p;p=next[p])
  18. #define Lson (x<<1)
  19. #define Rson ((x<<1)+1)
  20. #define MEM(a) memset(a,0,sizeof(a));
  21. #define MEMI(a) memset(a,127,sizeof(a));
  22. #define MEMi(a) memset(a,128,sizeof(a));
  23. #define INF (2139062143)
  24. #define F (100000007)
  25. #define MAXN (200000+10)
  26. #define MAXM (100000+10)
  27. typedef long long ll;
  28. ll mul(ll a,ll b){return (a*b)%F;}
  29. ll add(ll a,ll b){return (a+b)%F;}
  30. ll sub(ll a,ll b){return (a-b+(a-b)/F*F+F)%F;}
  31. int modF(int a,int b){return (a+a/F*F+F)%F;}
  32. void upd(ll &a,ll b){a=(a%F+b%F)%F;}
  33.  
  34. int n,m;
  35. int a[MAXN];
  36. class Splay
  37. {
  38. public:
  39. int father[MAXN],siz[MAXN],n;
  40. int ch[MAXN][2],val[MAXN];
  41. bool root[MAXN],rev[MAXN];
  42. int addv[MAXN],minv[MAXN];
  43. int roo; //root
  44. void mem(int _n)
  45. {
  46. MEM(father) MEM(siz) MEM(root) MEM(rev) MEM(ch) MEM(val) flag=0; MEM(addv) MEM(minv)
  47. n=0;
  48. roo=1;
  49. build(roo,1,_n,0);root[1]=1;
  50. }
  51. void newnode(int &x,int f,int v)
  52. {
  53. x=++n;
  54. father[x]=f;
  55. val[x]=minv[x]=v;siz[x]=1;
  56. }
  57.  
  58. void build(int &x,int L,int R,int f)
  59. {
  60. if (L>R) return ;
  61. int m=(L+R)>>1;
  62. newnode(x,f,a[m]);
  63. build(ch[x][0],L,m-1,x);
  64. build(ch[x][1],m+1,R,x);
  65. maintain(x);
  66. }
  67. int getkth(int x,int k)
  68. {
  69. pushdown(x);
  70. int t;
  71. if (ch[x][0]) t=siz[ch[x][0]]; else t=0;
  72.  
  73. if (t==k-1) return x;
  74. else if (t>=k) return getkth(ch[x][0],k);
  75. else return getkth(ch[x][1],k-t-1);
  76.  
  77. }
  78.  
  79. void pushdown(int x)
  80. {
  81. if (x) if (rev[x])
  82. {
  83. swap(ch[x][0],ch[x][1]);
  84. if (ch[x][0]) rev[ ch[x][0] ]^=1;
  85. if (ch[x][1]) rev[ ch[x][1] ]^=1;
  86. rev[x]^=1;
  87. }
  88. if (addv[x])
  89. {
  90. if (ch[x][0]) addv[ ch[x][0] ]+=addv[x],minv[ ch[x][0] ]+=addv[x],val[ ch[x][0] ]+=addv[x];
  91. if (ch[x][1]) addv[ ch[x][1] ]+=addv[x],minv[ ch[x][1] ]+=addv[x],val[ ch[x][1] ]+=addv[x];
  92. addv[x]=0;
  93. }
  94. }
  95. void maintain(int x)
  96. {
  97. siz[x]=siz[ch[x][0]]+siz[ch[x][1]]+1;
  98. minv[x]=val[x];
  99. if (ch[x][0]) minv[ x ]=min(minv[x],minv[ ch[x][0] ] + addv[x] );
  100. if (ch[x][1]) minv[ x ]=min(minv[x],minv[ ch[x][1] ] + addv[x] );
  101. }
  102. void rotate(int x)
  103. {
  104. int y=father[x],kind=ch[y][1]==x;
  105.  
  106. pushdown(y); pushdown(x);
  107.  
  108. ch[y][kind]=ch[x][!kind];
  109. if (ch[y][kind]) {
  110. father[ch[y][kind]]=y;
  111. }
  112. father[x]=father[y];
  113. father[y]=x;
  114. ch[x][!kind]=y;
  115. if (root[y])
  116. {
  117. root[x]=1;root[y]=0;roo=x;
  118. }
  119. else
  120. {
  121. ch[father[x]][ ch[father[x]][1]==y ] = x;
  122. }
  123. maintain(y);maintain(x);
  124. }
  125. void splay(int x)
  126. {
  127. while(!root[x])
  128. {
  129. int y=father[x];
  130. int z=father[y];
  131. if (root[y]) rotate(x);
  132. else if ( (ch[y][1]==x)^(ch[z][1]==y) )
  133. {
  134. rotate(x); rotate(x);
  135. }
  136. else
  137. {
  138. rotate(y); rotate(x);
  139. }
  140. }
  141. roo=x;
  142. }
  143. void splay(int x,int r)
  144. {
  145. while(!(father[x]==r))
  146. {
  147. int y=father[x];
  148. int z=father[y];
  149. if (father[y]==r) rotate(x);
  150. else if ( (ch[y][1]==x)^(ch[z][1]==y) )
  151. {
  152. rotate(x); rotate(x);
  153. }
  154. else
  155. {
  156. rotate(y); rotate(x);
  157. }
  158. }
  159. }
  160.  
  161. void Cut(int a,int b,int c)
  162. {
  163. int x=getkth(roo,a),y=getkth(roo,b);
  164. splay(x);
  165. splay(y,roo);
  166. pushdown(x);pushdown(y);
  167. int z=ch[y][0];
  168. ch[y][0]=0; maintain(y); maintain(x);
  169.  
  170. int u=getkth(roo,c),v=getkth(roo,c+1);
  171. splay(u);
  172. splay(v,roo);
  173. pushdown(u);pushdown(v);
  174. ch[v][0]=z;father[z]=v;
  175. maintain(v);maintain(u);
  176.  
  177. }
  178.  
  179. void Flip(int a,int b)
  180. {
  181. int x=getkth(roo,a),y=getkth(roo,b);
  182. splay(x);
  183. splay(y,roo);
  184. pushdown(x);pushdown(y);
  185. int z=ch[y][0];
  186. rev[z]^=1;
  187. maintain(y); maintain(x);
  188. }
  189.  
  190. void Add(int a,int b,int c)
  191. {
  192. int x=getkth(roo,a),y=getkth(roo,b);
  193. splay(x);
  194. splay(y,roo);
  195. pushdown(x);pushdown(y);
  196. int z=ch[y][0];
  197. addv[z]+=c; val[z]+=c; minv[z]+=c;
  198. maintain(y); maintain(x);
  199. }
  200.  
  201. int queryMin(int a,int b)
  202. {
  203. int x=getkth(roo,a),y=getkth(roo,b);
  204. splay(x);
  205. splay(y,roo);
  206. pushdown(x);pushdown(y);
  207. int z=ch[y][0];
  208. maintain(y); maintain(x);
  209. return minv[z];
  210. }
  211.  
  212. void insert(int a,int P)
  213. {
  214. int x=getkth(roo,a),y=getkth(roo,a+1);
  215. splay(x);
  216. splay(y,roo);
  217. pushdown(x);pushdown(y);
  218. newnode(ch[y][0],y,P);
  219. maintain(y); maintain(x);
  220. }
  221. void Delete(int a,int b)
  222. {
  223. int x=getkth(roo,a),y=getkth(roo,b);
  224. splay(x);
  225. splay(y,roo);
  226. pushdown(x);pushdown(y);
  227. int z=ch[y][0];
  228. ch[y][0]=0; father[z]=0; maintain(y); maintain(x);
  229.  
  230. }
  231.  
  232. bool flag;
  233. void print(int x)
  234. {
  235. if (x==0) return ;
  236. pushdown(x);
  237. print(ch[x][0]);
  238. printf("%d ",val[x]);
  239. print(ch[x][1]);
  240. }
  241.  
  242. }S;
  243.  
  244. char s[20];
  245.  
  246. int main()
  247. {
  248. // freopen("poj3580.in","r",stdin);
  249. // freopen(".out","w",stdout);
  250.  
  251. while(cin>>n)
  252. {
  253. For(i,n) scanf("%d",&a[i+1]); a[1]=a[n+2]=INF;
  254. S.mem(n+2);
  255. cin>>m;
  256. For(i,m)
  257. {
  258. scanf("%s",s);
  259. if (s[0]=='A') //ADD
  260. {
  261. int x,y,D;
  262. scanf("%d%d%d",&x,&y,&D);
  263. S.Add(x,y+2,D);
  264. } else if (s[0]=='I') { //INSERT
  265. int x,P;
  266. scanf("%d%d",&x,&P);
  267. S.insert(x+1,P);
  268. } else if (s[0]=='D') { //DELETE
  269. int x;
  270. scanf("%d",&x);
  271. S.Delete(x,x+2);
  272. } else if (s[0]=='M') { //MIN
  273. int x,y;
  274. scanf("%d%d",&x,&y);
  275. printf("%d\n",S.queryMin(x,y+2));
  276. } else if (s[3]=='E') { //REVERSE
  277. int x,y;
  278. scanf("%d%d",&x,&y);
  279. S.Flip(x,y+2);
  280. } else { //REVOLVE
  281. int x,y,t;
  282. scanf("%d%d%d",&x,&y,&t);
  283. t=(t%(y-x+1)+(y-x+1))%(y-x+1);
  284. if (t) S.Cut(y+2-t-1,y+2,x);
  285. }
  286. // S.print(S.roo);cout<<endl;
  287. }
  288.  
  289. }
  290.  
  291. return 0;
  292. }

POJ 3580(SuperMemo-Splay区间加)[template:Splay V2]的更多相关文章

  1. POJ 3580 SuperMemo (splay tree)

    SuperMemo Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 6841   Accepted: 2268 Case Ti ...

  2. poj 3580 SuperMemo

    题目连接 http://poj.org/problem?id=3580 SuperMemo Description Your friend, Jackson is invited to a TV sh ...

  3. POJ 3580 - SuperMemo - [伸展树splay]

    题目链接:http://poj.org/problem?id=3580 Your friend, Jackson is invited to a TV show called SuperMemo in ...

  4. Splay树(多操作)——POJ 3580 SuperMemo

    相应POJ题目:点击打开链接 SuperMemo Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 11309   Accept ...

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

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

  6. POJ 3580 SuperMemo (FHQ_Treap)

    题意:让你维护一个序列,支持以下6种操作: ADD x y d: 第x个数到第y个数加d . REVERSE x y : 将区间[x,y]中的数翻转 . REVOLVE x y t :将区间[x,y] ...

  7. POJ 3580 SuperMemo 伸展树

    题意: 维护一个序列,支持如下几种操作: ADD x y D:将区间\([x,y]\)的数加上\(D\) REVERSE x y:翻转区间\([x,y]\) REVOLVE x y T:将区间\([x ...

  8. Splay 区间操作

    据大佬说,\(Splay\)是序列操作之王.\(Splay\)是一种平衡树,通过伸展(\(Splay\)),在不改变中序遍历的前提下变换根的位置,从而快速的进行序列操作 \(Splay\)最常见的序列 ...

  9. HDU 1754区间最值 & SPLAY

    真是亲切的1754啊..第一道傻逼版的线段树做的是这个,后来学了zkw做的是这个,在后来决定打lrj线段树又打了一遍,如今再用splay和老朋友见面   从上到下依次为:加了读入优化的splay,sp ...

随机推荐

  1. 让Android软键盘默认进入英文键盘

    今天在做一个功能的 时候,需要输入法软键盘弹出后,需要进入英文输入界面. 可以通过设置EditText的输入类型为EMAIL来实现.     //将输入法切换到英文     edit.setInput ...

  2. 【Luogu】P2607骑士(基环树DP)

    题目链接 这题……好吧我比着题解打的 题解连接 #include<cstring> #include<cstdio> #include<cstdlib> #incl ...

  3. BZOJ3507 [Cqoi2014]通配符匹配 【哈希 + 贪心】

    题目 几乎所有操作系统的命令行界面(CLI)中都支持文件名的通配符匹配以方便用户.最常见的通配符有两个,一个 是星号(""'),可以匹配0个及以上的任意字符:另一个是问号(&quo ...

  4. 96. Unique Binary Search Trees(I 和 II)

    Given n, how many structurally unique BST's (binary search trees) that store values 1-n? For example ...

  5. bzoj 4009 接水果 整体二分

    Description 先给出一些盘子, 用路径x-y表示, 有权值 再有Q个询问, 表示水果, 用路径x-y表示 如果盘子是水果的子路径, 可以接住 对于每个水果, 输出可以接住它的盘子的第k小权 ...

  6. Java程序员最常犯的错误盘点之Top 10

    1. 数组转ArrayList 为了实现把一个数组转换成一个ArrayList,很多Java程序员会使用如下的代码: Arrays.asList确实会返回一个ArrayList对象,但是该类是Arra ...

  7. Install Battery Historian

    1. Recommended extra packages for Trusty 14.04 $ sudo apt-get update $ sudo apt-get install \ linux- ...

  8. Vijos——P1137 组合数

    https://vijos.org/p/1137 描述 组合公式 C=N!/(M!*(N-M)!). 问题是求 C 中不同的质因子的个数例如 N=7, M=4. C=7!/(3!*4!)=5040/( ...

  9. 第4章 CentOS软件安装

    一.安装JDK 1.1 卸载旧版JDK 首先,在你的服务器上运行一下更新. yum update 然后,在您的系统上搜索,任何版本的已安装的JDK组件. rpm -qa | grep -E '^ope ...

  10. 查询和设置mysql事务隔离级别

    1.查看当前会话隔离级别 select @@tx_isolation; 2.查看系统当前隔离级别 select @@global.tx_isolation; 3.设置当前会话隔离级别 set sess ...