PS:巨难写,但是稳定 。
Solution
Step1:推式子
容易发现,答案和最大值的位置有关。
记:最大值为 。
记:最大值第一次出现的位置为 ,最后一次出现的位置为 。
那么:
证明:
当 时,,。
当 时:,。
当 时,,。
证毕!
Step2:单点修改对 的影响
沿用题目:修改的位置为 。
下面的 已修改。
记:。
记:
若对 有影响,则:。
此时:对于所有 满足 ,。
若对 有影响,则:。
此时:对于所有 满足 ,。
Step3:在有单点修改的情况下求 和
和这个题差不多。
开一颗线段树,维护的信息是最大值,支持单点修改。
的做法
如果我告诉你在一个线段树的子树中,保证有解,那怎么做?
那么:
如果右子树的最大值比 大,就去右子树,否则就去左子树,这样递归下去,得到的结果就是 。
记: 为这颗子树的深度, 为这颗线段树的深度。
那么,这样的时间复杂度为 。
所以我们这样的时间复杂度是 的。
那么,我们怎样保证有解。
我们可以按照线段树区间查询的方式,把 拆成 个区间。
这些区间我们可以做到有序。
然后我们倒序枚举每个区间,查最大值是否大于 即可。
这一步,也是 的。
的做法
同 的做法,不再赘述。
Step4:维护
结合 Step1 和 Step2,我们发现我们需要一个数据结构,支持区间覆盖,查区间和。
用线段树即可。
这也是 的。
Step5:计算时间复杂度
我们每一步都是 的,所以我们修改一次的时间复杂度也是 的。
那么修改操作的总的时间复杂度是 ,并且稳定。
我们还要花费 的时间,读入,建树。
总时间复杂度:。
Code
Code
#include<cstdio>
#include<algorithm>
#define ll long long
const int N = 1e5+5;
int n;
ll a[N],f[N],g[N];
struct Info{
ll Max,fst,last;
inline Info operator + (const Info x)const{
Info ans;
if(Max == x.Max){
ans.Max = Max;
ans.fst = fst,
ans.last = x.last;
}else ans = Max>x.Max ? (*this) : x;
return ans;
}
};
struct Segment_tree1{
struct Tree{
int lson,rson;
Info S;
}tree[N<<1];
int root,total = 0,cnt;
struct Get_Root{
int rt,l,r;
}R[30];
inline void push_up(int rt){
tree[rt].S = tree[tree[rt].lson].S + tree[tree[rt].rson].S;
}
inline void build(int &rt,int l,int r){
rt = ++total;
if(l == r) return void(tree[rt].S = {a[l],l,l});
int mid = l + r >> 1;
build(tree[rt].lson,l,mid);
build(tree[rt].rson,mid+1,r);
push_up(rt);
}
inline void Update(int rt,int l,int r,int pos,ll v){
if(l == r) return void(tree[rt].S = {v,l,l});
int mid = l + r >> 1;
if(pos<=mid) Update(tree[rt].lson,l,mid,pos,v);
else Update(tree[rt].rson,mid+1,r,pos,v);
push_up(rt);
}
inline void Split(int rt,int treeL,int treeR,int askL,int askR){
if(askL <= treeL && treeR <= askR)
return void(R[++cnt] = {rt,treeL,treeR});
int mid = treeL + treeR >> 1;
if(askR<=mid) return Split(tree[rt].lson,treeL,mid,askL,askR);
if(askL>mid) return Split(tree[rt].rson,mid+1,treeR,askL,askR);
Split(tree[rt].lson,treeL,mid,askL,askR);
Split(tree[rt].rson,mid+1,treeR,askL,askR);
}
inline int AskL(int rt,int l,int r,ll v){
if(l == r) return l;
ll Rmax = tree[tree[rt].rson].S.Max;
int mid = l + r >> 1;
if(Rmax>v) return AskL(tree[rt].rson,mid+1,r,v);
else return AskL(tree[rt].lson,l,mid,v);
}
inline int Lmax(int pos){
cnt = 0;
Split(root,1,n,1,pos);
for(int i = cnt;i;--i)
if(tree[R[i].rt].S.Max > a[pos])
return AskL(R[i].rt,R[i].l,R[i].r,a[pos]);
return 0;
}
inline int AskR(int rt,int l,int r,ll v){
if(l == r) return l;
ll Lmax = tree[tree[rt].lson].S.Max;
int mid = l + r >> 1;
if(Lmax>v) return AskR(tree[rt].lson,l,mid,v);
else return AskR(tree[rt].rson,mid+1,r,v);
}
inline int Rmax(int pos){
cnt = 0;
Split(root,1,n,pos,n);
for(int i = 1;i<=cnt;++i)
if(tree[R[i].rt].S.Max > a[pos])
return AskR(R[i].rt,R[i].l,R[i].r,a[pos]);
return n+1;
}
inline void clear(void){
for(int i = 1;i<=total;++i)
tree[i] = {0,0,{0,0,0}};
root = total = 0;
}
}T;
struct Segment_Tree2{
struct Tree{
int lson,rson;
ll Sum,Same;
inline void push_Same(ll v,ll len){
Sum = v * len;
Same = v;
}
}tree[N<<1];
int root,total = 0;
inline void push_up(int rt){
tree[rt].Sum = tree[tree[rt].lson].Sum + tree[tree[rt].rson].Sum;
}
inline void build(int &rt,int l,int r,ll *a){
rt = ++total;
tree[rt].Same = -1;
if(l == r) return void(tree[rt].Sum = a[l]);
int mid = l + r >> 1;
build(tree[rt].lson,l,mid,a);
build(tree[rt].rson,mid+1,r,a);
push_up(rt);
}
inline void push_down(int rt,int ln,int rn){
if(~tree[rt].Same){
tree[tree[rt].lson].push_Same(tree[rt].Same,ln);
tree[tree[rt].rson].push_Same(tree[rt].Same,rn);
tree[rt].Same = -1;
}
}
inline void Update(int rt,int treeL,int treeR,int askL,int askR,ll v){
if(askL <= treeL && treeR <= askR) return tree[rt].push_Same(v,treeR-treeL+1);
int mid = treeL + treeR >> 1;
push_down(rt,mid-treeL+1,treeR-mid);
if(askL<=mid) Update(tree[rt].lson,treeL,mid,askL,askR,v);
if(askR>mid) Update(tree[rt].rson,mid+1,treeR,askL,askR,v);
push_up(rt);
}
inline ll Ask(int rt,int treeL,int treeR,int askL,int askR){
if(askL <= treeL && treeR <= askR) return tree[rt].Sum;
int mid = treeL + treeR >> 1;
push_down(rt,mid-treeL+1,treeR-mid);
if(askR<=mid) return Ask(tree[rt].lson,treeL,mid,askL,askR);
if(askL>mid) return Ask(tree[rt].rson,mid+1,treeR,askL,askR);
return Ask(tree[rt].lson,treeL,mid,askL,askR) + Ask(tree[rt].rson,mid+1,treeR,askL,askR);
}
inline void clear(void){
for(int i = 1;i<=total;++i)
tree[i] = {0,0,0,0};
root = total = 0;
}
}Tf,Tg;
inline void Solve(void){
std::scanf("%d",&n);
ll S = 0;
for(int i = 1;i<=n;++i){
std::scanf("%lld",a+i);
S += a[i];
}
f[0] = f[n+1] = g[0] = g[n+1] = 0;
for(int i = 1;i<=n;++i) f[i] = std::max(f[i-1],a[i]);
for(int i = n;i;--i) g[i] = std::max(g[i+1],a[i]);
T.build(T.root,1,n);
Tf.build(Tf.root,1,n,f);
Tg.build(Tg.root,1,n,g);
int q;
std::scanf("%d",&q);
while(q--){
int x,v;
std::scanf("%d %d",&x,&v);
S += v,a[x] += v;
T.Update(T.root,1,n,x,a[x]);
Info Mx = T.tree[T.root].S;
int L = T.Lmax(x),R = T.Rmax(x);
if(!L) Tf.Update(Tf.root,1,n,x,R-1,a[x]);
if(R == n+1) Tg.Update(Tg.root,1,n,L+1,x,a[x]);
ll ans = 0;
if(Mx.fst>1) ans += Tf.Ask(Tf.root,1,n,1,Mx.fst-1);
ans += (Mx.last - Mx.fst + 1) * Mx.Max;
if(Mx.last<n) ans += Tg.Ask(Tg.root,1,n,Mx.last+1,n);
ans -= S;
printf("%lld\n",ans);
}
T.clear();
Tf.clear();
Tg.clear();
}
int main(){
int T;
std::scanf("%d",&T);
while(T--) Solve();
return 0;
}