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
| #include <bits/stdc++.h> #include <ext/pb_ds/tree_policy.hpp> #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; using namespace std; #define lli long long int #define mp make_pair #define pb push_back #define eb emplace_back #define test(x) cout << "Line(" << __LINE__ << ") " #x << ' ' << x << endl #define printv(x) {\ for (auto i : x) cout << i << ' ';\ cout << endl;\ } #define pii pair <int, int> #define pll pair <lli, lli> #define X first #define Y second #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() template<typename A, typename B> ostream& operator << (ostream& o, pair<A, B> a){ return o << a.X << ' ' << a.Y; } template<typename A, typename B> istream& operator >> (istream& o, pair<A, B> &a){ return o >> a.X >> a.Y; } const int mod = 1e9 + 7, abc = 864197532, N = 2008, logN = 17, K = 333;
tree<pii, null_type, less<pii>, rb_tree_tag, tree_order_statistics_node_update> oset;
struct Seg { int l, r, m, val, cnt, mx; Seg *ch[2]; Seg (int _l, int _r, vector<int> &a) : l(_l), r(_r), m(l + r >> 1) { if (r - l > 1) { ch[0] = new Seg(l, m, a); ch[1] = new Seg(m, r, a); pull(); } else { val = mx = a[l]; oset.insert(mp(a[l], l)); cnt = 1; } } void pull() { if (ch[0]->val == ch[1]->val) { val = ch[0]->val; cnt = ch[0]->cnt + ch[1]->cnt; } else if (ch[0]->cnt > ch[1]->cnt) { val = ch[0]->val; cnt = ch[0]->cnt - ch[1]->cnt; } else { val = ch[1]->val; cnt = ch[1]->cnt - ch[0]->cnt; } mx = max(ch[0]->mx, ch[1]->mx); } void modify(int a, int b, int k) { if (r - l == 1) { oset.erase(mp(val, l)); val = mx = val / k; oset.insert(mp(val, l)); } else { if (a < m && ch[0]->mx > 0) ch[0]->modify(a, b, k); if (m < b && ch[1]->mx > 0) ch[1]->modify(a, b, k); pull(); } } void set(int p, int v) { if (r - l == 1) { oset.erase(mp(val, l)); val = mx = v; oset.insert(mp(val, l)); } else { ch[p >= m]->set(p, v); pull(); } } pii query(int a, int b) { if (a <= l && r <= b) return mp(val, cnt); pii cur = mp(-1, 0); if (a < m) { pii tmp = ch[0]->query(a, b); if (cur.X == tmp.X) { cur.Y += tmp.Y; } else { if (cur.Y <= tmp.Y) swap(cur, tmp); cur.Y -= tmp.Y; } } if (m < b) { pii tmp = ch[1]->query(a, b); if (cur.X == tmp.X) { cur.Y += tmp.Y; } else { if (cur.Y <= tmp.Y) swap(cur, tmp); cur.Y -= tmp.Y; } } return cur; } };
int main () { ios::sync_with_stdio(false); cin.tie(0); int n, q; cin >> n >> q; vector <int> a(n); for (int i = 0; i < n; ++i) cin >> a[i]; Seg root(0, n, a); while (q--) { int t, l, r, k; cin >> t >> l >> r, l--; if (t == 1) { root.set(l, r); } else if (t == 2) { cin >> k; if (k > 1) root.modify(l, r, k); } else { pii tmp = root.query(l, r); if (oset.order_of_key(mp(tmp.X, r)) - oset.order_of_key(mp(tmp.X, l)) < (r - l) / 2 + 1) { cout << "-1\n"; } else { cout << tmp.X << '\n'; } } } }
|