POJ 3580(SuperMemo-Splay区间加)[template:Splay V2]
SuperMemo
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, {A1, A2, ... An}.
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 Input The first line contains n (n ≤ 100000). The following n lines describe the sequence. Then follows M (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
Sample Output
Source |
Splay裸题
- #include<cstdio>
- #include<cstring>
- #include<cstdlib>
- #include<algorithm>
- #include<functional>
- #include<iostream>
- #include<cmath>
- #include<cctype>
- #include<ctime>
- using namespace std;
- #define For(i,n) for(int i=1;i<=n;i++)
- #define Fork(i,k,n) for(int i=k;i<=n;i++)
- #define Rep(i,n) for(int i=0;i<n;i++)
- #define ForD(i,n) for(int i=n;i;i--)
- #define RepD(i,n) for(int i=n;i>=0;i--)
- #define Forp(x) for(int p=pre[x];p;p=next[p])
- #define Forpiter(x) for(int &p=iter[x];p;p=next[p])
- #define Lson (x<<1)
- #define Rson ((x<<1)+1)
- #define MEM(a) memset(a,0,sizeof(a));
- #define MEMI(a) memset(a,127,sizeof(a));
- #define MEMi(a) memset(a,128,sizeof(a));
- #define INF (2139062143)
- #define F (100000007)
- #define MAXN (200000+10)
- #define MAXM (100000+10)
- typedef long long ll;
- ll mul(ll a,ll b){return (a*b)%F;}
- ll add(ll a,ll b){return (a+b)%F;}
- ll sub(ll a,ll b){return (a-b+(a-b)/F*F+F)%F;}
- int modF(int a,int b){return (a+a/F*F+F)%F;}
- void upd(ll &a,ll b){a=(a%F+b%F)%F;}
- int n,m;
- int a[MAXN];
- class Splay
- {
- public:
- int father[MAXN],siz[MAXN],n;
- int ch[MAXN][2],val[MAXN];
- bool root[MAXN],rev[MAXN];
- int addv[MAXN],minv[MAXN];
- int roo; //root
- void mem(int _n)
- {
- MEM(father) MEM(siz) MEM(root) MEM(rev) MEM(ch) MEM(val) flag=0; MEM(addv) MEM(minv)
- n=0;
- roo=1;
- build(roo,1,_n,0);root[1]=1;
- }
- void newnode(int &x,int f,int v)
- {
- x=++n;
- father[x]=f;
- val[x]=minv[x]=v;siz[x]=1;
- }
- void build(int &x,int L,int R,int f)
- {
- if (L>R) return ;
- int m=(L+R)>>1;
- newnode(x,f,a[m]);
- build(ch[x][0],L,m-1,x);
- build(ch[x][1],m+1,R,x);
- maintain(x);
- }
- int getkth(int x,int k)
- {
- pushdown(x);
- int t;
- if (ch[x][0]) t=siz[ch[x][0]]; else t=0;
- if (t==k-1) return x;
- else if (t>=k) return getkth(ch[x][0],k);
- else return getkth(ch[x][1],k-t-1);
- }
- void pushdown(int x)
- {
- if (x) if (rev[x])
- {
- swap(ch[x][0],ch[x][1]);
- if (ch[x][0]) rev[ ch[x][0] ]^=1;
- if (ch[x][1]) rev[ ch[x][1] ]^=1;
- rev[x]^=1;
- }
- if (addv[x])
- {
- if (ch[x][0]) addv[ ch[x][0] ]+=addv[x],minv[ ch[x][0] ]+=addv[x],val[ ch[x][0] ]+=addv[x];
- if (ch[x][1]) addv[ ch[x][1] ]+=addv[x],minv[ ch[x][1] ]+=addv[x],val[ ch[x][1] ]+=addv[x];
- addv[x]=0;
- }
- }
- void maintain(int x)
- {
- siz[x]=siz[ch[x][0]]+siz[ch[x][1]]+1;
- minv[x]=val[x];
- if (ch[x][0]) minv[ x ]=min(minv[x],minv[ ch[x][0] ] + addv[x] );
- if (ch[x][1]) minv[ x ]=min(minv[x],minv[ ch[x][1] ] + addv[x] );
- }
- void rotate(int x)
- {
- int y=father[x],kind=ch[y][1]==x;
- pushdown(y); pushdown(x);
- ch[y][kind]=ch[x][!kind];
- if (ch[y][kind]) {
- father[ch[y][kind]]=y;
- }
- father[x]=father[y];
- father[y]=x;
- ch[x][!kind]=y;
- if (root[y])
- {
- root[x]=1;root[y]=0;roo=x;
- }
- else
- {
- ch[father[x]][ ch[father[x]][1]==y ] = x;
- }
- maintain(y);maintain(x);
- }
- void splay(int x)
- {
- while(!root[x])
- {
- int y=father[x];
- int z=father[y];
- if (root[y]) rotate(x);
- else if ( (ch[y][1]==x)^(ch[z][1]==y) )
- {
- rotate(x); rotate(x);
- }
- else
- {
- rotate(y); rotate(x);
- }
- }
- roo=x;
- }
- void splay(int x,int r)
- {
- while(!(father[x]==r))
- {
- int y=father[x];
- int z=father[y];
- if (father[y]==r) rotate(x);
- else if ( (ch[y][1]==x)^(ch[z][1]==y) )
- {
- rotate(x); rotate(x);
- }
- else
- {
- rotate(y); rotate(x);
- }
- }
- }
- void Cut(int a,int b,int c)
- {
- int x=getkth(roo,a),y=getkth(roo,b);
- splay(x);
- splay(y,roo);
- pushdown(x);pushdown(y);
- int z=ch[y][0];
- ch[y][0]=0; maintain(y); maintain(x);
- int u=getkth(roo,c),v=getkth(roo,c+1);
- splay(u);
- splay(v,roo);
- pushdown(u);pushdown(v);
- ch[v][0]=z;father[z]=v;
- maintain(v);maintain(u);
- }
- void Flip(int a,int b)
- {
- int x=getkth(roo,a),y=getkth(roo,b);
- splay(x);
- splay(y,roo);
- pushdown(x);pushdown(y);
- int z=ch[y][0];
- rev[z]^=1;
- maintain(y); maintain(x);
- }
- void Add(int a,int b,int c)
- {
- int x=getkth(roo,a),y=getkth(roo,b);
- splay(x);
- splay(y,roo);
- pushdown(x);pushdown(y);
- int z=ch[y][0];
- addv[z]+=c; val[z]+=c; minv[z]+=c;
- maintain(y); maintain(x);
- }
- int queryMin(int a,int b)
- {
- int x=getkth(roo,a),y=getkth(roo,b);
- splay(x);
- splay(y,roo);
- pushdown(x);pushdown(y);
- int z=ch[y][0];
- maintain(y); maintain(x);
- return minv[z];
- }
- void insert(int a,int P)
- {
- int x=getkth(roo,a),y=getkth(roo,a+1);
- splay(x);
- splay(y,roo);
- pushdown(x);pushdown(y);
- newnode(ch[y][0],y,P);
- maintain(y); maintain(x);
- }
- void Delete(int a,int b)
- {
- int x=getkth(roo,a),y=getkth(roo,b);
- splay(x);
- splay(y,roo);
- pushdown(x);pushdown(y);
- int z=ch[y][0];
- ch[y][0]=0; father[z]=0; maintain(y); maintain(x);
- }
- bool flag;
- void print(int x)
- {
- if (x==0) return ;
- pushdown(x);
- print(ch[x][0]);
- printf("%d ",val[x]);
- print(ch[x][1]);
- }
- }S;
- char s[20];
- int main()
- {
- // freopen("poj3580.in","r",stdin);
- // freopen(".out","w",stdout);
- while(cin>>n)
- {
- For(i,n) scanf("%d",&a[i+1]); a[1]=a[n+2]=INF;
- S.mem(n+2);
- cin>>m;
- For(i,m)
- {
- scanf("%s",s);
- if (s[0]=='A') //ADD
- {
- int x,y,D;
- scanf("%d%d%d",&x,&y,&D);
- S.Add(x,y+2,D);
- } else if (s[0]=='I') { //INSERT
- int x,P;
- scanf("%d%d",&x,&P);
- S.insert(x+1,P);
- } else if (s[0]=='D') { //DELETE
- int x;
- scanf("%d",&x);
- S.Delete(x,x+2);
- } else if (s[0]=='M') { //MIN
- int x,y;
- scanf("%d%d",&x,&y);
- printf("%d\n",S.queryMin(x,y+2));
- } else if (s[3]=='E') { //REVERSE
- int x,y;
- scanf("%d%d",&x,&y);
- S.Flip(x,y+2);
- } else { //REVOLVE
- int x,y,t;
- scanf("%d%d%d",&x,&y,&t);
- t=(t%(y-x+1)+(y-x+1))%(y-x+1);
- if (t) S.Cut(y+2-t-1,y+2,x);
- }
- // S.print(S.roo);cout<<endl;
- }
- }
- return 0;
- }
POJ 3580(SuperMemo-Splay区间加)[template:Splay V2]的更多相关文章
- POJ 3580 SuperMemo (splay tree)
SuperMemo Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 6841 Accepted: 2268 Case Ti ...
- poj 3580 SuperMemo
题目连接 http://poj.org/problem?id=3580 SuperMemo Description Your friend, Jackson is invited to a TV sh ...
- POJ 3580 - SuperMemo - [伸展树splay]
题目链接:http://poj.org/problem?id=3580 Your friend, Jackson is invited to a TV show called SuperMemo in ...
- Splay树(多操作)——POJ 3580 SuperMemo
相应POJ题目:点击打开链接 SuperMemo Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 11309 Accept ...
- 平衡树(Splay):Splaytree POJ 3580 SuperMemo
SuperMemo Description Your friend, Jackson is invited to a TV show called SuperMemo in which ...
- POJ 3580 SuperMemo (FHQ_Treap)
题意:让你维护一个序列,支持以下6种操作: ADD x y d: 第x个数到第y个数加d . REVERSE x y : 将区间[x,y]中的数翻转 . REVOLVE x y t :将区间[x,y] ...
- POJ 3580 SuperMemo 伸展树
题意: 维护一个序列,支持如下几种操作: ADD x y D:将区间\([x,y]\)的数加上\(D\) REVERSE x y:翻转区间\([x,y]\) REVOLVE x y T:将区间\([x ...
- Splay 区间操作
据大佬说,\(Splay\)是序列操作之王.\(Splay\)是一种平衡树,通过伸展(\(Splay\)),在不改变中序遍历的前提下变换根的位置,从而快速的进行序列操作 \(Splay\)最常见的序列 ...
- HDU 1754区间最值 & SPLAY
真是亲切的1754啊..第一道傻逼版的线段树做的是这个,后来学了zkw做的是这个,在后来决定打lrj线段树又打了一遍,如今再用splay和老朋友见面 从上到下依次为:加了读入优化的splay,sp ...
随机推荐
- 让Android软键盘默认进入英文键盘
今天在做一个功能的 时候,需要输入法软键盘弹出后,需要进入英文输入界面. 可以通过设置EditText的输入类型为EMAIL来实现. //将输入法切换到英文 edit.setInput ...
- 【Luogu】P2607骑士(基环树DP)
题目链接 这题……好吧我比着题解打的 题解连接 #include<cstring> #include<cstdio> #include<cstdlib> #incl ...
- BZOJ3507 [Cqoi2014]通配符匹配 【哈希 + 贪心】
题目 几乎所有操作系统的命令行界面(CLI)中都支持文件名的通配符匹配以方便用户.最常见的通配符有两个,一个 是星号(""'),可以匹配0个及以上的任意字符:另一个是问号(&quo ...
- 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 ...
- bzoj 4009 接水果 整体二分
Description 先给出一些盘子, 用路径x-y表示, 有权值 再有Q个询问, 表示水果, 用路径x-y表示 如果盘子是水果的子路径, 可以接住 对于每个水果, 输出可以接住它的盘子的第k小权 ...
- Java程序员最常犯的错误盘点之Top 10
1. 数组转ArrayList 为了实现把一个数组转换成一个ArrayList,很多Java程序员会使用如下的代码: Arrays.asList确实会返回一个ArrayList对象,但是该类是Arra ...
- Install Battery Historian
1. Recommended extra packages for Trusty 14.04 $ sudo apt-get update $ sudo apt-get install \ linux- ...
- Vijos——P1137 组合数
https://vijos.org/p/1137 描述 组合公式 C=N!/(M!*(N-M)!). 问题是求 C 中不同的质因子的个数例如 N=7, M=4. C=7!/(3!*4!)=5040/( ...
- 第4章 CentOS软件安装
一.安装JDK 1.1 卸载旧版JDK 首先,在你的服务器上运行一下更新. yum update 然后,在您的系统上搜索,任何版本的已安装的JDK组件. rpm -qa | grep -E '^ope ...
- 查询和设置mysql事务隔离级别
1.查看当前会话隔离级别 select @@tx_isolation; 2.查看系统当前隔离级别 select @@global.tx_isolation; 3.设置当前会话隔离级别 set sess ...