POJ
来源:互联网 发布:qc qa 软件研发体系 编辑:程序博客网 时间:2024/06/08 10:16
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
51 2 3 4 52ADD 2 4 1MIN 4 5
Sample Output
5
这篇博客用来保存伸展树模板!……看了好久的伸展树,也只是似懂非懂,但至少要会用。因此总结了一个模板。除了题目包含的操作外,还新增了几个操作。详看代码!
#include<iostream>#include<deque>#include<memory.h>#include<stdio.h>#include<map>#include<string.h>#include<algorithm>#include<vector>#include<math.h>#include<stack>#include<queue>#include<set>#include<bitset>#define Key_Value ch[ch[root][1]][0]using namespace std;typedef long long int ll;const int MAXN=300010;const int INF=0x3f3f3f3f;//伸展树模板!int pre[MAXN];//父节点int ch[MAXN][2];//左右两个孩子int size[MAXN];//子树大小int root;//根节点int tot1;//总大小int key[MAXN];//该点的值long long int sum[MAXN];//子树和int minnum[MAXN];//最小值int lazySum[MAXN];//区间累加延迟标记int lazyRev[MAXN];//翻转区间延迟标记int lazyCha[MAXN];//区间修改延迟标记int s[MAXN],tot2;//用于删除操作int A[MAXN];//原始数组int n,q;//新建节点,节点编号,父亲编号,值void NewNode(int &r,int fa,int k){ if(tot2) r=s[tot2--]; else r=++tot1; pre[r]=fa; size[r]=1; key[r]=k; lazyRev[r]=0; minnum[r]=k; lazySum[r]=0; lazyCha[r]=0; sum[r]=0; ch[r][0]=ch[r][1]=0;}//给r为根的子树增加值void Update_Add(int r,int C){ if(r==0) return; lazySum[r]+=C; key[r]+=C; minnum[r]+=C; sum[r]+=(long long)C*size[r];}void Update_Same(int r,int v){ if(!r)return; minnum[r]=v; key[r]=v; sum[r]=v*size[r]; lazyCha[r]=1;}void Update_Rev(int r){ if(!r) return; swap(ch[r][0],ch[r][1]); lazyRev[r]^=1;}void Push_Up(int r){ size[r]=size[ch[r][0]]+size[ch[r][1]]+1; sum[r]=sum[ch[r][0]]+sum[ch[r][1]]+key[r]; minnum[r]=key[r]; if(ch[r][0])minnum[r]=min(minnum[r],minnum[ch[r][0]]); if(ch[r][1])minnum[r]=min(minnum[r],minnum[ch[r][1]]);}void Push_Down(int r){ if(lazyRev[r]){ Update_Rev(ch[r][0]); Update_Rev(ch[r][1]); lazyRev[r]=0; } if(lazySum[r]){ Update_Add(ch[r][0],lazySum[r]); Update_Add(ch[r][1],lazySum[r]); lazySum[r]=0; } if(lazyCha[r]) { Update_Same(ch[r][0],key[r]); Update_Same(ch[r][1],key[r]); lazyCha[r]=0; }}//建树void Build(int &x,int l,int r,int fa){ if(l>r) return; int mid=(l+r)>>1; NewNode(x,fa,A[mid]); Build(ch[x][0],l,mid-1,x); Build(ch[x][1],mid+1,r,x); Push_Up(x);}void Init(){ root=tot1=tot2=0; lazyCha[root]=ch[root][0]=ch[root][1]=pre[root]=size[root]=lazySum[root]=lazyRev[root]=sum[root]=0; key[root]=0; minnum[root]=INF; // NewNode(root,0,INF); // NewNode(ch[root][1],root,INF);//求最小值用 NewNode(root,0,-1); NewNode(ch[root][1],root,-1); Build(Key_Value,1,n,ch[root][1]); Push_Up(ch[root][1]); Push_Up(root);}//0左旋,1右旋void Rotate(int x,int kind){ int y=pre[x]; Push_Down(y); Push_Down(x); ch[y][!kind]=ch[x][kind]; pre[ch[x][kind]]=y; if(pre[y]) ch[pre[y]][ch[pre[y]][1]==y]=x; pre[x]=pre[y]; ch[x][kind]=y; pre[y]=x; Push_Up(y);}//伸展,将r调到goal下void Splay(int r,int goal){ Push_Down(r); while(pre[r]!=goal){ if(pre[pre[r]]==goal){ Push_Down(pre[r]); Push_Down(r); Rotate(r,ch[pre[r]][0]==r); } else{ Push_Down(pre[pre[r]]); Push_Down(pre[r]); Push_Down(r); int y=pre[r]; int kind=ch[pre[y]][0]==y; if(ch[y][kind]==r){ Rotate(r,!kind); Rotate(r,kind); } else{ Rotate(y,kind); Rotate(r,kind); } } } Push_Up(r); if(goal==0) root=r;}void erase(int r)//回收内存{ if(r) { s[++tot2]=r; erase(ch[r][0]); erase(ch[r][1]); }}int Get_Kth(int r,int k){ Push_Down(r); int t=size[ch[r][0]]+1; if(t==k) return r; if(t>k) return Get_Kth(ch[r][0],k); else return Get_Kth(ch[r][1],k-t);}int Get_Min(int r){ Push_Down(r); while(ch[r][0]){ r=ch[r][0]; Push_Down(r); } return r;}int Get_Max(int r){ Push_Down(r); while(ch[r][1]){ r=ch[r][1]; Push_Down(r); } return r;}void Add(int l,int r,int C){ Splay(Get_Kth(root,l),0); Splay(Get_Kth(root,r+2),root); Update_Add(ch[ch[root][1]][0],C); Push_Up(ch[root][1]); Push_Up(root);}void Reverse(int l,int r){ Splay(Get_Kth(root,l),0); Splay(Get_Kth(root,r+2),root); Update_Rev(Key_Value); Push_Up(ch[root][1]); Push_Up(root);}void Revolve(int l,int r,int T)//循环右移{ int len=r-l+1; T=(T%len+len)%len; if(T==0)return; int c=r-T+1;//将区间[c,r]放在[l,c-1]前面 Splay(Get_Kth(root,c),0); Splay(Get_Kth(root,r+2),root); int tmp=Key_Value; Key_Value=0; Push_Up(ch[root][1]); Push_Up(root); Splay(Get_Kth(root,l),0); Splay(Get_Kth(root,l+1),root); Key_Value=tmp; pre[Key_Value]=ch[root][1];//这个不用忘记 Push_Up(ch[root][1]); Push_Up(root);}void Insert(int x,int P)//在第x个数后面插入P{ Splay(Get_Kth(root,x+1),0); Splay(Get_Kth(root,x+2),root); NewNode(Key_Value,ch[root][1],P); Push_Up(ch[root][1]); Push_Up(root);}//在第pos个数后插入tot个数void Insert_Range(int pos,int tot){ for(int i=0;i<tot;i++) scanf("%d",&A[i]); Splay(Get_Kth(root,pos+1),0); Splay(Get_Kth(root,pos+2),root); Build(Key_Value,0,tot-1,ch[root][1]); Push_Up(ch[root][1]); Push_Up(root);}long long int Query_Sum(int l,int r){ Splay(Get_Kth(root,l),0); Splay(Get_Kth(root,r+2),root); return sum[ch[ch[root][1]][0]];}void Delete(int x)//删除第x个数{ Splay(Get_Kth(root,x),0); Splay(Get_Kth(root,x+2),root); erase(Key_Value); pre[Key_Value]=0; Key_Value=0; Push_Up(ch[root][1]); Push_Up(root);}//从第pos个数开始连续删除tot个数void Delete(int pos,int tot){ Splay(Get_Kth(root,pos),0); Splay(Get_Kth(root,pos+tot+1),root); erase(Key_Value); pre[Key_Value]=0; Key_Value=0; Push_Up(ch[root][1]); Push_Up(root);}//从第pos个数连续开始的tot个数修改为cvoid Make_Same(int pos,int tot,int c){ Splay(Get_Kth(root,pos),0); Splay(Get_Kth(root,pos+tot+1),root); Update_Same(Key_Value,c); Push_Up(ch[root][1]); Push_Up(root);}int Query_Min(int l,int r){ Splay(Get_Kth(root,l),0); Splay(Get_Kth(root,r+2),root); return minnum[Key_Value];}//将l,r区间搬到c后void Move(int l,int r,int c){ Splay(Get_Kth(root,l),0); Splay(Get_Kth(root,r+2),root); int tmp=Key_Value; Key_Value=0; Push_Up(ch[root][1]); Push_Up(root); Splay(Get_Kth(root,c+1),0); Splay(Get_Kth(root,c+2),root); Key_Value=tmp; pre[Key_Value]=ch[root][1]; Push_Up(ch[root][1]); Push_Up(root);}int cnt=0;void Print(int r=root){ if(!r)return; Push_Down(r); Print(ch[r][0]); if(cnt>=1&&cnt<=n) { printf("%d",key[r]); if(cnt<n)printf(" "); else printf("\n"); } cnt++; Print(ch[r][1]);}int main(){ char op[20]; int x,y,z; while(scanf("%d",&n)==1) { for(int i=1;i<=n;i++)scanf("%d",&A[i]); Init(); scanf("%d",&q); while(q--) { scanf("%s",op); if(strcmp(op,"ADD")==0) { scanf("%d%d%d",&x,&y,&z); Add(x,y,z); } else if(strcmp(op,"REVERSE")==0) { scanf("%d%d",&x,&y); Reverse(x,y); } else if(strcmp(op,"REVOLVE")==0) { scanf("%d%d%d",&x,&y,&z); Revolve(x,y,z); } else if(strcmp(op,"INSERT")==0) { scanf("%d%d",&x,&y); Insert(x,y); } else if(strcmp(op,"DELETE")==0) { scanf("%d",&x); Delete(x); } else { scanf("%d%d",&x,&y); printf("%d\n",Query_Min(x,y)); } } } return 0;}
- POJ
- poj
- POJ
- POJ
- poj
- poj
- POJ
- POJ
- poj
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- RabbitMQ之Publish/Subscribe
- 文章标题
- Kafka 消息偏移量
- 如何自学Python?
- tomcat服务如何配置vue-router的history模式
- POJ
- C++进阶—>函数重载、重写、重定义的区别
- 第六周第二课--串
- 2017-10-12离线赛总结
- java泛型与c 语言之关系
- 论文投稿技巧方法
- 用JAVA写个简单的聊天室-单人版
- innobackupex备份恢复详解及详细增量备份及恢复脚本
- CSS3简单动画效果与使用列表制作菜单