DuongNhi99

METRICS

Dec 10th, 2020
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.12 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. using namespace std;
  4.  
  5. const int N = 1e5 + 5;
  6.  
  7. int n, m;
  8. int a[N];
  9. pair<int, int> b[N];
  10. ll sb[N], sj[N], sbj[N];
  11.  
  12. int main() {
  13.     //freopen("in.txt", "r", stdin);
  14.     freopen("METRICS.inp", "r", stdin);
  15.     freopen("METRICS.out", "w", stdout);
  16.     ios_base::sync_with_stdio(false);
  17.     cin.tie(NULL); cout.tie(NULL);
  18.  
  19.     cin >> n;
  20.     for(int i = 1; i <= n; ++i)
  21.         cin >> a[i];
  22.  
  23.     cin >> m;
  24.     for(int j = 1; j <= m; ++j){
  25.         cin >> b[j].first;
  26.         b[j].second = j;
  27.     }
  28.     sort(b + 1, b + m + 1);
  29.  
  30.     for(int i = 1; i <= m; ++i){
  31.         sb[i] = sb[i-1] + b[i].first;
  32.         sj[i] = sj[i-1] + b[i].second;
  33.         sbj[i] = sbj[i-1] + b[i].first * b[i].second;
  34.     }
  35.  
  36.     ll ans = 0;
  37.     for(int i = 1; i <= n; ++i){
  38.         int k = upper_bound(b, b+m+1, make_pair(a[i], 0)) - b - 1;
  39.         k = max(k, 0);
  40.  
  41.         ans += 1LL * a[i]*i*k + sbj[k];
  42.         ans -= a[i]*sj[k] + i*sb[k];
  43.  
  44.         ans -= 1LL * a[i]*i*(m-k) + (sbj[m]-sbj[k]);
  45.         ans += a[i]*(sj[m]-sj[k]) + i*(sb[m]-sb[k]);
  46.     }
  47.     cout << ans;
  48.     return 0;
  49. }
  50.  
Add Comment
Please, Sign In to add comment