跳到正文

题解

题解:P16521 [GKS 2015 #E] Sums of Sums

HACKILLER约 6 分钟
原文
文章信息与版本

文章信息

发布
2026年5月19日
收录
2026年9月7日
更新
2026年11月11日
篇幅
约 6 分钟
题目
P16521

版本

  1. v1当前

题解:P16521 [GKS 2015 #E] Sums of Sums

简化题意

给那些懒得看题的人类。
给定正整数组 a[1..n]a[1..n],将其所有 n(n+1)/2n(n+1)/2 个连续子数组的和从小到大排序得到数组 SS。QQ 次询问 [L,R][L,R],求 ∑i=lrSi\sum_{i=l}^{r}S_i 的和。

思路

为什么 QQ 那么小呢?很明显就是每个问询独立查询。应该很容易想到用前缀和维护:∑i=lrSi=∑i=1rSi−∑i=1l−1Si\sum_{i=l}^{r}S_i=\sum_{i=1}^{r}S_i-\sum_{i=1}^{l-1}S_i。

想到用二分答案。设 f(x)f(x) 为子数组和 ≤x\le x 的个数,g(x)g(x) 为这些子数组的和的总和。则 SS 前 kk 小的和:ans(k)=g(v−1)+v⋅(k−f(v−1))ans(k)=g(v-1)+v\cdot\bigl(k-f(v-1)\bigr)
其中 v=min⁡{x∣f(x)≥k}v=\min\{x\mid f(x)\ge k\}。

然后就可以用双指针计算 f(x)f(x) 与 g(x)g(x):固定左端点 ll,右端点 rr 单调移动,使区间 [l,r][l,r] 的和 ≤x\le x。个数贡献 r−l+1r-l+1,总和贡献利用前缀和及其前缀 ssss 快速计算:∑j=lr(sj−sl−1)=(ss[r]−ss[l−1])−(r−l+1)⋅sl−1.\sum_{j=l}^{r}(s_j-s_{l-1}) = (ss[r]-ss[l-1])-(r-l+1)\cdot s_{l-1}.
预处理 si=∑k=1iaks_i=\sum_{k=1}^i a_k,ssi=∑k=1iskss_i=\sum_{k=1}^i s_k。

复杂度就是 O(Qnlog⁡sum)O(Qn\log sum)。

代码

C++
#include<bits/stdc++.h>
//Things will always be better, just let time play
using namespace std;
#define HACKILLER_FAST ios::sync_with_stdio(false),cin.tie(0)
#define int long long
#define rint register int
#define db double
#define pii pair<int,int>
#define vc vector
#define pb push_back
#define ls x<<1
#define rs x<<1|1
int T,n,q,a[200050],s[200050],ss[200050],sum,gcnt,gsum,ql,qr;
inline int count(int lmt){
	int cnt = 0,cur = 0,r = 0;
	for(int l = 1;l <= n;++l){
        if(l > r){
            r = l-1;
            cur = 0;
        }
		while(r < n && cur+a[r+1] <= lmt){
			++r;
			cur+=a[r];
		}
		if(r >= l) cnt+=(r-l+1);
		if(l <= r) cur-=a[l];
	}
    return cnt;
}
inline void work(int lmt){
	gcnt = gsum = 0;
	int cur = 0,r = 0;
	for(int l = 1;l <= n;++l){
        if(l > r){
            r = l-1;
            cur = 0;
        }
		while(r < n && cur+a[r+1] <= lmt){
			++r;
			cur+=a[r];
		}
		if(r >= l){
			int len = r-l+1;
			gcnt+=len;
			gsum+=(ss[r]-ss[l-1]-len*s[l-1]);
		}
		if(l <= r) cur-=a[l];
	}
}
inline int solve(int x){
    if(x == 0) return 0;
    int l = 1,r = sum,res = sum;
    while(l <= r){
    	int mid = l+r>>1;
    	if(count(mid) >= x){
    		res = mid;
    		r = mid-1;
		}
		else l = mid+1;
	}
	work(res-1);
	return gsum+res*(x-gcnt);
}

signed main(){
//	freopen("data.in","r",stdin);
//	freopen("task.out","w",stdout);
	HACKILLER_FAST;
	cin>>T;
	for(int t = 1;t <= T;++t){
		cin>>n>>q;
		for(int i = 1;i <= n;++i) cin>>a[i];
		s[0] = ss[0] = 0;
		for(int i = 1;i <= n;++i) s[i] = s[i-1]+a[i];
		for(int i = 1;i <= n;++i) ss[i] = ss[i-1]+s[i];
		sum = s[n];
		cout<<"Case #"<<t<<":"<<'\n';
		while(q--){
			cin>>ql>>qr;
			cout<<solve(qr)-solve(ql-1)<<'\n';
		}
	}
	return 0;
}
/*
Sometimes things go against your will
But true man never stops their steps
Toward the destiny that they fulfill
And gain the life that they want
Do what you feel is right to do
Don't try to interpret what the others may say
The more you try, the more you will be disappointed
Feel the warmness of others
Believe in the ones who you should believe in
No matter what they say
Do not question them
Follow their words
And the love song goes on
*/ //Wish for 99, Recall and watch the future, I will love her forever~
原文