Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int n;
- vector<unsigned int> p;
- vector<vector<unsigned int>> t;
- void build(int v = 1, int tl = 0, int tr = n - 1) {
- if (tl == tr) {
- t[v].push_back(p[tl]);
- return;
- }
- int tm = (tl + tr) / 2;
- build(v * 2, tl, tm);
- build(v * 2 + 1, tm + 1, tr);
- merge(t[v * 2].begin(), t[v * 2].end(),
- t[v * 2 + 1].begin(), t[v * 2 + 1].end(),
- back_inserter(t[v]));
- }
- long long get(int l, int r, int x, int v = 1, int tl = 0, int tr = n - 1) {
- if (l > r) {
- return 0;
- }
- if (l == tl && r == tr) {
- return tr - tl + 1 -
- (upper_bound(t[v].begin(), t[v].end(), x) - t[v].begin());
- }
- int tm = (tl + tr) / 2;
- return get(l, min(r, tm), x, v * 2, tl, tm) +
- get(max(l, tm + 1), r, x, v * 2 + 1, tm + 1, tr);
- }
- unsigned int cur = 0, a, b;
- unsigned int nextRand24() {
- cur = cur * a + b;
- return cur >> 8;
- }
- int main() {
- unsigned int m;
- cin >> n >> m;
- cin >> a >> b;
- p.resize(n);
- t.resize(4 * n);
- for (int i = 0; i < n; ++i) {
- p[i] = nextRand24() % m;
- }
- build();
- long long ans = 0;
- for (int i = 1; i < n; ++i) {
- ans += get(0, i - 1, p[i]);
- }
- cout << ans;
- }
Add Comment
Please, Sign In to add comment