ProgMe

Untitled

Nov 29th, 2023
650
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.31 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int n;
  6.  
  7. vector<unsigned int> p;
  8. vector<vector<unsigned int>> t;
  9.  
  10. void build(int v = 1, int tl = 0, int tr = n - 1) {
  11.     if (tl == tr) {
  12.         t[v].push_back(p[tl]);
  13.         return;
  14.     }
  15.  
  16.     int tm = (tl + tr) / 2;
  17.  
  18.     build(v * 2, tl, tm);
  19.     build(v * 2 + 1, tm + 1, tr);
  20.  
  21.     merge(t[v * 2].begin(), t[v * 2].end(),
  22.           t[v * 2 + 1].begin(), t[v * 2 + 1].end(),
  23.           back_inserter(t[v]));
  24. }
  25.  
  26. long long get(int l, int r, int x, int v = 1, int tl = 0, int tr = n - 1) {
  27.     if (l > r) {
  28.         return 0;
  29.     }
  30.     if (l == tl && r == tr) {
  31.         return tr - tl + 1 -
  32.         (upper_bound(t[v].begin(), t[v].end(), x) - t[v].begin());
  33.     }
  34.  
  35.     int tm = (tl + tr) / 2;
  36.  
  37.     return get(l, min(r, tm), x, v * 2, tl, tm) +
  38.             get(max(l, tm + 1), r, x, v * 2 + 1, tm + 1, tr);
  39. }
  40.  
  41. unsigned int cur = 0, a, b;
  42.  
  43. unsigned int nextRand24() {
  44.     cur = cur * a + b;
  45.     return cur >> 8;
  46. }
  47.  
  48. int main() {
  49.     unsigned int m;
  50.     cin >> n >> m;
  51.     cin >> a >> b;
  52.  
  53.     p.resize(n);
  54.     t.resize(4 * n);
  55.  
  56.     for (int i = 0; i < n; ++i) {
  57.         p[i] = nextRand24() % m;
  58.     }
  59.  
  60.     build();
  61.  
  62.     long long ans = 0;
  63.     for (int i = 1; i < n; ++i) {
  64.         ans += get(0, i - 1, p[i]);
  65.     }
  66.     cout << ans;
  67. }
  68.  
Advertisement
Add Comment
Please, Sign In to add comment