#include <bits/stdc++.h> #define fi first #define se second #define ll long long #define ld long double #define pb push_back #define vi vector<int> #define vll vector<ll> using namespace std; struct fish{ ll w; bool l = true; }; bool operator<(fish a, fish b) { if(a.w != b.w) return a.w < b.w; else return a.l > b.l; } struct event{ int c; ll s, k, w; }; vector<fish> W; vector<event> E; priority_queue<ll> L; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin>>n; W.resize(n); for(int i=0; i<n; i++) cin>>W[i].w; int q; cin>>q; E.resize(q); for(int i=0; i<q; i++) { cin>>E[i].c; if(E[i].c == 1) { cin>>E[i].s>>E[i].k; } else if (E[i].c == 2) { cin>>E[i].w; fish f; f.w = E[i].w; f.l = false; W.pb(f); } else { cin>>E[i].w; } } sort(W.begin(), W.end()); ll z; int p, r; for(event e : E) { if(e.c == 1) { while(!L.empty()) L.pop(); p = 0; r = 0; while(p < W.size() && W[p].w < e.s) { if(W[p].l) L.push(W[p].w); p++; } while(!L.empty() && e.s < e.k) { z = L.top(); L.pop(); e.s += z; while(p < W.size() && W[p].w < e.s) { if(W[p].l) L.push(W[p].w); p++; } r++; } if(e.s >= e.k) cout<<r<<endl; else cout<<-1<<endl; } else if(e.c == 2) { fish tmp; tmp.w = e.w; tmp.l = false; auto lower = std::lower_bound(W.begin(), W.end(), tmp); lower->l=true; } else { fish tmp; tmp.w = e.w; tmp.l = false; auto lower = std::lower_bound(W.begin(), W.end(), tmp); lower--; lower->l=false; } } 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 | #include <bits/stdc++.h> #define fi first #define se second #define ll long long #define ld long double #define pb push_back #define vi vector<int> #define vll vector<ll> using namespace std; struct fish{ ll w; bool l = true; }; bool operator<(fish a, fish b) { if(a.w != b.w) return a.w < b.w; else return a.l > b.l; } struct event{ int c; ll s, k, w; }; vector<fish> W; vector<event> E; priority_queue<ll> L; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin>>n; W.resize(n); for(int i=0; i<n; i++) cin>>W[i].w; int q; cin>>q; E.resize(q); for(int i=0; i<q; i++) { cin>>E[i].c; if(E[i].c == 1) { cin>>E[i].s>>E[i].k; } else if (E[i].c == 2) { cin>>E[i].w; fish f; f.w = E[i].w; f.l = false; W.pb(f); } else { cin>>E[i].w; } } sort(W.begin(), W.end()); ll z; int p, r; for(event e : E) { if(e.c == 1) { while(!L.empty()) L.pop(); p = 0; r = 0; while(p < W.size() && W[p].w < e.s) { if(W[p].l) L.push(W[p].w); p++; } while(!L.empty() && e.s < e.k) { z = L.top(); L.pop(); e.s += z; while(p < W.size() && W[p].w < e.s) { if(W[p].l) L.push(W[p].w); p++; } r++; } if(e.s >= e.k) cout<<r<<endl; else cout<<-1<<endl; } else if(e.c == 2) { fish tmp; tmp.w = e.w; tmp.l = false; auto lower = std::lower_bound(W.begin(), W.end(), tmp); lower->l=true; } else { fish tmp; tmp.w = e.w; tmp.l = false; auto lower = std::lower_bound(W.begin(), W.end(), tmp); lower--; lower->l=false; } } return 0; } |