#include <stdio.h> #include <time.h> #include <math.h> #include <stdlib.h> #include <string.h> #include <map> #include <string> #include <vector> #include <iostream> #include <sstream> #include <queue> #include <algorithm> #include <assert.h> using namespace std; #define ll long long #define PB push_back #define FOR(a,start,end) for(int a=int(start); a<int(end); a++) #define INF INT_MAX #define SORT(a) sort(a.begin(),a.end()) #define CL(a,x) memset(a,x,sizeof(a)) #define REP(a,x) for(int a=0;a<x;a++) #define REP1(a,x) for(int a=1;a<=x;a++) #define MP make_pair typedef vector<ll> vi; typedef pair<ll, ll> pii; typedef vector<string> vs; typedef vector<pii> vii; typedef vector<vector<ll> > vvi; typedef vector<vector<string> > vvs; typedef vector<pair<string, string> > vss; typedef pair<string, string> pss; typedef pair<ll, pii> ppii; typedef vector<ppii> vppii; typedef vector<vector<pii> > vvii; typedef vector<vvi> vvvi; ll n, k, a, b, c, d, i, j, q, t, z, m, h, w,s; vi vk, va, vb; vvi vvk, vva; vii vkx, vbx, vax2; pii pa1, pa2, pb1, pb2; ll MaxVal; ll tree[1000001]; ll bitMask; ll read(ll idx) { ll sum = 0; while (idx > 0) { sum += tree[idx]; idx -= (idx & -idx); } return sum; } void update(ll idx, ll val) { while (idx <= MaxVal) { tree[idx] += val; idx += (idx & -idx); } } int bs(ll a) { int i1, i2, i0; i1 = 1; i2 = vkx.size() - 1; while (i2 - i1 > 1) { i0 = (i1 + i2) / 2; if (vkx[i0].first < a) { i1 = i0; } else if (vkx[i0].first > a) { i2 = i0; } else return i0 - 1; } if (vkx[i2].first < a) return i2; else if (vkx[i1].first < a) return i1; else return 0; } int bs2(ll a) { int i1, i2, i0; i1 = 1; i2 = vkx.size() - 1; while (i2 - i1 > 1) { i0 = (i1 + i2) / 2; if (vkx[i0].first < a) { i1 = i0; } else if (vkx[i0].first > a) { i2 = i0; } else return i0; } if (vkx[i1].first == a) return i1; else return i2; } int find(ll a,int i2) { int i1, i0; i1 = 0; while (i2 - i1 > 1) { i0 = (i1 + i2) / 2; ll b = read(i0); if (b < a) i1 = i0; else if (b >= a) { i2 = i0; } } if (read(i1) == a) return i1; else return i2; } ll ils(ll s, ll k) { vi va; va.clear(); ll z = 0; while (s < k) { int i = bs(s); ll a = read(i); if (a == 0) { z = -1; break; } ll b = find(a,i); s += vkx[b].first; update(b, -1); va.push_back(b); z++; } for (int i = 0; i < va.size(); i++) update(va[i], 1); return z; } int main() { //freopen("c:\\wojtek\\uva\\pa\\debug\\t1.in", "rt", stdin); //pi=2*acos(0.0); scanf("%lld", &n); vkx.clear(); vkx.push_back(MP(0, 0)); for (int i = 0; i < n; i++) { scanf("%lld", &a); vkx.push_back(MP(a,0)); } SORT(vkx); i = 1; j = 2; vkx[i].second = 1; while (j < n+1) { if (vkx[j].first == vkx[i].first) vkx[i].second++; else { vkx[++i].first = vkx[j].first; vkx[i].second = 1; } j++; } vkx.resize(i + 1); // vvk.clear(); scanf("%lld", &q); vk.assign(3, 0); for (int i = 0; i < q; i++) { scanf("%lld", &a); vk[0] = a; if (a == 1) { scanf("%lld%lld", &vk[1],&vk[2]); vvk.push_back(vk); } else if (a == 2) {//dodaj scanf("%lld", &vk[1]); vvk.push_back(vk); vkx.push_back(MP(vk[1], 0)); } else { scanf("%lld", &vk[1]);//usun vvk.push_back(vk); } } SORT(vkx); i = 1; j = 2; while (j < (ll)vkx.size()) { if (vkx[j].first != vkx[i].first) i++; j++; } vkx.resize(i + 1); MaxVal = (ll)vkx.size(); m = 0; bitMask = MaxVal; while (bitMask != 0) { m = bitMask; bitMask -= (bitMask&-bitMask); } bitMask = m; for(int i=1;i<(int)vkx.size();i++) update(i, vkx[i].second); for (int i = 0; i <(int) vvk.size(); i++) { if (vvk[i][0] == 1) { z = ils(vvk[i][1], vvk[i][2]); printf("%lld\n", z); } else if (vvk[i][0] == 2) {//dodaj z = bs2(vvk[i][1]); update(z, 1); } else { z = bs2(vvk[i][1]); update(z, -1); } } 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 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | #include <stdio.h> #include <time.h> #include <math.h> #include <stdlib.h> #include <string.h> #include <map> #include <string> #include <vector> #include <iostream> #include <sstream> #include <queue> #include <algorithm> #include <assert.h> using namespace std; #define ll long long #define PB push_back #define FOR(a,start,end) for(int a=int(start); a<int(end); a++) #define INF INT_MAX #define SORT(a) sort(a.begin(),a.end()) #define CL(a,x) memset(a,x,sizeof(a)) #define REP(a,x) for(int a=0;a<x;a++) #define REP1(a,x) for(int a=1;a<=x;a++) #define MP make_pair typedef vector<ll> vi; typedef pair<ll, ll> pii; typedef vector<string> vs; typedef vector<pii> vii; typedef vector<vector<ll> > vvi; typedef vector<vector<string> > vvs; typedef vector<pair<string, string> > vss; typedef pair<string, string> pss; typedef pair<ll, pii> ppii; typedef vector<ppii> vppii; typedef vector<vector<pii> > vvii; typedef vector<vvi> vvvi; ll n, k, a, b, c, d, i, j, q, t, z, m, h, w,s; vi vk, va, vb; vvi vvk, vva; vii vkx, vbx, vax2; pii pa1, pa2, pb1, pb2; ll MaxVal; ll tree[1000001]; ll bitMask; ll read(ll idx) { ll sum = 0; while (idx > 0) { sum += tree[idx]; idx -= (idx & -idx); } return sum; } void update(ll idx, ll val) { while (idx <= MaxVal) { tree[idx] += val; idx += (idx & -idx); } } int bs(ll a) { int i1, i2, i0; i1 = 1; i2 = vkx.size() - 1; while (i2 - i1 > 1) { i0 = (i1 + i2) / 2; if (vkx[i0].first < a) { i1 = i0; } else if (vkx[i0].first > a) { i2 = i0; } else return i0 - 1; } if (vkx[i2].first < a) return i2; else if (vkx[i1].first < a) return i1; else return 0; } int bs2(ll a) { int i1, i2, i0; i1 = 1; i2 = vkx.size() - 1; while (i2 - i1 > 1) { i0 = (i1 + i2) / 2; if (vkx[i0].first < a) { i1 = i0; } else if (vkx[i0].first > a) { i2 = i0; } else return i0; } if (vkx[i1].first == a) return i1; else return i2; } int find(ll a,int i2) { int i1, i0; i1 = 0; while (i2 - i1 > 1) { i0 = (i1 + i2) / 2; ll b = read(i0); if (b < a) i1 = i0; else if (b >= a) { i2 = i0; } } if (read(i1) == a) return i1; else return i2; } ll ils(ll s, ll k) { vi va; va.clear(); ll z = 0; while (s < k) { int i = bs(s); ll a = read(i); if (a == 0) { z = -1; break; } ll b = find(a,i); s += vkx[b].first; update(b, -1); va.push_back(b); z++; } for (int i = 0; i < va.size(); i++) update(va[i], 1); return z; } int main() { //freopen("c:\\wojtek\\uva\\pa\\debug\\t1.in", "rt", stdin); //pi=2*acos(0.0); scanf("%lld", &n); vkx.clear(); vkx.push_back(MP(0, 0)); for (int i = 0; i < n; i++) { scanf("%lld", &a); vkx.push_back(MP(a,0)); } SORT(vkx); i = 1; j = 2; vkx[i].second = 1; while (j < n+1) { if (vkx[j].first == vkx[i].first) vkx[i].second++; else { vkx[++i].first = vkx[j].first; vkx[i].second = 1; } j++; } vkx.resize(i + 1); // vvk.clear(); scanf("%lld", &q); vk.assign(3, 0); for (int i = 0; i < q; i++) { scanf("%lld", &a); vk[0] = a; if (a == 1) { scanf("%lld%lld", &vk[1],&vk[2]); vvk.push_back(vk); } else if (a == 2) {//dodaj scanf("%lld", &vk[1]); vvk.push_back(vk); vkx.push_back(MP(vk[1], 0)); } else { scanf("%lld", &vk[1]);//usun vvk.push_back(vk); } } SORT(vkx); i = 1; j = 2; while (j < (ll)vkx.size()) { if (vkx[j].first != vkx[i].first) i++; j++; } vkx.resize(i + 1); MaxVal = (ll)vkx.size(); m = 0; bitMask = MaxVal; while (bitMask != 0) { m = bitMask; bitMask -= (bitMask&-bitMask); } bitMask = m; for(int i=1;i<(int)vkx.size();i++) update(i, vkx[i].second); for (int i = 0; i <(int) vvk.size(); i++) { if (vvk[i][0] == 1) { z = ils(vvk[i][1], vvk[i][2]); printf("%lld\n", z); } else if (vvk[i][0] == 2) {//dodaj z = bs2(vvk[i][1]); update(z, 1); } else { z = bs2(vvk[i][1]); update(z, -1); } } return 0; } |