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 was 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 all 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's 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 are granted a chance to turn to someone else so means when Jackson fee LS difficult in answering a query he is a call to help. You task was to watch the TV show and write a program giving the correct answer to each query in order to assist Jackson WH Enever 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 Wuyi 2 3 4 52ADD 2 4 1MIN 4 5
Sample Output 5
Source POJ founder Monthly contest–2008.04.13, Yao Jinyu |
splay Nude Questions
Recommend the use of stretching trees to solve the problem of series maintenance
#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)/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; Rootvoid 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);p ushdown (x);p ushdown (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);p ushdown (U);p ushdown (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);p ushdown (x);p ushdown (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);p ushdown (x);p ushdown (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);p ushdown (x);p ushdown (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);p ushdown (x);p ushdown (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);p ushdown (x);p ushdown (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;p ushdown (x);p rint (ch[x][0]);p rintf ("%d", val[x]);p rint (ch[x][1]);}} S;char S[20];int Main () {//freopen ("poj3580.in", "R", stdin),//freopen (". Out", "w", stdout) and 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 ') {//minint x,y;scanf ("%d%d", &x,&y);p rintf ("%d\n", S.querymin (x,y+2));} else if (s[3]== ' E ') { Reverseint 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;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ 3580 (supermemo-splay zone Plus) [Template:splay V2]