#include <algorithm> #include <map> #include <iostream> #include <utility> #include <vector> using namespace std; namespace { using ll = long long; struct Query { int type; ll a; ll b; }; struct Helper { int n = 0; vector<ll> data; vector<pair<int, ll>> undo; void init(int n_) { n = n_; data.clear(); data.resize(n); } void set(int i, ll v) { data[i] = v; } ll operator[](int i) { return data[i]; } int first_at_least(ll v) { for (int i = 0; i < n; ++i) { if (data[i] >= v) return i; } return n; } void reset() { reverse(undo.begin(), undo.end()); for (auto const& [i, v]: undo) { set(i, v); } undo.clear(); } pair<ll, int> get(int a, int b) { ll sum = 0; int cnt = 0; for (int i = a; i < b; ++i) { if (data[i] == 0) continue; sum += data[i]; ++cnt; } return {sum, cnt}; } int find_left(int a, ll target) { ll sum = 0; --a; while (sum < target && a >= 0) { sum += data[a]; --a; } if (sum >= target) return a + 1; return -1; } void silence(int a, int b) { for (int i = a; i < b; ++i) { undo.emplace_back(i, data[i]); data[i] = 0; } } }; struct Solver { vector<ll> all_fish; Helper helper; int solve_one(ll a, ll b) { helper.reset(); int res = 0; while (a < b) { auto q = helper.first_at_least(a); auto t = b; if (q < helper.n) t = min(t, helper[q] + 1); auto p = helper.find_left(q, t - a); if (p < 0) return -1; auto [sum, cnt] = helper.get(p, q); helper.silence(p, q); a += sum; res += cnt; } return res; } vector<int> operator()(vector<ll> const& fish, vector<Query> const& queries) { all_fish = fish; for (auto const& q: queries) { if (q.type == 2) all_fish.emplace_back(q.a); } sort(all_fish.begin(), all_fish.end()); map<ll, int> where; map<ll, int> where_del; int n = all_fish.size(); for (int i = 0; i < n; ++i) { auto f = all_fish[i]; where.insert({f, i}); where_del.insert({f, i}); } helper.init(n); for (auto const& f: fish) { helper.set(where[f]++, f); } vector<int> res; res.reserve(queries.size()); for (auto const& q: queries) { if (q.type == 1) { res.emplace_back(solve_one(q.a, q.b)); } else if (q.type == 2) { helper.reset(); helper.set(where[q.a]++, q.a); } else if (q.type == 3) { helper.reset(); helper.set(where_del[q.a]++, 0); } } return res; } }; } int main() { iostream::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<ll> fish(n); for (auto& x: fish) cin >> x; int m; cin >> m; vector<Query> queries(m); for (auto& q: queries) { cin >> q.type; if (q.type == 1) { cin >> q.a >> q.b; } else { cin >> q.a; } } auto res = Solver{}(fish, queries); for (auto const& x: res) cout << x << '\n'; 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 | #include <algorithm> #include <map> #include <iostream> #include <utility> #include <vector> using namespace std; namespace { using ll = long long; struct Query { int type; ll a; ll b; }; struct Helper { int n = 0; vector<ll> data; vector<pair<int, ll>> undo; void init(int n_) { n = n_; data.clear(); data.resize(n); } void set(int i, ll v) { data[i] = v; } ll operator[](int i) { return data[i]; } int first_at_least(ll v) { for (int i = 0; i < n; ++i) { if (data[i] >= v) return i; } return n; } void reset() { reverse(undo.begin(), undo.end()); for (auto const& [i, v]: undo) { set(i, v); } undo.clear(); } pair<ll, int> get(int a, int b) { ll sum = 0; int cnt = 0; for (int i = a; i < b; ++i) { if (data[i] == 0) continue; sum += data[i]; ++cnt; } return {sum, cnt}; } int find_left(int a, ll target) { ll sum = 0; --a; while (sum < target && a >= 0) { sum += data[a]; --a; } if (sum >= target) return a + 1; return -1; } void silence(int a, int b) { for (int i = a; i < b; ++i) { undo.emplace_back(i, data[i]); data[i] = 0; } } }; struct Solver { vector<ll> all_fish; Helper helper; int solve_one(ll a, ll b) { helper.reset(); int res = 0; while (a < b) { auto q = helper.first_at_least(a); auto t = b; if (q < helper.n) t = min(t, helper[q] + 1); auto p = helper.find_left(q, t - a); if (p < 0) return -1; auto [sum, cnt] = helper.get(p, q); helper.silence(p, q); a += sum; res += cnt; } return res; } vector<int> operator()(vector<ll> const& fish, vector<Query> const& queries) { all_fish = fish; for (auto const& q: queries) { if (q.type == 2) all_fish.emplace_back(q.a); } sort(all_fish.begin(), all_fish.end()); map<ll, int> where; map<ll, int> where_del; int n = all_fish.size(); for (int i = 0; i < n; ++i) { auto f = all_fish[i]; where.insert({f, i}); where_del.insert({f, i}); } helper.init(n); for (auto const& f: fish) { helper.set(where[f]++, f); } vector<int> res; res.reserve(queries.size()); for (auto const& q: queries) { if (q.type == 1) { res.emplace_back(solve_one(q.a, q.b)); } else if (q.type == 2) { helper.reset(); helper.set(where[q.a]++, q.a); } else if (q.type == 3) { helper.reset(); helper.set(where_del[q.a]++, 0); } } return res; } }; } int main() { iostream::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<ll> fish(n); for (auto& x: fish) cin >> x; int m; cin >> m; vector<Query> queries(m); for (auto& q: queries) { cin >> q.type; if (q.type == 1) { cin >> q.a >> q.b; } else { cin >> q.a; } } auto res = Solver{}(fish, queries); for (auto const& x: res) cout << x << '\n'; return 0; } |