#include <bits/stdc++.h> using namespace std; #define REP(i,a,b) for (int i = (a); i <= (b); ++i) #define REPD(i,a,b) for (int i = (a); i >= (b); --i) #define FORI(i,n) REP(i,1,n) #define FOR(i,n) REP(i,0,int(n)-1) #define mp make_pair #define pb push_back #define pii pair<int,int> #define vi vector<int> #define ll long long #define SZ(x) int((x).size()) #define DBG(v) cerr << #v << " = " << (v) << endl; #define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++) #define fi first #define se second const int OP_MAX = 1; const int N = 300300; int n; ll w[N]; int used[N]; int op; void add(ll w_cur) { op++; w[n++] = w_cur; } void rmv(ll w_cur) { op++; FOR(i,n) if (w[i] == w_cur) { swap(w[i], w[n-1]); n--; return; } } void upd() { sort(w,w+n); w[n] = 1e13; } int qit; int query(ll w_ini, ll w_fin) { qit++; if (op >= OP_MAX) upd(); int ret = 0; while(w_ini < w_fin) { //cerr << w_ini << " " << w_fin << "\n"; int pos = lower_bound(w,w+n,w_ini) - w; if (pos == 0) return -1; // todo check -1 before pos--; if (w[pos]*2 >= w_ini) { used[pos] = qit; w_ini += w[pos]; //cerr << "eating big " << w[pos] << "\n"; ret++; } else { ll goal = min(w_fin, w[pos+1]+1); //cerr << "goal is " << goal << "\n"; while (w_ini < goal) { while(used[pos] == qit) { if (pos == 0) return -1; pos--; } w_ini += w[pos]; ret++; used[pos] = qit; } } } return ret; } int main() { scanf("%d", &n); FOR(i,n) scanf("%lld", &w[i]); upd(); int qn; scanf("%d", &qn); FOR(qi,qn) { int qt; scanf("%d", &qt); if (qt == 1) { ll w_ini, w_fin; scanf("%lld%lld", &w_ini, &w_fin); printf("%d\n", query(w_ini, w_fin)); } else if (qt == 2) { ll w_cur; scanf("%lld", &w_cur); add(w_cur); } else { ll w_cur; scanf("%lld", &w_cur); rmv(w_cur); } } 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 | #include <bits/stdc++.h> using namespace std; #define REP(i,a,b) for (int i = (a); i <= (b); ++i) #define REPD(i,a,b) for (int i = (a); i >= (b); --i) #define FORI(i,n) REP(i,1,n) #define FOR(i,n) REP(i,0,int(n)-1) #define mp make_pair #define pb push_back #define pii pair<int,int> #define vi vector<int> #define ll long long #define SZ(x) int((x).size()) #define DBG(v) cerr << #v << " = " << (v) << endl; #define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++) #define fi first #define se second const int OP_MAX = 1; const int N = 300300; int n; ll w[N]; int used[N]; int op; void add(ll w_cur) { op++; w[n++] = w_cur; } void rmv(ll w_cur) { op++; FOR(i,n) if (w[i] == w_cur) { swap(w[i], w[n-1]); n--; return; } } void upd() { sort(w,w+n); w[n] = 1e13; } int qit; int query(ll w_ini, ll w_fin) { qit++; if (op >= OP_MAX) upd(); int ret = 0; while(w_ini < w_fin) { //cerr << w_ini << " " << w_fin << "\n"; int pos = lower_bound(w,w+n,w_ini) - w; if (pos == 0) return -1; // todo check -1 before pos--; if (w[pos]*2 >= w_ini) { used[pos] = qit; w_ini += w[pos]; //cerr << "eating big " << w[pos] << "\n"; ret++; } else { ll goal = min(w_fin, w[pos+1]+1); //cerr << "goal is " << goal << "\n"; while (w_ini < goal) { while(used[pos] == qit) { if (pos == 0) return -1; pos--; } w_ini += w[pos]; ret++; used[pos] = qit; } } } return ret; } int main() { scanf("%d", &n); FOR(i,n) scanf("%lld", &w[i]); upd(); int qn; scanf("%d", &qn); FOR(qi,qn) { int qt; scanf("%d", &qt); if (qt == 1) { ll w_ini, w_fin; scanf("%lld%lld", &w_ini, &w_fin); printf("%d\n", query(w_ini, w_fin)); } else if (qt == 2) { ll w_cur; scanf("%lld", &w_cur); add(w_cur); } else { ll w_cur; scanf("%lld", &w_cur); rmv(w_cur); } } return 0; } |