fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. using ll = long long;
  7.  
  8. int main() {
  9. int N, K;
  10. cin >> N;
  11. vector<int> A(N), B(N);
  12. vector<int> X(K), Y(K);
  13.  
  14. for (int i = 0; i < N; ++i) cin >> A[i];
  15. for (int i = 0; i < N; ++i) cin >> B[i];
  16.  
  17. cin>>K;
  18. for (int i = 0; i < K; ++i){
  19. cin >> X[i] >> Y[i];
  20. X[i]--;
  21. Y[i]--;
  22. }
  23.  
  24. // Sort A and B
  25. sort(A.begin(), A.end());
  26. sort(B.begin(), B.end());
  27.  
  28. // Compute prefix sums
  29. vector<ll> prefixA(N + 1, 0), prefixB(N + 1, 0);
  30. for (int i = 1; i <= N; ++i) {
  31. prefixA[i] = prefixA[i - 1] + A[i - 1];
  32. prefixB[i] = prefixB[i - 1] + B[i - 1];
  33. }
  34.  
  35. // Answer each query
  36. for (int k = 0; k < K; ++k) {
  37. int x = X[k], y = Y[k];
  38.  
  39. ll sumA = prefixA[x];
  40. ll sumB = prefixB[y];
  41.  
  42. // Total sum of absolute differences
  43. ll result = x * sumB + y * sumA - 2 * (prefixA[x] * y);
  44. cout << result << endl;
  45. }
  46.  
  47. return 0;
  48. }
  49.  
Success #stdin #stdout 0.01s 5280KB
stdin
2
2 4
3 5
4
1 1
1 2
2 1
2 2

stdout
0
0
0
1