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
 
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:

 
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 ;
}

最新文章

  1. Oracle数据库操作知道
  2. [转]为何需要调用“super viewDidLoad
  3. Linux由管道组成的值得学习的命令
  4. bzoj 1264 [AHOI2006]基因匹配Match(DP+树状数组)
  5. UVA 10340 (13.08.25)
  6. python全栈开发day115、116-websocket、websocket原理、websocket加解密、简单问答机器人实现
  7. linux学习笔记整理(七)
  8. C# 用 WebClient 的 Post 方法向 WebServer 传输数据
  9. 【BZOJ3811】玛里苟斯(线性基)
  10. HDU 4859 海岸线(最小割+最大独立点权变形)
  11. JS函数入门
  12. swift 带有下划线的UIbutton
  13. 我的第一篇博文,开启我的Java程序人生之旅!
  14. [Canvas]更多的球
  15. python3新特点
  16. ssh命令远程登录
  17. DHTML参考手册
  18. UVA 1213 Sum of Different Primes
  19. 数据结构&amp;字符串:字典树
  20. linux命令(44):date命令

热门文章

  1. 2017-2018 ACM-ICPC, NEERC, Southern Subregional Contest, qualification stage
  2. URAL题解三
  3. int(long) 类型转换为char
  4. xshell 映射带跳板机服务器的端口到本地
  5. 2.rabbitmq 工作队列
  6. 以太坊go-ethereum客户端查询交易列表(二)
  7. 以太坊go-ethereum客户端JSON-RPC API调用(一)
  8. 对于JAVA多线程卖票小程序的理解
  9. python平均值和加权平均值
  10. CentOS 7 kibana安装配置