Advertisement
pb_jiang

abc261/f/main.cpp

Sep 17th, 2022
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.32 KB | None | 0 0
  1. #include <assert.h>
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. using ll = long long;
  6. using pii = pair<int, int>;
  7.  
  8. const int MAXN = 3e5 + 3;
  9. int n;
  10. ll ans;
  11. int c[MAXN], x[MAXN];
  12. int idx[MAXN];
  13. int tmp_mg[MAXN];
  14.  
  15. void sort(int beg, int end)
  16. {
  17.     if (end - beg <= 1) {
  18.         return;
  19.     }
  20.     int mid = (beg + end) / 2;
  21.     sort(beg, mid);
  22.     sort(mid, end);
  23.  
  24.     unordered_map<int, int> ccnt;
  25.     int now = beg;
  26.     int i, j;
  27.     for (i = beg, j = mid; i < mid && j < end;) {
  28.         if (x[idx[i]] <= x[idx[j]]) {
  29.             ans += (j - mid) - ccnt[c[idx[i]]];
  30.             tmp_mg[now++] = idx[i++];
  31.         } else {
  32.             ccnt[c[idx[j]]]++;
  33.             tmp_mg[now++] = idx[j++];
  34.         }
  35.     }
  36.  
  37.     while (i < mid) {
  38.         ans += (j - mid) - ccnt[c[idx[i]]];
  39.         tmp_mg[now++] = idx[i++];
  40.     }
  41.     while (j < end) {
  42.         tmp_mg[now++] = idx[j++];
  43.     }
  44.     assert(now == end);
  45.     for (int i = beg; i < end; ++i) {
  46.         idx[i] = tmp_mg[i];
  47.     }
  48. }
  49.  
  50. int main(int argc, char **argv)
  51. {
  52.     scanf("%d", &n);
  53.     for (int i = 0; i < n; ++i)
  54.         idx[i] = i;
  55.     for (int i = 0; i < n; ++i) {
  56.         scanf("%d", c + i);
  57.     }
  58.     for (int i = 0; i < n; ++i) {
  59.         scanf("%d", x + i);
  60.     }
  61.     ans = 0;
  62.     sort(0, n);
  63.     printf("%lld\n", ans);
  64.     return 0;
  65. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement