#include <bits/stdc++.h> using namespace std; using ll = long long; using node_size_t = ll; // splej z biblioteczki UJ struct SplayTree { #define _subtree_size(x) ((x)?(x)->subtree : 0ll) #define _subtreeVal_size(x) ((x)?x->subtreeVal : 0ll) struct Node{ ll val; Node* left_, *right_, *p_; //tych (i dalszych) pol nie dotykac bezposrednio Node(const ll& v): val(v), left_(NULL), right_(NULL), p_(NULL){ resize(); } void set_left(Node *x){ left_ = x; if(x) x->p_ = this; resize(); } void set_right(Node *x){ right_ = x; if(x) x->p_ = this; resize();} //template<> node_size_t SplayTree<KTH_TEST>::Node::node_size(){ ... } node_size_t node_size(){return 1;} node_size_t subtree; ll subtreeVal; void resize(){ subtree = _subtree_size(left_) + _subtree_size(right_) + node_size(); subtreeVal = _subtreeVal_size(left_) + _subtreeVal_size(right_) + val; } void rotate() { Node *parent = p_; this->p_ = parent->p_; if(parent->p_) { if (parent==parent->p_->left_) parent->p_->set_left(this); else parent->p_->set_right(this); } if (this==parent->left_) { parent->set_left(this->right_); set_right(parent); } else { parent->set_right(this->left_); set_left(parent); } } void dump_inorder_(vector<ll>&out){ /* DUMP */ if(left_) left_->dump_inorder_(out); /* DUMP */ out.push_back(val); /* DUMP */ if(right_) right_->dump_inorder_(out); /* DUMP */ } /* DUMP */ void clear_(){ /* CLEAR */ if(left_) {left_->clear_(); delete left_;} /* CLEAR */ if(right_) {right_->clear_(); delete right_;} /* CLEAR */ } /* CLEAR */ } *root; void dump_inorder(vector<ll>&out){ /* DUMP */ if(root) root->dump_inorder_(out); /* DUMP */ } /* DUMP */ SplayTree():root(NULL){} //UWAGA - piszac deepcopy pamietaj o pushowaniu // ~SplayTree(){ clear(); } /* CLEAR */ void swap(SplayTree& rh){ std::swap(root, rh.root); } void clear(){ /* CLEAR */ if(!root) return; /* CLEAR */ root->clear_(); /* CLEAR */ delete root; /* CLEAR */ root = NULL; /* CLEAR */ } Node* splay(Node *v) { //uwaga, zmienia korzen drzewa while(v->p_) { if (v->p_->p_ && ((v==v->p_->left_) == (v->p_==v->p_->p_->left_))) v->p_->rotate(); v->rotate(); } return root = v; } void debug() { vector<ll> v; dump_inorder(v); for (auto i : v) printf("%lld ", i); printf("\n"); } /* MULTISET */ Node* lower_bound(const ll &x){ Node *v = root, *res = NULL, *prev = NULL; while(v){ prev = v; if(v->val < x) v = v->right_; else { res = v; v = v->left_; } } if (res) splay(res); else if(prev) splay(prev); return res; } Node* insert(const ll& x){ Node *v = new Node(x); while(root){ if(x < root->val){ if(root->left_) root = root->left_; else { root->set_left(v); break; } } else { if(root->right_) root = root->right_; else { root->set_right(v); break; } } } return splay(v); }; void remove(const ll& x){ lower_bound(x); auto l = root->left_; auto r = root->right_; root->set_left(NULL); root->set_right(NULL); if (l) l->p_ = NULL; if (r) r->p_ = NULL; if (l == NULL) { root = r; return; } root = l; if (r) { append_(r); } } /* PATH */ // Pierwszy wierzcholek taki ze suma rozmiarow wierzcholkow w porzadku // inorder do tego wierzcholka wlacznie jest > k. Jesli rozmiary sa = 1, // to jest to k-ty wierzcholek w porzadku inorder. (Liczac od 0) Node *splay_kth(node_size_t k){ Node *v = root; if (_subtree_size(v) <= k) return NULL; for(;;) { if(!v) return NULL; if (_subtree_size(v->left_) <= k && _subtree_size(v->left_) + v->node_size()>k){ return splay(v); } if (_subtree_size(v->left_) <= k) { k -= (v->node_size()+_subtree_size(v->left_)); v = v->right_; } else v = v->left_; } } void append_(Node* nw){ if(!nw) return; Node *v = root; while(v){ if(v->right_) v = v->right_; else break; } if(v) v->set_right(nw); splay(nw); } void append(const ll& val){ append_(new Node(val)); } void insert_at(node_size_t k, const ll& val){ //val bedzie na ktej pozycji // (od zera). UWAGA - Jesli k jest duze, to val bedzie appendowane. // Jesli rozmiary wezlow nie sa jednostkowe, to val zostanie wsadzone // na najdalsza pozycje taka, ze suma elementow przed nia jest <= k if(!root){ append(val); return; } SplayTree st = split_from(k); append(val); extend(st); } SplayTree split_from(node_size_t k){ //odrywa podsciezke o ind. [k, k+1, ...] SplayTree res; if(!splay_kth(k)) return res; Node *left_ = root->left_; root->set_left(NULL); root->resize(); if(left_) left_->p_ = NULL; res.root = root; root = left_; return res; } void extend(SplayTree &rh){ //wchlania sciezke rh (dokleja na koniec) assert(this != &rh); if(!rh.root) return; if(!root) root = rh.root; else append_(rh.root); rh.root = NULL; } SplayTree cutLeftChild() { SplayTree res; res.root = root->left_; if (res.root) { res.root->p_ = NULL; } root->set_left(NULL); return res; } // zjedz conajmniej s na sume najwiekszych szprot SplayTree zjedzSzproty(ll s) { // zwraca splay niezjedzonych szprot auto v = root; while (1) { if (_subtreeVal_size(v->right_) >= s) { v = v->right_; } else if (v->val + _subtreeVal_size(v->right_) >= s) { break; } else { s -= v->val + _subtreeVal_size(v->right_); v = v->left_; } } splay(v); return cutLeftChild(); } int size() { return root->subtree; } ll sizeVal() { return root->subtreeVal; } ll minimum() { auto v = root; while (v->left_) { v = v->left_; } splay(v); return v->val; } }; SplayTree t; vector<SplayTree> zjedzone; int solve(ll s, ll k) { // printf("query %lld %lld\n", s, k); // printf("zywe "); t.debug(); int res = 0; while (s < k) { ll needed = k; auto lb = t.lower_bound(s); needed = min(needed, lb->val + 1); needed -= s; // printf("needed %lld\n", needed); ll possibru = _subtreeVal_size(lb->left_); // printf("needed %lld possibru %lld\n", needed, possibru); if (possibru < needed) { res = -1; break; } auto leweSzproty = t.cutLeftChild(); // printf("do zjedzenia %lld\n", leweSzproty.sizeVal()); // printf("lewe "); leweSzproty.debug(); // printf("niejadalne "); t.debug(); auto niezjedzoneSzproty = leweSzproty.zjedzSzproty(needed); // printf("niezjedzone %lld\n", niezjedzoneSzproty.sizeVal()); // printf("niezjedzone "); niezjedzoneSzproty.debug(); // printf("zjedzone "); leweSzproty.debug(); res += leweSzproty.size(); s += leweSzproty.sizeVal(); // printf("zjedzone %lld\n", leweSzproty.sizeVal()); zjedzone.push_back(leweSzproty); niezjedzoneSzproty.extend(t); t.swap(niezjedzoneSzproty); } // printf("ocalale "); t.debug(); while (!zjedzone.empty()) { auto sp = zjedzone.back(); zjedzone.pop_back(); // printf("przywracam "); sp.debug(); t.lower_bound(sp.minimum() + 1); auto mniejsze = t.cutLeftChild(); mniejsze.extend(sp); mniejsze.extend(t); t.swap(mniejsze); // printf("po "); t.debug(); } return res; } int main() { int n; scanf("%d", &n); while (n--) { ll a; scanf("%lld", &a); t.insert(a); } t.insert(1000000000000000001ll); int m; scanf("%d", &m); while (m--) { int q; scanf("%d", &q); if (q == 1) { ll s, k; scanf("%lld %lld", &s, &k); printf("%d\n", solve(s, k)); } else { ll w; scanf("%lld", &w); if (q == 2) { t.insert(w); } else { t.remove(w); } // vector<ll> v; // t.dump_inorder(v); // for (auto i : v) printf("%lld ", i); printf("\n"); } } }
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 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 | #include <bits/stdc++.h> using namespace std; using ll = long long; using node_size_t = ll; // splej z biblioteczki UJ struct SplayTree { #define _subtree_size(x) ((x)?(x)->subtree : 0ll) #define _subtreeVal_size(x) ((x)?x->subtreeVal : 0ll) struct Node{ ll val; Node* left_, *right_, *p_; //tych (i dalszych) pol nie dotykac bezposrednio Node(const ll& v): val(v), left_(NULL), right_(NULL), p_(NULL){ resize(); } void set_left(Node *x){ left_ = x; if(x) x->p_ = this; resize(); } void set_right(Node *x){ right_ = x; if(x) x->p_ = this; resize();} //template<> node_size_t SplayTree<KTH_TEST>::Node::node_size(){ ... } node_size_t node_size(){return 1;} node_size_t subtree; ll subtreeVal; void resize(){ subtree = _subtree_size(left_) + _subtree_size(right_) + node_size(); subtreeVal = _subtreeVal_size(left_) + _subtreeVal_size(right_) + val; } void rotate() { Node *parent = p_; this->p_ = parent->p_; if(parent->p_) { if (parent==parent->p_->left_) parent->p_->set_left(this); else parent->p_->set_right(this); } if (this==parent->left_) { parent->set_left(this->right_); set_right(parent); } else { parent->set_right(this->left_); set_left(parent); } } void dump_inorder_(vector<ll>&out){ /* DUMP */ if(left_) left_->dump_inorder_(out); /* DUMP */ out.push_back(val); /* DUMP */ if(right_) right_->dump_inorder_(out); /* DUMP */ } /* DUMP */ void clear_(){ /* CLEAR */ if(left_) {left_->clear_(); delete left_;} /* CLEAR */ if(right_) {right_->clear_(); delete right_;} /* CLEAR */ } /* CLEAR */ } *root; void dump_inorder(vector<ll>&out){ /* DUMP */ if(root) root->dump_inorder_(out); /* DUMP */ } /* DUMP */ SplayTree():root(NULL){} //UWAGA - piszac deepcopy pamietaj o pushowaniu // ~SplayTree(){ clear(); } /* CLEAR */ void swap(SplayTree& rh){ std::swap(root, rh.root); } void clear(){ /* CLEAR */ if(!root) return; /* CLEAR */ root->clear_(); /* CLEAR */ delete root; /* CLEAR */ root = NULL; /* CLEAR */ } Node* splay(Node *v) { //uwaga, zmienia korzen drzewa while(v->p_) { if (v->p_->p_ && ((v==v->p_->left_) == (v->p_==v->p_->p_->left_))) v->p_->rotate(); v->rotate(); } return root = v; } void debug() { vector<ll> v; dump_inorder(v); for (auto i : v) printf("%lld ", i); printf("\n"); } /* MULTISET */ Node* lower_bound(const ll &x){ Node *v = root, *res = NULL, *prev = NULL; while(v){ prev = v; if(v->val < x) v = v->right_; else { res = v; v = v->left_; } } if (res) splay(res); else if(prev) splay(prev); return res; } Node* insert(const ll& x){ Node *v = new Node(x); while(root){ if(x < root->val){ if(root->left_) root = root->left_; else { root->set_left(v); break; } } else { if(root->right_) root = root->right_; else { root->set_right(v); break; } } } return splay(v); }; void remove(const ll& x){ lower_bound(x); auto l = root->left_; auto r = root->right_; root->set_left(NULL); root->set_right(NULL); if (l) l->p_ = NULL; if (r) r->p_ = NULL; if (l == NULL) { root = r; return; } root = l; if (r) { append_(r); } } /* PATH */ // Pierwszy wierzcholek taki ze suma rozmiarow wierzcholkow w porzadku // inorder do tego wierzcholka wlacznie jest > k. Jesli rozmiary sa = 1, // to jest to k-ty wierzcholek w porzadku inorder. (Liczac od 0) Node *splay_kth(node_size_t k){ Node *v = root; if (_subtree_size(v) <= k) return NULL; for(;;) { if(!v) return NULL; if (_subtree_size(v->left_) <= k && _subtree_size(v->left_) + v->node_size()>k){ return splay(v); } if (_subtree_size(v->left_) <= k) { k -= (v->node_size()+_subtree_size(v->left_)); v = v->right_; } else v = v->left_; } } void append_(Node* nw){ if(!nw) return; Node *v = root; while(v){ if(v->right_) v = v->right_; else break; } if(v) v->set_right(nw); splay(nw); } void append(const ll& val){ append_(new Node(val)); } void insert_at(node_size_t k, const ll& val){ //val bedzie na ktej pozycji // (od zera). UWAGA - Jesli k jest duze, to val bedzie appendowane. // Jesli rozmiary wezlow nie sa jednostkowe, to val zostanie wsadzone // na najdalsza pozycje taka, ze suma elementow przed nia jest <= k if(!root){ append(val); return; } SplayTree st = split_from(k); append(val); extend(st); } SplayTree split_from(node_size_t k){ //odrywa podsciezke o ind. [k, k+1, ...] SplayTree res; if(!splay_kth(k)) return res; Node *left_ = root->left_; root->set_left(NULL); root->resize(); if(left_) left_->p_ = NULL; res.root = root; root = left_; return res; } void extend(SplayTree &rh){ //wchlania sciezke rh (dokleja na koniec) assert(this != &rh); if(!rh.root) return; if(!root) root = rh.root; else append_(rh.root); rh.root = NULL; } SplayTree cutLeftChild() { SplayTree res; res.root = root->left_; if (res.root) { res.root->p_ = NULL; } root->set_left(NULL); return res; } // zjedz conajmniej s na sume najwiekszych szprot SplayTree zjedzSzproty(ll s) { // zwraca splay niezjedzonych szprot auto v = root; while (1) { if (_subtreeVal_size(v->right_) >= s) { v = v->right_; } else if (v->val + _subtreeVal_size(v->right_) >= s) { break; } else { s -= v->val + _subtreeVal_size(v->right_); v = v->left_; } } splay(v); return cutLeftChild(); } int size() { return root->subtree; } ll sizeVal() { return root->subtreeVal; } ll minimum() { auto v = root; while (v->left_) { v = v->left_; } splay(v); return v->val; } }; SplayTree t; vector<SplayTree> zjedzone; int solve(ll s, ll k) { // printf("query %lld %lld\n", s, k); // printf("zywe "); t.debug(); int res = 0; while (s < k) { ll needed = k; auto lb = t.lower_bound(s); needed = min(needed, lb->val + 1); needed -= s; // printf("needed %lld\n", needed); ll possibru = _subtreeVal_size(lb->left_); // printf("needed %lld possibru %lld\n", needed, possibru); if (possibru < needed) { res = -1; break; } auto leweSzproty = t.cutLeftChild(); // printf("do zjedzenia %lld\n", leweSzproty.sizeVal()); // printf("lewe "); leweSzproty.debug(); // printf("niejadalne "); t.debug(); auto niezjedzoneSzproty = leweSzproty.zjedzSzproty(needed); // printf("niezjedzone %lld\n", niezjedzoneSzproty.sizeVal()); // printf("niezjedzone "); niezjedzoneSzproty.debug(); // printf("zjedzone "); leweSzproty.debug(); res += leweSzproty.size(); s += leweSzproty.sizeVal(); // printf("zjedzone %lld\n", leweSzproty.sizeVal()); zjedzone.push_back(leweSzproty); niezjedzoneSzproty.extend(t); t.swap(niezjedzoneSzproty); } // printf("ocalale "); t.debug(); while (!zjedzone.empty()) { auto sp = zjedzone.back(); zjedzone.pop_back(); // printf("przywracam "); sp.debug(); t.lower_bound(sp.minimum() + 1); auto mniejsze = t.cutLeftChild(); mniejsze.extend(sp); mniejsze.extend(t); t.swap(mniejsze); // printf("po "); t.debug(); } return res; } int main() { int n; scanf("%d", &n); while (n--) { ll a; scanf("%lld", &a); t.insert(a); } t.insert(1000000000000000001ll); int m; scanf("%d", &m); while (m--) { int q; scanf("%d", &q); if (q == 1) { ll s, k; scanf("%lld %lld", &s, &k); printf("%d\n", solve(s, k)); } else { ll w; scanf("%lld", &w); if (q == 2) { t.insert(w); } else { t.remove(w); } // vector<ll> v; // t.dump_inorder(v); // for (auto i : v) printf("%lld ", i); printf("\n"); } } } |