1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
| #include<bits/stdc++.h> using namespace std; #define int long long struct node { int l, r; mutable int val; node(int _l = -1, int _r = -1, int _val = 0) : l(_l), r(_r), val(_val) {} bool operator < (node other) const { return l < other.l; } }; set<node> chtholly; set<node>::iterator split(int x) { auto it = chtholly.lower_bound(node(x)); if (it != chtholly.end() && x == it -> l) { return it; } it--; int l = it -> l, r = it -> r, val = it -> val; chtholly.erase(it); chtholly.insert(node(l, x - 1, val)); return chtholly.insert(node(x, r, val)).first; } void apply1(int l, int r, int x) { auto it2 = split(r + 1), it1 = split(l); for (auto i = it1; i != it2; i++) { i -> val += x; } } void merge(int l, int r, int val) { auto it2 = split(r + 1), it1 = split(l); chtholly.erase(it1, it2); chtholly.insert(node(l, r, val)); }
int apply3(int l, int r, int x) { auto it2 = split(r + 1), it1 = split(l); vector<pair<int, int>> tp; for (auto i = it1; i != it2; i++) { tp.push_back({i -> val, i -> r - i -> l + 1}); } sort(tp.begin(), tp.end(), [&](pair<int, int> x, pair<int, int> y) { return x.first < y.first; }); for (int i = 0; i < tp.size(); i++) { x -= tp[i].second; if (x <= 0) { return tp[i].first; } } } int fastpow(int a, int b, int mod) { a %= mod; int res = 1; while (b) { if (b & 1) { res = (res * a) % mod; } a = (a * a) % mod; b >>= 1; } return res; } int apply4(int l, int r, int x, int y) { int res = 0; auto it2 = split(r + 1), it1 = split(l); for (auto i = it1; i != it2; i++) { res = (res + ((i -> r - i -> l + 1) % y * fastpow(i -> val, x, y)) % y) % y; } return res; } int n, m, seed, vmax; int rnd() { int tp = seed; seed = (seed * 7 + 13) % 1000000007; return tp; } signed main() { cin >> n >> m >> seed >> vmax; chtholly.insert(node(0, n + 1)); int ls = -1, val = -1; for (int i = 1; i <= n; i++) { int x = (rnd() % vmax) + 1; if (x != val) { if (ls != -1) { merge(ls, i - 1, val); } ls = i; val = x; } } merge(ls, n, val); for (int i = 1; i <= m; i++) { int ch = (rnd() % 4) + 1, l = (rnd() % n) + 1, r = (rnd() % n) + 1; if (l > r) { swap(l, r); } int x, y; if (ch == 3) { x = (rnd() % (r - l + 1)) + 1; } else { x = (rnd() % vmax) + 1; } if (ch == 4) { y = (rnd() % vmax) + 1; } if (ch == 1) { apply1(l, r, x); } else if (ch == 2) { merge(l, r, x); } else if (ch == 3) { cout << apply3(l, r, x) << '\n'; } else { cout << apply4(l, r, x, y) << '\n'; } } return 0; }
|