bzoj3064 Tyvj 1518 CPU监控

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

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

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

 

正解:线段树。

这道吉司机论文题真的是搞得我心力憔悴。。

去学了下线段树的历史最值操作。对于历史最值操作,我们只要蒯一套当前标记就行了,相当于是维护两棵线段树,一棵是当前线段树,一棵是历史线段树。我们可以把历史最值操作理解成线段树每个结点都是一个队列,当前线段树维护的是队尾,历史线段树维护的是队列中的最值。所以我们每次下放时,要先下放历史标记,再下放当前标记。这道题带区间覆盖,所以下放特别麻烦,就不展开说了,看看代码吧。。

 

  1 //It is made by wfj_2048~
  2 #include <algorithm>
  3 #include <iostream>
  4 #include <complex>
  5 #include <cstring>
  6 #include <cstdlib>
  7 #include <cstdio>
  8 #include <vector>
  9 #include <cmath>
 10 #include <queue>
 11 #include <stack>
 12 #include <map>
 13 #include <set>
 14 #define inf (1<<30)
 15 #define N (100010)
 16 #define ls (x<<1)
 17 #define rs (x<<1|1)
 18 #define il inline
 19 #define RG register
 20 #define ll long long
 21 #define File(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)
 22 
 23 using namespace std;
 24 
 25 int nmx[4*N],nadd[4*N],ncov[4*N];
 26 int pmx[4*N],padd[4*N],pcov[4*N];
 27 int n,m;
 28 char s[5];
 29 
 30 il int gi(){
 31     RG int x=0,q=1; RG char ch=getchar();
 32     while ((ch<'0' || ch>'9') && ch!='-') ch=getchar();
 33     if (ch=='-') q=-1,ch=getchar();
 34     while (ch>='0' && ch<='9') x=x*10+ch-48,ch=getchar();
 35     return q*x;
 36 }
 37 
 38 il void pushup(RG int x){ nmx[x]=max(nmx[ls],nmx[rs]),pmx[x]=max(pmx[ls],pmx[rs]); return; }
 39 
 40 il void pushdown(RG int x){
 41     for (RG int i=0,s;i<=1;++i){
 42     s=x<<1|i,pmx[s]=max(pmx[s],max(padd[x]+nmx[s],pcov[x]));
 43     if (ncov[x]==-inf){
 44         nmx[s]+=nadd[x];
 45         if (ncov[s]==-inf) padd[s]=max(padd[s],nadd[s]+padd[x]),nadd[s]+=nadd[x];
 46         else pcov[s]=max(pcov[s],ncov[s]+padd[x]),ncov[s]=nmx[s];
 47     } else{
 48         if (ncov[s]==-inf) padd[s]=max(padd[s],nadd[s]+padd[x]);
 49         else pcov[s]=max(pcov[s],nmx[s]+padd[x]);
 50         nmx[s]=ncov[s]=ncov[x],pcov[s]=max(pcov[s],pcov[x]);
 51     }
 52     }
 53     nadd[x]=padd[x]=0,ncov[x]=pcov[x]=-inf; return;
 54 }
 55 
 56 il void build(RG int x,RG int l,RG int r){
 57     ncov[x]=pcov[x]=-inf; if (l==r){ nmx[x]=gi(),pmx[x]=nmx[x]; return; }
 58     RG int mid=(l+r)>>1; build(ls,l,mid),build(rs,mid+1,r); pushup(x); return;
 59 }
 60 
 61 il void update(RG int x,RG int l,RG int r,RG int xl,RG int xr,RG int v,RG int fg){
 62     if (xl<=l && r<=xr){
 63     if (!fg){
 64         pmx[x]=max(pmx[x],nmx[x]+=v);
 65         if (ncov[x]==-inf) padd[x]=max(padd[x],nadd[x]+=v);
 66         else pcov[x]=max(pcov[x],ncov[x]=nmx[x]);
 67     } else pmx[x]=max(pmx[x],nmx[x]=v),pcov[x]=max(pcov[x],ncov[x]=v);
 68     return;
 69     }
 70     pushdown(x); RG int mid=(l+r)>>1;
 71     if (xr<=mid) update(ls,l,mid,xl,xr,v,fg);
 72     else if (xl>mid) update(rs,mid+1,r,xl,xr,v,fg);
 73     else update(ls,l,mid,xl,mid,v,fg),update(rs,mid+1,r,mid+1,xr,v,fg);
 74     pushup(x); return;
 75 }
 76 
 77 il int query(RG int x,RG int l,RG int r,RG int xl,RG int xr,RG int fg){
 78     if (xl<=l && r<=xr) return fg ? pmx[x] : nmx[x];
 79     pushdown(x); RG int mid=(l+r)>>1;
 80     if (xr<=mid) return query(ls,l,mid,xl,xr,fg);
 81     else if (xl>mid) return query(rs,mid+1,r,xl,xr,fg);
 82     else return max(query(ls,l,mid,xl,mid,fg),query(rs,mid+1,r,mid+1,xr,fg));
 83 }
 84 
 85 il void work(){
 86     n=gi(); build(1,1,n); m=gi();
 87     for (RG int i=1,l,r,z;i<=m;++i){
 88     scanf("%s",s);
 89     if (s[0]=='Q') l=gi(),r=gi(),printf("%d\n",query(1,1,n,l,r,0));
 90     if (s[0]=='A') l=gi(),r=gi(),printf("%d\n",query(1,1,n,l,r,1));
 91     if (s[0]=='P') l=gi(),r=gi(),z=gi(),update(1,1,n,l,r,z,0);
 92     if (s[0]=='C') l=gi(),r=gi(),z=gi(),update(1,1,n,l,r,z,1);
 93     }
 94     return;
 95 }
 96 
 97 int main(){
 98     File("cpu");
 99     work();
100     return 0;
101 }

 

posted @ 2017-04-07 15:07  wfj_2048  阅读(754)  评论(0编辑  收藏  举报