[title]
LOJ
Given a sequence AAA with nnn length and mmm operations, each operation changes an AiA_iAi to kkk, and the modification is independent. After each modification, a monotone sequence B I B_i Bi is required, so that i=1n (A i_B i) 2 sum {i=1} ^ n (A_i-B_i)^ 2_i=1n (A i_B i) 2 is minimized, and the minimum value is output. In particular, BBB can be a fraction, but the answer is to model 998244353998244353998244353.
n≤3×105,m≤105,k,Ai≤109n\leq 3\times 10^5,m\leq 10^5,k,A_i\leq 10^9n≤3×105,m≤105,k,Ai≤109
[Thoughts on Problem Solving]
Consider what to do without modification. As BBB is required to be monotonous, it is not difficult to find that if A i> A i+1A_i> A {i+1} A I > A i+1, B i=B i+1B_{i}=B {i+1} B I B i=B i+1. It is not difficult to find that the BBB takes the average of AAA as the best. Then we use the monotone stack to maintain the shrinking blocks, so that the average value of the blocks is monotonous. Each time we add a new element, we use the top of the stack and merge the new elements until the properties of the shrinking blocks are satisfied.
Another important feature is that the order of mergers does not affect the final result, so consider what impact a change will have on the answer.
First, we need to maintain the monotonous stack on the left and right sides of this location, and then consider merging this number. Next, we will consider a whole block as an element.
When I think about this place, I guess that the answer about the number of stacks is a convex function, and the superposition of functions on both sides is convex, and then I can set three-thirds! (However, this function may have a flat place, so it's cool.)
Considering the left side, if we divide the number of stacks, it is obvious that the larger the number of stacks, the more likely it is to make the stack legal. If the current stack count is not legal (higher or lower than the front), we should merge with the right side to raise (or lower) this section, obviously this can also be divided. Note that the two dichotomies here are independent, that is, it is still illegal for me to complete everything on the left, and dichotomy on the right, so the complexity is a log.
Write to find their own fake, after the right two divide a boundary, the left may not need to merge, so it is still a two divide two divide.
Complexity O (n log 2n) O (n log ^ 2n) O (n log 2 n)
In particular, we may need to maintain the stack rollback.
[Reference Code]
#include<bits/stdc++.h> #define pb push_back using namespace std; typedef long long ll; const int N=1e5+10,mod=998244353; namespace IO { int read() { int ret=0;char c=getchar(); while(!isdigit(c)) c=getchar(); while(isdigit(c)) ret=ret*10+(c^48),c=getchar(); return ret; } void write(int x){if(x>9)write(x/10);putchar(x%10^48);} void writeln(int x){write(x);putchar('\n');} } using namespace IO; namespace Math { int inv[N]; int mul(int x,int y){return 1ll*x*y%mod;} int sqr(int x){return mul(x,x);} int upm(int x){return x>=mod?x-mod:(x<0?x+mod:x);} void up(int &x,int y){x=upm(x+y);} void initmath(){inv[1]=1;for(int i=2;i<N;++i)inv[i]=mod-mul(mod/i,inv[mod%i]);} } using namespace Math; namespace DreamLolita { int n,Q,sum,top1,top2; int a[N],ans[N],st1[N],st2[N],f[N],g[N]; ll s[N];//s should use long long vector<int>oper[N]; struct data { int val,id; data(int _v=0,int _i=0):val(_v),id(_i){} }; vector<data>qr[N]; bool cmp(int l1,int r1,int l2,int r2,int x,int y){return (s[r1]-s[l1]+x)*(r2-l2)>(s[r2]-s[l2]+y)*(r1-l1);} int calc(int l,int r,int x){return mod-(((ll)s[r]-s[l]+x)%mod)*(((ll)s[r]-s[l]+x)%mod)%mod*inv[r-l]%mod;} void init() { n=read();Q=read(); for(int i=1;i<=n;++i) a[i]=read(),s[i]=s[i-1]+a[i],sum=upm(sum+sqr(a[i])); qr[1].pb(data(a[1],0));ans[0]=sum; for(int i=1,x,y;i<=Q;++i) { x=read();y=read();qr[x].pb(data(y,i)); ans[i]=upm(upm(sum+mul(mod-a[x],a[x]))+sqr(y)); //cerr<<ans[i]<<endl; } for(int i=1;i<=n;++i) { //printf("%d %d %d\n",st1[top1-1],st1[top1],cmp(st1[top1-1],st1[top1],st1[top1],i,0,0)); for(;top1 && cmp(st1[top1-1],st1[top1],st1[top1],i,0,0);) oper[i].pb(st1[top1--]); st1[++top1]=i;f[top1]=upm(f[top1-1]+calc(st1[top1-1],st1[top1],0)); reverse(oper[i].begin(),oper[i].end()); //printf("%d %d %d\n",i,top1,f[top1]); } //cerr<<top1; } void solve() { st2[0]=n; for(int i=n;i;--i) { --top1; for(auto j:oper[i]) st1[++top1]=j,f[top1]=upm(f[top1-1]+calc(st1[top1-1],j,0));; if(i^n) { for(;top2 && cmp(i,st2[top2],st2[top2],st2[top2-1],0,0);--top2); st2[++top2]=i;g[top2]=upm(g[top2-1]+calc(i,st2[top2-1],0)); } //printf("%d %d\n",top1,top2); //for(int i=1;i<=top1;++i) printf("%d ",st1[top1]); puts("!"); //for(int i=1;i<=top2;++i) printf("%d ",st2[top2]); puts("?"); for(auto j:qr[i]) { int l=1,r=top1,delta=j.val-a[i],res=0; while(l<=r) { int mid=(l+r)>>1; if(cmp(st1[mid-1],st1[mid],st1[mid],i,0,delta)) r=mid-1; else res=mid,l=mid+1; } //printf("now:%d\n",res); if(!top2 || !cmp(st1[res],i,i,st2[top2-1],delta,0)) up(ans[j.id],upm(upm(calc(st1[res],i,delta)+f[res])+g[top2])); else { int l=1,r=top2-1,ret=0; while(l<=r) { int mid=(l+r)>>1; int L=1,R=res,rep=0; //cerr<<l<<" "<<r<<endl; while(L<=R) { //cerr<<L<<" "<<R<<endl; int Mid=(L+R)>>1; if(cmp(st1[Mid-1],st1[Mid],st1[Mid],st2[mid],0,delta)) R=Mid-1; else rep=Mid,L=Mid+1; } //printf("res:%d %d %d\n",rep,mid,delta); if(cmp(st1[rep],st2[mid],st2[mid],st2[mid-1],delta,0)) r=mid-1; else ret=mid,l=mid+1; } //printf("zzz:%d\n",ret); int L=1,R=res,rep=0; while(L<=R) { int Mid=(L+R)>>1; if(cmp(st1[Mid-1],st1[Mid],st1[Mid],st2[ret],0,delta)) R=Mid-1; else rep=Mid,L=Mid+1; } //printf("res:%d %d %d\n",rep,ret,delta); //cerr<<ans[j.id]<<" "<<rep<<" "<<ret<<endl; up(ans[j.id],upm(upm(calc(st1[rep],st2[ret],delta)+f[rep])+g[ret])); //cerr<<ans[j.id]<<endl; } } } for(int i=0;i<=Q;++i) writeln(ans[i]); } void solution() { initmath();init();solve(); } } int main() { #ifdef Durant_Lee freopen("LOJ3059.in","r",stdin); freopen("LOJ3059.out","w",stdout); #endif DreamLolita::solution(); return 0; }