orazma

e.cpp

Mar 27th, 2026
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.74 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. #define f first
  4. #define s second
  5. using namespace std;
  6. int n, m, q;
  7. const int MAXN = 1e6 + 10;
  8. const int INF = 1e9 + 7;
  9. int a[MAXN], b[MAXN];
  10. vector <int> t_pref(MAXN * 4, 0);
  11. vector <int> t_suf(MAXN * 4, 0);
  12.  
  13. void update(vector <int> &t, int v, int tl, int tr, int pos, int val){
  14.     if (tl > tr){
  15.         return;
  16.     }
  17.     if (tl == tr){
  18.         t[v] += val;
  19.         return;
  20.     }
  21.     int tm = (tl + tr) / 2;
  22.     if (pos <= tm){
  23.         update(t, v + v, tl, tm, pos, val);
  24.     }
  25.     else{
  26.         update(t, v + v + 1, tm + 1, tr, pos, val);
  27.     }
  28.     t[v] = t[v + v] + t[v + v + 1];
  29. }
  30.  
  31. int get_sum(vector <int> &t, int v, int tl, int tr, int l, int r){
  32.     if (l > r){
  33.         return 0;
  34.     }
  35.     if (tl == l && tr == r){
  36.         return t[v];
  37.     }
  38.     int tm = (tl + tr) / 2;
  39.     return get_sum(t, v + v, tl , tm, l, min(r, tm)) +
  40.         get_sum(t, v + v + 1, tm + 1, tr, max(l, tm + 1), r);
  41. }
  42.  
  43. int main() {
  44.     int n;
  45.     cin >> n;
  46.     for (int i = 1; i <= n; i++){
  47.         cin >> a[i];
  48.         b[i] = a[i];
  49.     }
  50.     sort(b + 1, b + n + 1);
  51.     int timer = 1;
  52.     map <int, int> ma;
  53.     ma[b[1]] = timer;
  54.     for (int i = 2; i <= n; i++){
  55.         if (b[i] != b[i - 1]){
  56.             timer++;
  57.             ma[b[i]] = timer;
  58.         }
  59.     }
  60.  
  61.     for (int i = 1; i <= n; i++){
  62.         a[i] = ma[a[i]];
  63.         update(t_suf, 1, 1, n, a[i], 1);
  64.     }
  65.     ll ans = 0;
  66.     for (int j = 1; j <= n; j++){
  67.         update(t_suf, 1, 1, n, a[j], -1);
  68.         int val1 = get_sum(t_pref, 1, 1, n, a[j] + 1, n);
  69.         int val2 = get_sum(t_suf, 1, 1, n, 1, a[j] - 1);
  70.         ans += 1LL * val1 * val2;
  71.         update(t_pref, 1, 1, n, a[j], 1);
  72.     }
  73.     cout << ans;
  74. }
  75.  
  76.  
Advertisement
Add Comment
Please, Sign In to add comment