Poj 3580-SuperMemo Splay
题目:http://poj.org/problem?id=3580
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 13105 | Accepted: 4104 | |
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, {A1, A2, ... An}. Then the host performs a series of operations and queries on the sequence which consists:
- 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}
- 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}
- REVOLVE x y T: rotate sub-sequence {Ax ... Ay} T times. For example, performing "REVOLVE 2 4 2" on {1, 2, 3, 4, 5} results in {1, 3, 4, 2, 5}
- 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}
- DELETE x: delete Ax. For example, performing "DELETE 2" on {1, 2, 3, 4, 5} results in {1, 3, 4, 5}
- 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 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
- 5
- 1
- 2
- 3
- 4
- 5
- 2
- ADD 2 4 1
- MIN 4 5
Sample Output
- 5
Source
- #include<bits/stdc++.h>
- using namespace std;
- #define MAXN 100010
- #define MAXM 100010
- #define INF 1e9
- #define LL long long
- struct node
- {
- LL left,right,mn,val,size;
- }tree[MAXN+MAXM];
- LL a[MAXN+MAXM],father[MAXN+MAXM],rev[MAXN+MAXM],tag[MAXN+MAXM];
- LL read()
- {
- LL s=,fh=;char ch=getchar();
- while(ch<''||ch>''){if(ch=='-')fh=-;ch=getchar();}
- while(ch>=''&&ch<=''){s=s*+(ch-'');ch=getchar();}
- return s*fh;
- }
- void Pushup(LL x)
- {
- LL l=tree[x].left,r=tree[x].right;
- tree[x].size=tree[l].size+tree[r].size+;
- tree[x].mn=min(min(tree[l].mn,tree[r].mn),tree[x].val);
- }
- void Build(LL l,LL r,LL f)
- {
- if(l>r)return;
- LL now=l,last=f;
- if(l==r)
- {
- tree[now].val=tree[now].mn=a[l];father[now]=last;
- tree[now].size=;
- if(l<f)tree[last].left=now;
- else tree[last].right=now;
- }
- LL mid=(l+r)/;
- now=mid;
- Build(l,mid-,mid);Build(mid+,r,mid);
- father[now]=last;tree[now].val=a[mid];
- Pushup(now);
- if(mid<f)tree[last].left=now;
- else tree[last].right=now;
- }
- /*void Pushup(int x)
- {
- int l=tree[x].left,r=tree[x].right;
- tree[x].size=tree[l].size+tree[r].size+1;
- tree[x].mn=min(min(tree[l].mn,tree[r].mn),tree[x].val);
- }*/
- void rotate(LL x,LL &root)
- {
- LL y=father[x],z=father[y];
- if(y==root)root=x;
- else
- {
- if(tree[z].left==y)tree[z].left=x;
- else tree[z].right=x;
- }
- if(tree[y].left==x)
- {
- father[x]=z;father[y]=x;tree[y].left=tree[x].right;tree[x].right=y;father[tree[y].left]=y;
- }
- else
- {
- father[x]=z;father[y]=x;tree[y].right=tree[x].left;tree[x].left=y;father[tree[y].right]=y;
- }
- Pushup(y);Pushup(x);
- }
- void Splay(LL x,LL &root)
- {
- while(x!=root)
- {
- int y=father[x],z=father[y];
- if(y!=root)
- {
- if((tree[y].left==x)^(tree[z].left==y))rotate(x,root);
- else rotate(y,root);
- }
- rotate(x,root);
- }
- }
- void Pushdown(LL x)
- {
- LL l=tree[x].left,r=tree[x].right;
- if(tag[x]!=)
- {
- tag[l]+=tag[x];tag[r]+=tag[x];
- tree[l].val+=tag[x];tree[r].val+=tag[x];
- tree[l].mn+=tag[x];tree[r].mn+=tag[x];
- tag[x]=;
- }
- if(rev[x]!=)
- {
- rev[l]^=;rev[r]^=;rev[x]^=;
- swap(tree[x].left,tree[x].right);
- }
- }
- LL Find(LL root,LL rank)
- {
- Pushdown(root);
- if(tree[tree[root].left].size+==rank)return root;
- else if(rank<=tree[tree[root].left].size)return Find(tree[root].left,rank);
- else return Find(tree[root].right,rank-tree[tree[root].left].size-);
- }
- int main()
- {
- LL n,m,i,rt,SIZE,l,r,add,x,y,z,L,R,T,X,P;
- char fh[];
- n=read();
- tree[].val=INF;a[]=tree[].mn=INF;
- tree[].val=INF;tree[n+].val=INF;
- tree[].mn=INF;tree[n+].mn=INF;
- a[]=INF;a[n+]=INF;
- for(i=;i<=n+;i++)a[i]=read(),tree[i].val=tree[i].mn=INF;
- Build(,n+,);
- SIZE=n+;rt=(+n+)/;
- m=read();
- for(i=;i<=m;i++)
- {
- scanf("\n%s",fh);
- if(fh[]=='A')
- {
- l=read();r=read();add=read();
- x=Find(rt,l);y=Find(rt,r+);
- Splay(x,rt);Splay(y,tree[x].right);
- z=tree[y].left;
- tag[z]+=add;tree[z].val+=add;tree[z].mn+=add;
- }
- else if(fh[]=='R')
- {
- if(fh[]=='E')
- {
- l=read();r=read();
- x=Find(rt,l);y=Find(rt,r+);
- Splay(x,rt);Splay(y,tree[x].right);
- z=tree[y].left;
- rev[z]^=;
- }
- else
- {
- l=read();r=read();T=read();
- L=l;R=r;
- T=(T%(r-l+)+(r-l+))%(r-l+);
- if(T==)continue;
- l=r-T+;
- x=Find(rt,l);y=Find(rt,r+);
- Splay(x,rt);Splay(y,tree[x].right);
- z=tree[y].left;
- father[z]=;tree[y].left=;
- Pushup(y);Pushup(x);
- x=Find(rt,L);y=Find(rt,L+);
- Splay(x,rt);Splay(y,tree[x].right);
- father[z]=y;tree[y].left=z;
- Pushup(y);Pushup(x);
- }
- }
- else if(fh[]=='I')
- {
- X=read();P=read();
- x=Find(rt,X+);y=Find(rt,X+);
- Splay(x,rt);Splay(y,tree[x].right);
- tree[y].left=++SIZE;tree[SIZE].val=P;
- father[SIZE]=y;tree[SIZE].size=;
- tree[SIZE].mn=P;
- Pushup(y);Pushup(x);
- }
- else if(fh[]=='D')
- {
- X=read();
- x=Find(rt,X);y=Find(rt,X+);
- Splay(x,rt);Splay(y,tree[x].right);
- z=tree[y].left;tree[y].left=;
- tree[z].size=;father[z]=;
- //tree[SIZE].val=INF;tree[SIZE].mn=INF;
- Pushup(y);Pushup(x);
- }
- else
- {
- l=read();r=read();
- x=Find(rt,l);y=Find(rt,r+);
- Splay(x,rt);Splay(y,tree[x].right);
- z=tree[y].left;
- printf("%lld\n",tree[z].mn);
- }
- }
- fclose(stdin);
- fclose(stdout);
- return ;
- }
Poj 3580-SuperMemo Splay的更多相关文章
- 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):Splaytree POJ 3580 SuperMemo
SuperMemo Description Your friend, Jackson is invited to a TV show called SuperMemo in which ...
- POJ 3580 SuperMemo (splay tree)
SuperMemo Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 6841 Accepted: 2268 Case Ti ...
- Splay树(多操作)——POJ 3580 SuperMemo
相应POJ题目:点击打开链接 SuperMemo Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 11309 Accept ...
- 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 ...
- POJ 3580:SuperMemo(Splay)
http://poj.org/problem?id=3580 题意:有6种操作,其中有两种之前没做过,就是Revolve操作和Min操作.Revolve一开始想着一个一个删一个一个插,觉得太暴力了,后 ...
- 【POJ 3580】SuperMemo Splay
题意 给定$n$个数,$m$个询问,每次在$[L,R]$区间加上一个数,或者反转一个区间$[L,R]$,或者循环右移区间$[L,R]$共$T$次,或者在第$x$个数后插入一个数$p$,或者删除第$x$ ...
- POJ 3580(SuperMemo-Splay区间加)[template:Splay V2]
SuperMemo Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 11384 Accepted: 3572 Case T ...
随机推荐
- Android 设计随便说说
我曾经搞过应用程序的设计,当时只是读了半本宝典<重构...>,现在看来就这半本九阴真经,收益甚多啊 .再加上这现年工作上的印证,基本上可以拿出喷一下了.当然现在看来当年的项目设计真是很烂了 ...
- 配置wamp开发环境【1】
新手在PHP网站建设时,会使用使用PHP的集成开发环境,这样利于开发和理解!但是做为一个网站开发人员,会独立的配置开发环境这是必须的……因为集成的环境毕竟是固定的,不利于自己的开发.好,废话少说咱现在 ...
- 半质数的个数 csdn 英雄会 高校俱乐部
2·14 情人&元宵节专题:半质数的个数. 题目:质数是大家熟知的概念,我们定义一个半质数的概念:如果一个数恰好是两个质数的乘积(可以相同),则称它为半质数.前几个半质数是 4, 6, 9, ...
- USACO 2.2 Subset Sums 集合(subset)
Description 对于从1到N的连续整集合,能划分成两个子集合,且保证每个集合的数字和是相等的.举个例子,如果N=3,对于{1,2,3}能划分成两个子集合,他们每个的所有数字和是相等的: {3} ...
- jcarousellite 实现图片列表滚动
jcarousel Lite与jcarousel 相比去掉了皮肤的约束,可以自定义的设置前后按钮和选项卡按钮,生成的html更加简洁,文件更小(2k) 下载链接:http://www.gmarwaha ...
- sae的kvdb使用注意
之前没仔细看,原来sae的kvdb使用一定要先调用初始化函数 $kv = new SaeKV(); $kv->init();//必须使用 $kv->set('index', $data);
- 精通 Oracle+Python,第 3 部分:数据解析
进行数据解析的理由不计其数,相关的工具和技巧也同样如此.但是,当您需要用这些数据做一些新的事情时,即使有“合适的”工具可能也是不够的.这一担心对于异类数据源的集成同样存在.用来做这项工作的合适工具迟早 ...
- DATE 使用
DATE 使用 标签(空格分隔): SHELL 使用shell处理文本时经常要使用date,但各种参数经常忘,记录在此: #date 获取当前时间 #date -d "-1 week&quo ...
- HIVE:用外连接替代子查询
由于hive也支持sql,很多人会把hql跟标准sql进行比较,甚至有的时候会直接套用.hive不支持事务也不支持索引,更不支持追加写,但是对于一般的sql都是能够支持的.但是对于一些子查询确实无法支 ...
- 学习Swift -- 拓展
拓展(Extension) 扩展就是向一个已有的类.结构体.枚举类型或者协议类型添加新功能.这包括在没有权限获取原始源代码的情况下扩展类型的能力(即逆向建模).扩展和 Objective-C 中的分类 ...