#include <cstdio> #include <set> #include <map> #include <stack> using namespace std; int main() { int n, q, t; long long a, b; map<long long, int> ms; stack<long long> S; scanf("%d", &n); while (n--) { scanf("%lld", &a); auto it = ms.find(a); if (it != ms.end()) it->second++; else ms.insert(pair<long long, int>(a, 1)); } scanf("%d", &q); while (q--) { scanf("%d", &t); if (t == 1) { scanf("%lld%lld", &a, &b); int res = 0; while (a < b) { if (ms.empty() || ms.begin()->first >= a) { res = -1; break; } auto it = ms.lower_bound(a); --it; a += it->first; S.push(it->first); it->second--; if (it-> second == 0) ms.erase(it); ++res; } while(!S.empty()) { a = S.top(); S.pop(); auto it = ms.find(a); if (it != ms.end()) it->second++; else ms.insert(pair<long long, int>(a, 1)); } printf("%d\n", res); } else if (t == 2) { scanf("%lld", &a); auto it = ms.find(a); if (it != ms.end()) it->second++; else ms.insert(pair<long long, int>(a, 1)); } else if (t == 3) { scanf("%lld", &a); auto it = ms.find(a); it->second--; if (it-> second == 0) ms.erase(it); } } }
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 | #include <cstdio> #include <set> #include <map> #include <stack> using namespace std; int main() { int n, q, t; long long a, b; map<long long, int> ms; stack<long long> S; scanf("%d", &n); while (n--) { scanf("%lld", &a); auto it = ms.find(a); if (it != ms.end()) it->second++; else ms.insert(pair<long long, int>(a, 1)); } scanf("%d", &q); while (q--) { scanf("%d", &t); if (t == 1) { scanf("%lld%lld", &a, &b); int res = 0; while (a < b) { if (ms.empty() || ms.begin()->first >= a) { res = -1; break; } auto it = ms.lower_bound(a); --it; a += it->first; S.push(it->first); it->second--; if (it-> second == 0) ms.erase(it); ++res; } while(!S.empty()) { a = S.top(); S.pop(); auto it = ms.find(a); if (it != ms.end()) it->second++; else ms.insert(pair<long long, int>(a, 1)); } printf("%d\n", res); } else if (t == 2) { scanf("%lld", &a); auto it = ms.find(a); if (it != ms.end()) it->second++; else ms.insert(pair<long long, int>(a, 1)); } else if (t == 3) { scanf("%lld", &a); auto it = ms.find(a); it->second--; if (it-> second == 0) ms.erase(it); } } } |