#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 100010;
int w[N], s[N];
int n, m;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &w[i]);
scanf("%d", &m);
while (m--) {
int l, r;
scanf("%d%d", &l, &r);
s[l]++, s[r + 1]--;
}
for (int i = 1; i <= n; i++) s[i] += s[i - 1];
LL res1 = 0;
for (int i = 1; i <= n; i++) res1 += (LL)w[i] * s[i];
sort(w + 1, w + n + 1);
sort(s + 1, s + n + 1);
LL res2 = 0;
for (int i = 1; i <= n; i++) res2 += (LL)w[i] * s[i];
printf("%lld\n", res2 - res1);
return 0;
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class Main {
static int N = 100010;
static int[] a = new int[N];
static long[] s = new long[N];
static long[] g = new long[N];
static int n;
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws IOException {
n = Integer.parseInt(br.readLine());
String[] v = br.readLine().split(" ");
for (int i = 1; i <= n; i++) {
a[i] = Integer.parseInt(v[i - 1]);
s[i] = s[i - 1] + a[i];
}
int m = Integer.parseInt(br.readLine());
long res = 0;
for (int i = 0; i < m; i++) {
v = br.readLine().split(" ");
int l = Integer.parseInt(v[0]);
int r = Integer.parseInt(v[1]);
g[l]++;
g[r + 1]--;
res += s[r] - s[l - 1];
}
for (int i = 1; i <= n; i++) {
g[i] += g[i - 1];
}
long ans = 0;
Arrays.sort(a);
Arrays.sort(g);
int pre = g.length - 1;
while (g[pre] != 0) {
ans += g[pre] * a[pre];
pre--;
}
System.out.println(ans - res);
}
}
在其他OJ都是AC的啊?
错了嘛?
@Wind_Rises
@root
题解都不对
(低调中)
共 3 条回复
要在评论区艾特才有用哈,
@Wind_Rises 好的
数据错误 一直没时间改 过段时间