HDU-4699 Editor 数据结构维护

时间:2023-01-28 06:41:18

  题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4699

  题意:开始有一个光标,每次有5中操作:1,光标当前位置插入一个数,2,光标当前位置删除一个,3,光标向左移一位,4,光标向右移动一位,5,询问前面的数列的最大前缀和。

  由于每次删除的数都是在当前的光标位置,而且询问的前缀和都是在光标前面的位置,因此问题简化了很多,否则要用Splay tree搞了。我们可以直接用一个链表或者两个栈来维护光标以前的最大前缀和,然后直接模拟操作就可以了。。。

 //STATUS:C++_AC_671MS_10824KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
//typedef __int64 LL;
//typedef unsigned __int64 ULL;
//const
const int N=;
const int INF=0x3f3f3f3f;
const int MOD=,STA=;
//const LL LNF=1LL<<60;
const double EPS=1e-;
const double OO=1e15;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End int sta1[N][],sta2[N];
int n; int main()
{
// freopen("in.txt","r",stdin);
int i,j,a,top1,top2;
char op[];
while(~scanf("%d",&n))
{
top1=top2=;
sta1[][]=-INF;
while(n--){
scanf("%s",op);
if(op[]=='I'){
scanf("%d",&a);
sta1[++top1][]=a;
sta1[top1][]=sta1[top1-][]+a;
sta1[top1][]=Max(sta1[top1][],sta1[top1-][]);
}
else if(op[]=='D'){
top1--;
}
else if(op[]=='L'){
if(top1<)continue;
sta2[++top2]=sta1[top1--][];
}
else if(op[]=='R'){
if(top2<)continue;
a=sta2[top2--];
sta1[++top1][]=a;
sta1[top1][]=sta1[top1-][]+a;
sta1[top1][]=Max(sta1[top1][],sta1[top1-][]);
}
else {
scanf("%d\n",&a);
printf("%d\n",sta1[a][]);
}
}
}
return ;
}