3064: Tyvj 1518 CPU监控
Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 305 Solved: 115
[ Submit][ Status]
Description
Bob需要一个程序来监视CPU使用率。这是一个很繁琐的过程,为了让问题更加简单,Bob会慢慢列出今天会在用计算机时做什么事。
Bob会干很多事,除了跑暴力程序看视频之外,还会做出去玩玩和用鼠标乱点之类的事,甚至会一脚踢掉电源……这些事有的会让做这件事的这段时间内CPU使用率增加或减少一个值;有的事还会直接让CPU使用率变为一个值。
当然Bob会询问:在之前给出的事件影响下,CPU在某段时间内,使用率最高是多少。有时候Bob还会好奇地询问,在某段时间内CPU曾经的最高使用率是多少。
为了使计算精确,使用率不用百分比而用一个整数表示。
不保证Bob的事件列表出了莫名的问题,使得使用率为负………………
Input
第一行一个正整数T,表示Bob需要监视CPU的总时间。
然后第二行给出T个数表示在你的监视程序执行之前,Bob干的事让CPU在这段时间内每个时刻的使用率达已经达到了多少。
第三行给出一个数E,表示Bob需要做的事和询问的总数。
接下来E行每行表示给出一个询问或者列出一条事件:
Q X Y:询问从X到Y这段时间内CPU最高使用率
A X Y:询问从X到Y这段时间内之前列出的事件使CPU达到过的最高使用率
P X Y Z:列出一个事件这个事件使得从X到Y这段时间内CPU使用率增加Z
C X Y Z:列出一个事件这个事件使得从X到Y这段时间内CPU使用率变为Z
时间的单位为秒,使用率没有单位。
X和Y均为正整数(X<=Y),Z为一个整数。
从X到Y这段时间包含第X秒和第Y秒。
保证必要运算在有符号32位整数以内。
Output
对于每个询问,输出一行一个整数回答。
Sample Input
-62 -83 -9 -70 79 -78 -31 40 -18 -5
20
A 2 7
A 4 4
Q 4 4
P 2 2 -74
P 7 9 -71
P 7 10 -8
A 10 10
A 5 9
C 1 8 10
Q 6 6
Q 8 10
A 1 7
P 9 9 96
A 5 5
P 8 10 -53
P 6 6 5
A 10 10
A 4 4
Q 1 5
P 4 9 -69
Sample Output
-70
-70
-5
79
10
10
79
79
-5
10
10
HINT
数据分布如下:
第1、2个数据保证T和E均小于等于1000
第3、4个数据保证只有Q类询问
第5、6个数据保证只有C类事件
第7、8个数据保证只有P类事件
全部数据保证T和E均小于等于100000
/**************************************************************
Problem: 3064
User: _vampire_
Language: C++
Result: Accepted
Time:4320 ms
Memory:13388 kb
****************************************************************/
#include<iostream>
#include<cstdio>
#include<cstring>
#define F(i,x,y) for(i=x;i<=y;++i)
#define M -2100000000LL
using namespace std;
#define N 1,1,n
#define mid (l+r)/2
#define L k<<1,l,mid
#define R k<<1|1,mid+1,r
struct S{
int maxn,c,p,pmaxn,pc,pp;
}tr[500000];
int n,a[100001]={0},m;
char ch[10];
inline void update(int k)
{
tr[k].maxn=max(tr[k<<1].maxn,tr[k<<1|1].maxn);
tr[k].pmaxn=max(tr[k<<1].pmaxn,tr[k<<1|1].pmaxn);
}
inline void build(int k,int l,int r)
{
tr[k].p=tr[k].pp=0;
tr[k].c=tr[k].pc=M;
if(l==r)
{
tr[k].maxn=tr[k].pmaxn=a[l];
return ;
}
build(L);build(R);
update(k);
}
inline void pushdown(int k)
{
int i;
F(i,0,1)
{
int root=k*2+i;
tr[root].pmaxn=max(tr[root].pmaxn,max(tr[k].pc,tr[k].pp+tr[root].maxn));
if(tr[root].c==M) tr[root].pp=max(tr[root].pp,tr[k].pp+tr[root].p);
else tr[root].pc=max(tr[root].pc,tr[root].c+tr[k].pp);
if(tr[k].p)
{
if(tr[root].c!=M) tr[root].c+=tr[k].p;
else tr[root].p+=tr[k].p;
tr[root].maxn+=tr[k].p;
}
if(tr[k].c!=M)
{
tr[root].c=tr[root].maxn=tr[k].c;
tr[root].p=0;
}
tr[root].pc=max(tr[root].pc,max(tr[k].pc,tr[root].c));
tr[root].pp=max(tr[root].pp,tr[root].p);
}
tr[k].c=tr[k].pc=M;
tr[k].p=tr[k].pp=0;
}
inline int ask(int k,int l,int r,int x,int y)
{
int maxn=M;
if(l<r) pushdown(k);
if(x<=l&&y>=r)
{
if(ch[0]=='A') return tr[k].pmaxn;
else return tr[k].maxn;
}
if(x<=mid) maxn=max(maxn,ask(L,x,y));
if(y>mid) maxn=max(maxn,ask(R,x,y));
return maxn;
}
inline void change(int k,int l,int r,int x,int y,int z)
{
if(l<r) pushdown(k);
if(x<=l&&y>=r)
{
if(ch[0]=='P')
{
tr[k].p+=z;
tr[k].maxn+=z;
tr[k].pp+=z;
}
else
{
tr[k].c=z;
tr[k].maxn=z;
tr[k].pc=z;
}
tr[k].pmaxn=max(tr[k].maxn,tr[k].pmaxn);
return ;
}
if(x<=mid) change(L,x,y,z);
if(y>mid) change(R,x,y,z);
update(k);
}
inline int in()
{
char c=getchar();int bj=1,result=0;
while (c!='-'&&!(c<='9'&&c>='0')) c=getchar();
if (c=='-') bj=-1,c=getchar();
while (c<='9'&&c>='0') result=result*10+c-'0',c=getchar();
return result*bj;
}
int main()
{
int i,j,x,y,z;
n=in();
F(i,1,n) a[i]=in();
build(N);
m=in();
while(m--)
{
scanf("%s",&ch);
if(ch[0]=='Q'||ch[0]=='A')
{
x=in();y=in();
printf("%d\n",ask(N,x,y));
}
if(ch[0]=='P'||ch[0]=='C')
{
x=in();y=in();z=in();
change(N,x,y,z);
}
}
}