P5665 [CSP-S2019] 划分

/*
sum[i]-sum[pre[i]]>=sum[pre[i]]-sum[pre[pre[i]]] 
sum[i] 一定满足  ->    sum[i]>= 2*sum[pre[i]]-sum[pre[pre[i]]] =val(i)
换言之 只要满足 sumi>=RHS就一定是 不减的合法区间
设dp[i] 以i结尾的抹一段 总的贡献
-> 因为prei 是最靠后满足 sumi>=RHS的位置 因为2*x*sum_side
-> 若 sum[i]>=dp[j],dp[k] 且j>k 即都合法 则 pre[i]=j
&& 队首排除 hd++ 若val(hd+1)<=sumi 显然排除队首 因为即使队首满足也不是pre -> 队尾不符合 tl-- 若 vali<=val(tail) 更小就没法玩了 显而易见的排除 其他情况就都压入 在队首判断pre ::: sum[]+=sum[]+a[] >> 1e9 ==int + n=4e7 ->__int128 ::: tmp 可能 ==sum[n] -> __int128 ::: int 4byte(B) == 32 bit /ll 8B/__int128 16B 1GB=1024^3 4e7*10*4+4e7*8+ 16 > 4e9 所以若全部替换成————int128 exceed!!!
*/ #include<cstdio> #include<iostream> #include<algorithm> #include<cmath> //#include<queue> //#include<vector> //#include<bits/stdc++.h> #define ll long long #define ddd printf("-----------------------\n"); using namespace std; const int maxn=4e7+10 ; const int maxnn=1e5+10; const int inf=0x3f3f3f3f; const int mod=1<<30; int x,y,z,m,b[maxn],a[maxn],l[maxnn],r[maxnn],p[maxnn]; int n,type,q[maxn],hd=0,tl=0,pre[maxn]; ll sum[maxn]; __int128 ans=0; ll d(int x){ return sum[x]-sum[pre[x]];} void print(__int128 x){ if(x<0) putchar('-'),x=-x; if(x==0) return; if(x>9) print(x/10); putchar(x%10+'0'); } int main() { ios::sync_with_stdio(false); cin>>n>>type; for(int i=2;i<=n;i++) pre[i]=i; if(type==0){ for(int i=1;i<=n;i++) cin>>a[i],sum[i]+=sum[i-1]+a[i]; } else { cin>>x>>y>>z>>b[1]>>b[2]>>m; for(int i=1;i<=m;i++) cin>>p[i]>>l[i]>>r[i]; if(n>2) for(int i=3;i<=n;i++) b[i]=(1ll*b[i-1]*x+1ll*b[i-2]*y+z)%mod;//b[]<=1<<30 for(int i=1;i<=m;i++) for(int j=p[i-1]+1;j<=p[i];j++) a[j]=b[j]%(r[i]-l[i]+1)+l[i],sum[j]=sum[j-1]+a[j]; } for(int i=1;i<=n;i++) { while(hd<tl&& d(q[hd+1])+sum[q[hd+1]]<=sum[i] ) hd++; pre[i]=q[hd]; while(hd<tl&& d(q[tl])+sum[q[tl]]>=d(i)+sum[i]) tl--; q[++tl]=i; } int now=n;__int128 tmp=0; while(now){//tmp*tmp ==type(tmp) >>ll ==__int128 tmp=d(now); ans+=tmp*tmp; now=pre[now]; } /* // 因为 pre[i]=q[hd]; ans>>actually __int128 tmp=0; for(int i=1;i<=n;i++) tmp=d(i),ans+=tmp*tmp; */ print(ans); return 0; }

 

posted @ 2023-09-23 22:37  JMXZ  阅读(27)  评论(0)    收藏  举报