#include <bits/stdc++.h> using namespace std; struct Query { long long type, i1, i2; int fish_index; }; struct Fish { long long weight, index; int query_index; bool in_pond; }; bool operator<(const Fish &lhs, const Fish &rhs) { if (lhs.weight < rhs.weight) return true; return false; } void read(multiset<Fish> &pond, vector<Fish> &fish, vector<Query> &queries) { int N; cin >> N; for (int i = 0; i < N; i++) { Fish f; long long weight; cin >> weight; f.weight = weight; f.in_pond = true; fish.push_back(f); } int Q; cin >> Q; for (int i = 0; i < Q; i++) { long long t, i1, i2; cin >> t; if (t == 1) cin >> i1 >> i2; else cin >> i1; if (t == 2) { Fish f; f.weight = i1; f.in_pond = false; f.query_index = i; fish.push_back(f); } queries.push_back({t, i1, i2}); } sort(fish.begin(), fish.end()); for (int i = 0; i < fish.size(); i++) { fish[i].index = i; if (fish[i].in_pond) { pond.insert(fish[i]); } else { queries[fish[i].query_index].fish_index = i; } } } int LSB(int x) { return x & (-x); } void add_value(vector<long long> &prefix, int index, long long value) { int i = index; while (i < prefix.size()) { prefix[i] += value; i += LSB(i); } } long long get_value(vector<long long> &prefix, int index) { int i = index; long long sum = 0; while (i > 0) { sum += prefix[i]; i -= LSB(i); } return sum; } void construct_prefix(vector<Fish> &fish, vector<long long> &prefix) { fill(prefix.begin(), prefix.end(), 0); prefix[0] = 0; for (int i = 0; i < fish.size(); i++) { if (fish[i].in_pond) add_value(prefix, i + 1, fish[i].weight); } } void add_fish(Fish f, vector<Fish> &fish, multiset<Fish> &pond, vector<long long> &prefix) { fish[f.index].in_pond = true; pond.insert(f); add_value(prefix, f.index + 1, f.weight); } void remove_fish(long long weight, vector<Fish> &fish, multiset<Fish> &pond, vector<long long> &prefix) { Fish to_find; to_find.weight = weight; to_find.index = -1; auto f = pond.find(to_find); add_value(prefix, (*f).index + 1, -(*f).weight); pond.erase(f); fish[f->index].in_pond = false; } int handle_attack( long long start_weight, long long end_weight, vector<Fish> &fish, multiset<Fish> &pond, const vector<long long> &prefix) { if (start_weight >= end_weight) return 0; int eaten_count = 0; long long weight = start_weight; Fish attacker; attacker.weight = end_weight; vector<long long> eaten(fish.size() + 2); fill(eaten.begin(), eaten.end(), 0); while (weight < end_weight) { Fish current; current.weight = weight; current.index = std::numeric_limits<int>::max(); auto next = pond.lower_bound(current); long long weight_diff; int index; if (next == pond.end()) { weight_diff = end_weight - 1 - weight; index = fish.size(); } else { weight_diff = min(next->weight, end_weight - 1) - weight; index = next->index; } int i = index - 1; long long sum = 0; // cout << weight << " " << weight_diff << "\n"; while (i >= 0 && sum <= weight_diff) { // cout << i << " i " << get_value(eaten, i + 1) << "\n"; if (fish[i].in_pond && get_value(eaten, i + 1) == 0) { sum += fish[i].weight; eaten_count++; } i--; } // cout << i << " " << index << " " << sum << "\n\n"; add_value(eaten, i + 2, 1); add_value(eaten, index + 1, -1); weight += sum; if (weight >= end_weight) return eaten_count; if (sum <= weight_diff) return -1; } return eaten_count; } void solve() { vector<Fish> fish; vector<Query> queries; multiset<Fish> pond; read(pond, fish, queries); vector<long long> prefix(fish.size() + 1); construct_prefix(fish, prefix); for (Query q : queries) { if (q.type == 2) add_fish(fish[q.fish_index], fish, pond, prefix); else if (q.type == 3) remove_fish(q.i1, fish, pond, prefix); else cout << handle_attack(q.i1, q.i2, fish, pond, prefix) << "\n"; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); }
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 | #include <bits/stdc++.h> using namespace std; struct Query { long long type, i1, i2; int fish_index; }; struct Fish { long long weight, index; int query_index; bool in_pond; }; bool operator<(const Fish &lhs, const Fish &rhs) { if (lhs.weight < rhs.weight) return true; return false; } void read(multiset<Fish> &pond, vector<Fish> &fish, vector<Query> &queries) { int N; cin >> N; for (int i = 0; i < N; i++) { Fish f; long long weight; cin >> weight; f.weight = weight; f.in_pond = true; fish.push_back(f); } int Q; cin >> Q; for (int i = 0; i < Q; i++) { long long t, i1, i2; cin >> t; if (t == 1) cin >> i1 >> i2; else cin >> i1; if (t == 2) { Fish f; f.weight = i1; f.in_pond = false; f.query_index = i; fish.push_back(f); } queries.push_back({t, i1, i2}); } sort(fish.begin(), fish.end()); for (int i = 0; i < fish.size(); i++) { fish[i].index = i; if (fish[i].in_pond) { pond.insert(fish[i]); } else { queries[fish[i].query_index].fish_index = i; } } } int LSB(int x) { return x & (-x); } void add_value(vector<long long> &prefix, int index, long long value) { int i = index; while (i < prefix.size()) { prefix[i] += value; i += LSB(i); } } long long get_value(vector<long long> &prefix, int index) { int i = index; long long sum = 0; while (i > 0) { sum += prefix[i]; i -= LSB(i); } return sum; } void construct_prefix(vector<Fish> &fish, vector<long long> &prefix) { fill(prefix.begin(), prefix.end(), 0); prefix[0] = 0; for (int i = 0; i < fish.size(); i++) { if (fish[i].in_pond) add_value(prefix, i + 1, fish[i].weight); } } void add_fish(Fish f, vector<Fish> &fish, multiset<Fish> &pond, vector<long long> &prefix) { fish[f.index].in_pond = true; pond.insert(f); add_value(prefix, f.index + 1, f.weight); } void remove_fish(long long weight, vector<Fish> &fish, multiset<Fish> &pond, vector<long long> &prefix) { Fish to_find; to_find.weight = weight; to_find.index = -1; auto f = pond.find(to_find); add_value(prefix, (*f).index + 1, -(*f).weight); pond.erase(f); fish[f->index].in_pond = false; } int handle_attack( long long start_weight, long long end_weight, vector<Fish> &fish, multiset<Fish> &pond, const vector<long long> &prefix) { if (start_weight >= end_weight) return 0; int eaten_count = 0; long long weight = start_weight; Fish attacker; attacker.weight = end_weight; vector<long long> eaten(fish.size() + 2); fill(eaten.begin(), eaten.end(), 0); while (weight < end_weight) { Fish current; current.weight = weight; current.index = std::numeric_limits<int>::max(); auto next = pond.lower_bound(current); long long weight_diff; int index; if (next == pond.end()) { weight_diff = end_weight - 1 - weight; index = fish.size(); } else { weight_diff = min(next->weight, end_weight - 1) - weight; index = next->index; } int i = index - 1; long long sum = 0; // cout << weight << " " << weight_diff << "\n"; while (i >= 0 && sum <= weight_diff) { // cout << i << " i " << get_value(eaten, i + 1) << "\n"; if (fish[i].in_pond && get_value(eaten, i + 1) == 0) { sum += fish[i].weight; eaten_count++; } i--; } // cout << i << " " << index << " " << sum << "\n\n"; add_value(eaten, i + 2, 1); add_value(eaten, index + 1, -1); weight += sum; if (weight >= end_weight) return eaten_count; if (sum <= weight_diff) return -1; } return eaten_count; } void solve() { vector<Fish> fish; vector<Query> queries; multiset<Fish> pond; read(pond, fish, queries); vector<long long> prefix(fish.size() + 1); construct_prefix(fish, prefix); for (Query q : queries) { if (q.type == 2) add_fish(fish[q.fish_index], fish, pond, prefix); else if (q.type == 3) remove_fish(q.i1, fish, pond, prefix); else cout << handle_attack(q.i1, q.i2, fish, pond, prefix) << "\n"; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); } |