#include <bits/stdc++.h> #define FOR(i, a, b) for (int i=(a); i<(b); i++) #define PPC(x) __builtin_popcountll((x)) #define ALL(x) (x).begin(), (x).end() #define pb push_back #define st first #define nd second using namespace std; typedef pair <long long, int> Node_t; const int maxN = 1 << 19, maxTS = 1 << 20, maxL = 25; const long long INF = 2000000000000000000ll; struct Tree { int offset, qbegin, qend, root, lvl[maxTS]; Node_t T[maxTS], res; long long mozna[maxTS], target; bool onQ[maxTS]; queue <int> Q[maxL]; vector <int> bnd; void rcmp(int v) { //assert(v < offset); #define SUM(u) (T[u].st & mozna[u]) #define NO(u) (T[u].nd & mozna[u]) T[v] = {SUM(v*2) + SUM(v*2+1), NO(v*2) + NO(v*2+1)}; } void wez(int v) { //assert(mozna[v]); res = {res.st + T[v].st, res.nd + T[v].nd}; mozna[v] = 0ll; bnd.pb(v); } void qpriv(int v, int left, int right) { if (left >= qend or right <= qbegin or root != 0) return; if (left >= qbegin and right <= qend) { if (mozna[v]) { if (res.st + T[v].st < target) wez(v); else root = v; } return; } qpriv(v*2+1, (left + right) / 2, right); qpriv(v * 2, left, (left + right) / 2); rcmp(v); } void updt(int v, long long dx) { v += offset; T[v].st += dx; T[v].nd += dx > 0ll ? 1 : -1; for (v /= 2; v != 0; v /= 2) rcmp(v); } void wloz(int v) { //assert(mozna[v] and res.st + T[v].st >= target); if (v >= offset) { wez(v); return; } if (mozna[v*2+1]) { if (res.st + T[v*2+1].st < target) wez(v*2+1), wloz(v * 2); else wloz(v*2+1); } else wloz(v * 2); rcmp(v); } Node_t query(int a, int b, long long c) { qbegin = a + offset; qend = b + offset; target = c; root = 0; res = {0ll, 0}; qpriv(1, offset, offset * 2); if (root == 0) return {0ll, 0}; wloz(root); for (root /= 2; root != 0; root /= 2) rcmp(root); return res; } void amnesty() { #define PUSH(v) if (!onQ[v]) Q[lvl[v]].push(v), onQ[v] = true for (int v : bnd) { mozna[v] = -1ll; int u = v / 2; PUSH(u); } bnd.resize(0); for (int l=maxL-1; l>0; l--) while (!Q[l].empty()) { int v = Q[l].front(); Q[l].pop(); onQ[v] = false; rcmp(v); v /= 2; PUSH(v); } } void resize(int n) { for (offset = 1; offset < n; offset *= 2); FOR(i, 1, offset*2) mozna[i] = -1ll, lvl[i] = lvl[i/2] + 1; } void add0(int v, long long a) { T[offset + v] = {a, 1}; } void build() { for (int i=offset-1; i>0; i--) rcmp(i); } }tree; map <long long, int> Start, pos; long long vals[maxN], vals0[maxN]; pair <long long, long long> Q[maxN]; multiset <long long> S; int query(long long a, long long b) { S.insert(b-1); Start[b-1] = Start.lower_bound(b-1)->second; int res = 0; while (a < b) { long long c = *S.lower_bound(a); Node_t x = tree.query(0, Start[c], c-a+1ll); if (x.nd == 0) { res = -1; break; } //assert(x.st > 0); a += x.st, res += x.nd; } tree.amnesty(); S.erase(S.find(b-1)); return res; } int main() { int n; scanf ("%d", &n); FOR(i, 0, n) scanf ("%lld", vals0+i), vals[i] = vals0[i]; int q, m = n; scanf ("%d", &q); FOR(i, 0, q) { long long a, b, c; scanf ("%lld", &a); if (a == 1ll) scanf ("%lld%lld", &b, &c); else { scanf ("%lld", &c); b = a-3ll; vals[m++] = c; } Q[i] = {b, c}; } sort(vals, vals + m); sort(vals0, vals0 + n); FOR(i, 0, m) if (i == 0 or vals[i-1] != vals[i]) Start[vals[i]] = i, pos[vals[i]] = i; tree.resize(m); int p; FOR(i, 0, n) { long long x = vals0[i]; if (i == 0 or vals0[i-1] != x) p = pos[x]; tree.add0(p++, x); if (i == n-1 or vals0[i+1] != x) pos[x] = p; S.insert(x); } tree.build(); S.insert(INF); Start[INF] = m; FOR(i, 0, q) { long long a, b; tie(a,b) = Q[i]; if (a == -1ll) tree.updt(pos[b]++, b), S.insert(b); if (a == 0ll) tree.updt(--pos[b], -b), S.erase(S.find(b)); if (a > 0ll) printf("%d\n", query(a, b)); } return 0; }
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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 | #include <bits/stdc++.h> #define FOR(i, a, b) for (int i=(a); i<(b); i++) #define PPC(x) __builtin_popcountll((x)) #define ALL(x) (x).begin(), (x).end() #define pb push_back #define st first #define nd second using namespace std; typedef pair <long long, int> Node_t; const int maxN = 1 << 19, maxTS = 1 << 20, maxL = 25; const long long INF = 2000000000000000000ll; struct Tree { int offset, qbegin, qend, root, lvl[maxTS]; Node_t T[maxTS], res; long long mozna[maxTS], target; bool onQ[maxTS]; queue <int> Q[maxL]; vector <int> bnd; void rcmp(int v) { //assert(v < offset); #define SUM(u) (T[u].st & mozna[u]) #define NO(u) (T[u].nd & mozna[u]) T[v] = {SUM(v*2) + SUM(v*2+1), NO(v*2) + NO(v*2+1)}; } void wez(int v) { //assert(mozna[v]); res = {res.st + T[v].st, res.nd + T[v].nd}; mozna[v] = 0ll; bnd.pb(v); } void qpriv(int v, int left, int right) { if (left >= qend or right <= qbegin or root != 0) return; if (left >= qbegin and right <= qend) { if (mozna[v]) { if (res.st + T[v].st < target) wez(v); else root = v; } return; } qpriv(v*2+1, (left + right) / 2, right); qpriv(v * 2, left, (left + right) / 2); rcmp(v); } void updt(int v, long long dx) { v += offset; T[v].st += dx; T[v].nd += dx > 0ll ? 1 : -1; for (v /= 2; v != 0; v /= 2) rcmp(v); } void wloz(int v) { //assert(mozna[v] and res.st + T[v].st >= target); if (v >= offset) { wez(v); return; } if (mozna[v*2+1]) { if (res.st + T[v*2+1].st < target) wez(v*2+1), wloz(v * 2); else wloz(v*2+1); } else wloz(v * 2); rcmp(v); } Node_t query(int a, int b, long long c) { qbegin = a + offset; qend = b + offset; target = c; root = 0; res = {0ll, 0}; qpriv(1, offset, offset * 2); if (root == 0) return {0ll, 0}; wloz(root); for (root /= 2; root != 0; root /= 2) rcmp(root); return res; } void amnesty() { #define PUSH(v) if (!onQ[v]) Q[lvl[v]].push(v), onQ[v] = true for (int v : bnd) { mozna[v] = -1ll; int u = v / 2; PUSH(u); } bnd.resize(0); for (int l=maxL-1; l>0; l--) while (!Q[l].empty()) { int v = Q[l].front(); Q[l].pop(); onQ[v] = false; rcmp(v); v /= 2; PUSH(v); } } void resize(int n) { for (offset = 1; offset < n; offset *= 2); FOR(i, 1, offset*2) mozna[i] = -1ll, lvl[i] = lvl[i/2] + 1; } void add0(int v, long long a) { T[offset + v] = {a, 1}; } void build() { for (int i=offset-1; i>0; i--) rcmp(i); } }tree; map <long long, int> Start, pos; long long vals[maxN], vals0[maxN]; pair <long long, long long> Q[maxN]; multiset <long long> S; int query(long long a, long long b) { S.insert(b-1); Start[b-1] = Start.lower_bound(b-1)->second; int res = 0; while (a < b) { long long c = *S.lower_bound(a); Node_t x = tree.query(0, Start[c], c-a+1ll); if (x.nd == 0) { res = -1; break; } //assert(x.st > 0); a += x.st, res += x.nd; } tree.amnesty(); S.erase(S.find(b-1)); return res; } int main() { int n; scanf ("%d", &n); FOR(i, 0, n) scanf ("%lld", vals0+i), vals[i] = vals0[i]; int q, m = n; scanf ("%d", &q); FOR(i, 0, q) { long long a, b, c; scanf ("%lld", &a); if (a == 1ll) scanf ("%lld%lld", &b, &c); else { scanf ("%lld", &c); b = a-3ll; vals[m++] = c; } Q[i] = {b, c}; } sort(vals, vals + m); sort(vals0, vals0 + n); FOR(i, 0, m) if (i == 0 or vals[i-1] != vals[i]) Start[vals[i]] = i, pos[vals[i]] = i; tree.resize(m); int p; FOR(i, 0, n) { long long x = vals0[i]; if (i == 0 or vals0[i-1] != x) p = pos[x]; tree.add0(p++, x); if (i == n-1 or vals0[i+1] != x) pos[x] = p; S.insert(x); } tree.build(); S.insert(INF); Start[INF] = m; FOR(i, 0, q) { long long a, b; tie(a,b) = Q[i]; if (a == -1ll) tree.updt(pos[b]++, b), S.insert(b); if (a == 0ll) tree.updt(--pos[b], -b), S.erase(S.find(b)); if (a > 0ll) printf("%d\n", query(a, b)); } return 0; } |