BZOJ #3064. Tyvj 1518 CPU监控(线段树,历史最值)
Solution
我们考虑用线段树维护此题。
先不考虑历史最值。
大概需要维护一种特殊的懒标记 ( x , y ) (x,y) (x,y)表示让区间内所有数 p p p, p = m a x ( p + x , y ) p=max(p+x,y) p=max(p+x,y)。
对于区间加
z
z
z,打一个
(
z
,
−
∞
)
(z,-\infty)
(z,−∞)的标记即可。
对于区间覆盖
z
z
z,打一个
(
−
∞
,
z
)
(-\infty,z)
(−∞,z)的标记即可。
把标记
(
a
,
b
)
(a,b)
(a,b)合并到
(
c
,
d
)
(c,d)
(c,d)时,只需要让
c
′
=
a
+
c
,
d
′
=
m
a
x
(
d
+
a
,
b
)
c'=a+c,d'=max(d+a,b)
c′=a+c,d′=max(d+a,b)即可。
剩下的就是一个线段树维护区间最大值。
现在加上了历史最值,我们要多记录一个区间历史最大值和历史最大值的标记,该标记表示从上一次下传到当前时刻的所有修改中标记的最大贡献,注意这里的标记是有序合并的。这相当于一个分段函数最值,对于标记 ( a , b ) (a,b) (a,b), ( c , d ) (c,d) (c,d),不难发现其最大可以取到 ( m a x ( a , c ) , m a x ( b , d ) ) (max(a,c),max(b,d)) (max(a,c),max(b,d))。
于是按上面的方法维护即可。
时间复杂度 O ( n l g n ) O(nlgn) O(nlgn)。
需要注意 i n f inf inf的取值,很多标记合并可能会爆 i n t int int,太小可能减不完。
Code
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <cassert>
#include <string.h>
//#include <unordered_set>
//#include <unordered_map>
//#include <bits/stdc++.h>
#define MP(A,B) make_pair(A,B)
#define PB(A) push_back(A)
#define SIZE(A) ((int)A.size())
#define LEN(A) ((int)A.length())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define fi first
#define se second
using namespace std;
template<typename T>inline bool upmin(T &x,T y) { return y<x?x=y,1:0; }
template<typename T>inline bool upmax(T &x,T y) { return x<y?x=y,1:0; }
typedef long long ll;
typedef unsigned long long ull;
typedef long double lod;
typedef pair<int,int> PR;
typedef vector<int> VI;
const lod eps=1e-11;
const lod pi=acos(-1);
const int oo=1<<30;
const ll loo=1ll<<62;
const int mods=998244353;
const int MAXN=100005;
const int INF=0x3f3f3f3f;//1061109567
/*--------------------------------------------------------------------*/
inline int read()
{
int f=1,x=0; char c=getchar();
while (c<'0'||c>'9') { if (c=='-') f=-1; c=getchar(); }
while (c>='0'&&c<='9') { x=(x<<3)+(x<<1)+(c^48); c=getchar(); }
return x*f;
}
struct Node
{
ll x,y;
Node(){}
Node(ll x,ll y):x(x),y(y){}
friend Node operator + (Node a,Node b) { return Node(a.x+b.x,max(b.x+a.y,b.y)); }
friend Node operator * (Node a,Node b) { return Node(max(a.x,b.x),max(a.y,b.y)); }
} ntag[MAXN<<2],ptag[MAXN<<2];
ll nmx[MAXN<<2],pmx[MAXN<<2],a[MAXN];
void up(int x)
{
nmx[x]=max(nmx[x<<1],nmx[x<<1|1]);
pmx[x]=max(pmx[x<<1],pmx[x<<1|1]);
}
void build(int x,int l,int r)
{
ntag[x]=ptag[x]=Node(0,-INF);
if (l==r) { nmx[x]=pmx[x]=a[l]; return; }
int mid=(l+r)>>1;
build(x<<1,l,mid);
build(x<<1|1,mid+1,r);
up(x);
}
void down(int x)
{
int ls=x<<1,rs=x<<1|1;
ptag[ls]=ptag[ls]*(ntag[ls]+ptag[x]);
ptag[rs]=ptag[rs]*(ntag[rs]+ptag[x]);
ntag[ls]=ntag[ls]+ntag[x];
ntag[rs]=ntag[rs]+ntag[x];
pmx[ls]=max(pmx[ls],max(nmx[ls]+ptag[x].x,ptag[x].y));
pmx[rs]=max(pmx[rs],max(nmx[rs]+ptag[x].x,ptag[x].y));
nmx[ls]=max(nmx[ls]+ntag[x].x,ntag[x].y);
nmx[rs]=max(nmx[rs]+ntag[x].x,ntag[x].y);
ntag[x]=ptag[x]=Node(0,-INF);
}
void update(int x,int l,int r,int L,int R,Node y)
{
if (l>=L&&r<=R)
{
ntag[x]=ntag[x]+y;
ptag[x]=ptag[x]*ntag[x];
nmx[x]=max(nmx[x]+y.x,y.y);
pmx[x]=max(pmx[x],nmx[x]);
return;
}
down(x);
int mid=(l+r)>>1;
if (R<=mid) update(x<<1,l,mid,L,R,y);
else if (L>mid) update(x<<1|1,mid+1,r,L,R,y);
else update(x<<1,l,mid,L,mid,y),update(x<<1|1,mid+1,r,mid+1,R,y);
up(x);
}
ll query(int x,int l,int r,int L,int R,int opt)
{
if (l>=L&&r<=R) return opt?pmx[x]:nmx[x];
down(x);
int mid=(l+r)>>1;
if (R<=mid) return query(x<<1,l,mid,L,R,opt);
else if (L>mid) return query(x<<1|1,mid+1,r,L,R,opt);
else return max(query(x<<1,l,mid,L,mid,opt),query(x<<1|1,mid+1,r,mid+1,R,opt));
}
signed main()
{
int n=read();
for (int i=1;i<=n;i++) a[i]=read();
build(1,1,n);
int Case=read();
while (Case--)
{
char st[5]; scanf("%s",st);
int x=read(),y=read(),z;
if (st[0]=='Q') printf("%lld\n",query(1,1,n,x,y,0));
if (st[0]=='A') printf("%lld\n",query(1,1,n,x,y,1));
if (st[0]=='P') z=read(),update(1,1,n,x,y,Node(z,-INF));
if (st[0]=='C') z=read(),update(1,1,n,x,y,Node(-INF,z));
}
return 0;
}