HDU 4699 Editor (2013多校10,1004题)

时间:2024-05-17 16:35:08

Editor

Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 118    Accepted Submission(s): 38

Problem Description
HDU 4699 Editor (2013多校10,1004题)
Sample Input
8
I 2
I -1
I 1
Q 3
L
D
R
Q 2
Sample Output
2
3
Hint

The following diagram shows the status of sequence after each instruction:
HDU 4699 Editor (2013多校10,1004题)

Source
Recommend
zhuyuanchen520

这题只要用双向链表模拟一下。

查询最大前缀和,相当于维护一个单调队列。

 /* ***********************************************
Author :kuangbin
Created Time :2013/8/22 13:38:35
File Name :F:\2013ACM练习\2013多校10\1004.cpp
************************************************ */ #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
const int MAXN = ; int a[MAXN];
int next[MAXN],pre[MAXN],tot;
int NewNode()
{
next[tot] = -;
pre[tot] = -;
tot++;
return tot-;
}
int root;
int sum[MAXN];
vector<int>vec;
int now;
int n;
void init()
{
tot = ;
root = NewNode();
vec.clear();
n = now = ;
} void I(int x)
{
n++;
int t = NewNode();
a[t] = x;
pre[t] = now;
next[t] = next[now];
if(next[now] != -)pre[next[now]] = t;
next[now] = t;
now = t;
if(n == )
{
sum[n] = x;
vec.push_back(n);
}
else
{
sum[n] = sum[n-] + x;
int sz = vec.size();
if(sum[n] > sum[vec[sz-]])
vec.push_back(n);
}
}
void D()
{
if(n == )return;
int sz = vec.size();
if(vec[sz-] == n)
vec.pop_back();
n--;
int t = pre[now];
next[t] = next[now];
if(next[now] != -)pre[next[now]] = t;
now = t;
}
void L()
{
if(n == )return;
int sz = vec.size();
if(vec[sz-] == n)
vec.pop_back();
now = pre[now];
n--;
}
void R()
{
if(next[now] == -)return;
n++;
now = next[now];
if(n == )
{
sum[n] = a[now];
vec.push_back(n);
}
else
{
int sz = vec.size();
sum[n] = sum[n-] + a[now];
if(sum[n] > sum[vec[sz-]])
vec.push_back(n);
}
} int Q(int k)
{
int sz = vec.size();
if(vec[sz-] <= k)
return sum[vec[sz-]];
int t = upper_bound(vec.begin(),vec.end(),k) - vec.begin();
return sum[vec[t-]];
} int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int M;
int x;
char op[];
while(scanf("%d",&M) == )
{
init();
while(M--)
{
scanf("%s",&op);
if(op[] == 'I')
{
scanf("%d",&x);
I(x);
}
else if(op[] == 'D')
D();
else if(op[] == 'L')
L();
else if(op[] == 'R')
R();
else
{
scanf("%d",&x);
printf("%d\n",Q(x));
}
}
}
return ;
}