Codeforces 1119D(贡献计算)
排序看一看。
关键点在于发现性质:
算一个点的贡献时:
1.与后一个有重叠。$$当 a[i] + r >= a[i + 1] + l, 即 r - l >= a[i + 1] - a[i] 时$$a[i] 与 a[i+1] 重叠的部分,都算在a[i+1]里,则a[i]的贡献为:a[i+1] - a[i]
2.无重叠。r - l + 1
3.a[n]的贡献一定是r - l + 1
因此再把差值排序一下二分答案O(1)算出即可
const int maxn = 1e5 + 5;
int n, m, q;
ll a[maxn], d[maxn], sum[maxn];
int main() {
read(n);
rep(i, 1, n) read(a[i]);
sort(a + 1, a + 1 + n);
m = unique(a + 1, a + 1 + n) - a - 1;
rep(i, 1, m - 1) d[i] = a[i + 1] - a[i];
sort(d + 1, d + m);
rep(i, 1, m - 1) sum[i] = sum[i - 1] + d[i];
for (read(q); q; q--) {
ll l, r;
read(l), read(r);
int pos = upper_bound(d + 1, d + m, r - l) - d;
cout << sum[pos - 1] + (r - l + 1) * (m - pos + 1) << " ";
}
return 0;
}