#include <bits/stdc++.h> #include <iostream> using namespace std; pair<long long, pair<int, long long>> tree[2300000]; //prefix sum, nr active, min on interval int tree_size = 1; set< pair<long long, int>> active, inactive; // weight, index vector<tuple<int, long long, long long>> inst; // type, weight, + aim_weight vector< pair<long long,bool> > fish; //weight, active int n, q, type, N; long long weight, aim_weight; void insert_to_tree(int index, long long w) { int beg = index + tree_size; int end = N + tree_size - 1; int add = (w < 0) ? -1 : 1; tree[beg].first += w; tree[beg].second.first += add; tree[beg].second.second = tree[beg].first; if (end != beg) { tree[end].first += w; tree[end].second.first += add; tree[end].second.second = tree[end].first; } while (beg/2 != end/2) { if (beg % 2 == 0) { tree[beg+1].first += w; tree[beg+1].second.first += add; tree[beg+1].second.second = tree[2*beg+2].second.second + tree[beg+1].first; } if (end % 2 == 1) { tree[end-1].first += w; tree[end-1].second.first += add; tree[end-1].second.second = tree[2*end-2].second.second + tree[end-1].first; } beg /= 2; end /= 2; tree[beg].second.second = tree[2*beg].second.second + tree[beg].first; tree[end].second.second = tree[2*end].second.second + tree[end].first; } beg /= 2; while (beg > 0) { tree[beg].second.second = tree[2*beg].second.second + tree[beg].first; beg /=2; } } void init_tree() { N = fish.size(); while (tree_size < N) { tree_size *= 2; } for (auto elem : active) { insert_to_tree(elem.second, elem.first); } } pair<long long, int> query_tree(int index) { if (index == -1) { return {0, 0}; } int beg = index + tree_size; long long ret = 0; int act = 0; while (beg != 0) { ret += tree[beg].first; act += tree[beg].second.first; beg /= 2; } return {ret, act}; } int find_min_tree(long long val) { int current = 1; while (current < tree_size) { val -= tree[current].first; // cout << "VAL " << val << " " << current << endl; if (tree[2*current+1].second.second <= val) { current = 2*current + 1; } else { current = 2*current; } } return current - tree_size; } int binser(long long val, int beg, int end) { int ret = beg; while (beg <= end) { int sr = (beg + end) / 2; if (query_tree(sr).first <= val) { ret = sr; beg = sr + 1; } else { end = sr - 1; } } return ret; } int query_szczupak(long long s, long long k) { if (k <= s) return 0; if (active.empty()) return -1; // cout << "No elo" << endl; stack< pair<int, int>> ranges; stack< pair < pair<long long, int>, pair<long long, int> > > vals; int eated = 0; long long weight = s; long long last_index = -1; while (weight < k) { auto next = active.lower_bound({weight, -1}); int big_ind = (next == active.end()) ? N-1 : next->second-1; long long next_aim = (next == active.end() || next->first >= k) ? k : next->first + 1; // cout << next_aim << " moja waga: " << weight << endl; if (big_ind > last_index) { ranges.push({last_index, big_ind}); auto val1 = query_tree(last_index); auto val2 = query_tree(big_ind); vals.push({val1, val2}); last_index = big_ind; } // cout << "STOS" << ranges.top().first << " " << ranges.top().second << endl; // cout << "STOS" << vals.top().first.first << " " << vals.top().first.second << endl; // cout << "STOS" << vals.top().second.first << " " << vals.top().second.second << endl; while (!ranges.empty() && weight < next_aim) { //cout << ranges.top().first << " " << ranges.top().second << " moja waga: " << weight << endl; long long step = next_aim-weight; long long my_range = vals.top().second.first - vals.top().first.first; if (step >= my_range) { weight += my_range; eated += (vals.top().second.second - vals.top().first.second); ranges.pop(); vals.pop(); } else { long long last_weight = vals.top().second.first - step; int last_index2 = find_min_tree(last_weight); //int last_index2 = binser(last_weight, ranges.top().first, ranges.top().second); // cout << "__________" << endl; // cout << weight << endl; // cout << last_weight << endl; // cout << last_index2 << endl; auto last_query = query_tree(last_index2); // cout << last_query.first << " " << last_query.second << endl; eated += (vals.top().second.second - last_query.second); weight += (vals.top().second.first - last_query.first); if (last_index2 == ranges.top().first) { ranges.pop(); vals.pop(); } else { vals.top().second = last_query; ranges.top().second = last_index2; } } } if (weight < next_aim) { return -1; } } return eated; } void add_szprotka(long long w) { auto next = inactive.lower_bound({w, -1}); int index = next->second; // cout << "dodaje" << endl; // cout << index << endl; active.insert({w, index}); insert_to_tree(index, w); } void rm_szprotka(long long w) { auto next = active.lower_bound({w, -1}); int index = next->second; inactive.insert({w, index}); insert_to_tree(index, -w); } int main() { ios::sync_with_stdio(false); cin >> n; for (int i = 1; i <= n; i++) { cin >> weight; fish.push_back({weight, 1}); } cin >> q; for (int i = 1; i <= q; i++) { cin >> type; if (type == 1) { cin >> weight >> aim_weight; inst.push_back(make_tuple(type, weight, aim_weight)); } else if (type == 2) { cin >> weight; inst.push_back(make_tuple(type, weight, 0)); fish.push_back({weight, 0}); } else { cin >> weight; inst.push_back(make_tuple(type, weight, 0)); } } sort(fish.begin(), fish.end()); //initialize set for (int i = 0; i < fish.size(); i++) { if (fish[i].second) { active.insert({fish[i].first, i}); } else { inactive.insert({fish[i].first, i}); } } // initialize tree init_tree(); for (auto ins : inst) { switch (get<0>(ins)) { case 1: // cout << "ZAPYTANKO " << get<1>(ins) << " " << get<2>(ins) << endl; cout << query_szczupak(get<1>(ins), get<2>(ins)) << endl; break; case 2: add_szprotka(get<1>(ins)); break; default: rm_szprotka(get<1>(ins)); } } 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 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 229 230 | #include <bits/stdc++.h> #include <iostream> using namespace std; pair<long long, pair<int, long long>> tree[2300000]; //prefix sum, nr active, min on interval int tree_size = 1; set< pair<long long, int>> active, inactive; // weight, index vector<tuple<int, long long, long long>> inst; // type, weight, + aim_weight vector< pair<long long,bool> > fish; //weight, active int n, q, type, N; long long weight, aim_weight; void insert_to_tree(int index, long long w) { int beg = index + tree_size; int end = N + tree_size - 1; int add = (w < 0) ? -1 : 1; tree[beg].first += w; tree[beg].second.first += add; tree[beg].second.second = tree[beg].first; if (end != beg) { tree[end].first += w; tree[end].second.first += add; tree[end].second.second = tree[end].first; } while (beg/2 != end/2) { if (beg % 2 == 0) { tree[beg+1].first += w; tree[beg+1].second.first += add; tree[beg+1].second.second = tree[2*beg+2].second.second + tree[beg+1].first; } if (end % 2 == 1) { tree[end-1].first += w; tree[end-1].second.first += add; tree[end-1].second.second = tree[2*end-2].second.second + tree[end-1].first; } beg /= 2; end /= 2; tree[beg].second.second = tree[2*beg].second.second + tree[beg].first; tree[end].second.second = tree[2*end].second.second + tree[end].first; } beg /= 2; while (beg > 0) { tree[beg].second.second = tree[2*beg].second.second + tree[beg].first; beg /=2; } } void init_tree() { N = fish.size(); while (tree_size < N) { tree_size *= 2; } for (auto elem : active) { insert_to_tree(elem.second, elem.first); } } pair<long long, int> query_tree(int index) { if (index == -1) { return {0, 0}; } int beg = index + tree_size; long long ret = 0; int act = 0; while (beg != 0) { ret += tree[beg].first; act += tree[beg].second.first; beg /= 2; } return {ret, act}; } int find_min_tree(long long val) { int current = 1; while (current < tree_size) { val -= tree[current].first; // cout << "VAL " << val << " " << current << endl; if (tree[2*current+1].second.second <= val) { current = 2*current + 1; } else { current = 2*current; } } return current - tree_size; } int binser(long long val, int beg, int end) { int ret = beg; while (beg <= end) { int sr = (beg + end) / 2; if (query_tree(sr).first <= val) { ret = sr; beg = sr + 1; } else { end = sr - 1; } } return ret; } int query_szczupak(long long s, long long k) { if (k <= s) return 0; if (active.empty()) return -1; // cout << "No elo" << endl; stack< pair<int, int>> ranges; stack< pair < pair<long long, int>, pair<long long, int> > > vals; int eated = 0; long long weight = s; long long last_index = -1; while (weight < k) { auto next = active.lower_bound({weight, -1}); int big_ind = (next == active.end()) ? N-1 : next->second-1; long long next_aim = (next == active.end() || next->first >= k) ? k : next->first + 1; // cout << next_aim << " moja waga: " << weight << endl; if (big_ind > last_index) { ranges.push({last_index, big_ind}); auto val1 = query_tree(last_index); auto val2 = query_tree(big_ind); vals.push({val1, val2}); last_index = big_ind; } // cout << "STOS" << ranges.top().first << " " << ranges.top().second << endl; // cout << "STOS" << vals.top().first.first << " " << vals.top().first.second << endl; // cout << "STOS" << vals.top().second.first << " " << vals.top().second.second << endl; while (!ranges.empty() && weight < next_aim) { //cout << ranges.top().first << " " << ranges.top().second << " moja waga: " << weight << endl; long long step = next_aim-weight; long long my_range = vals.top().second.first - vals.top().first.first; if (step >= my_range) { weight += my_range; eated += (vals.top().second.second - vals.top().first.second); ranges.pop(); vals.pop(); } else { long long last_weight = vals.top().second.first - step; int last_index2 = find_min_tree(last_weight); //int last_index2 = binser(last_weight, ranges.top().first, ranges.top().second); // cout << "__________" << endl; // cout << weight << endl; // cout << last_weight << endl; // cout << last_index2 << endl; auto last_query = query_tree(last_index2); // cout << last_query.first << " " << last_query.second << endl; eated += (vals.top().second.second - last_query.second); weight += (vals.top().second.first - last_query.first); if (last_index2 == ranges.top().first) { ranges.pop(); vals.pop(); } else { vals.top().second = last_query; ranges.top().second = last_index2; } } } if (weight < next_aim) { return -1; } } return eated; } void add_szprotka(long long w) { auto next = inactive.lower_bound({w, -1}); int index = next->second; // cout << "dodaje" << endl; // cout << index << endl; active.insert({w, index}); insert_to_tree(index, w); } void rm_szprotka(long long w) { auto next = active.lower_bound({w, -1}); int index = next->second; inactive.insert({w, index}); insert_to_tree(index, -w); } int main() { ios::sync_with_stdio(false); cin >> n; for (int i = 1; i <= n; i++) { cin >> weight; fish.push_back({weight, 1}); } cin >> q; for (int i = 1; i <= q; i++) { cin >> type; if (type == 1) { cin >> weight >> aim_weight; inst.push_back(make_tuple(type, weight, aim_weight)); } else if (type == 2) { cin >> weight; inst.push_back(make_tuple(type, weight, 0)); fish.push_back({weight, 0}); } else { cin >> weight; inst.push_back(make_tuple(type, weight, 0)); } } sort(fish.begin(), fish.end()); //initialize set for (int i = 0; i < fish.size(); i++) { if (fish[i].second) { active.insert({fish[i].first, i}); } else { inactive.insert({fish[i].first, i}); } } // initialize tree init_tree(); for (auto ins : inst) { switch (get<0>(ins)) { case 1: // cout << "ZAPYTANKO " << get<1>(ins) << " " << get<2>(ins) << endl; cout << query_szczupak(get<1>(ins), get<2>(ins)) << endl; break; case 2: add_szprotka(get<1>(ins)); break; default: rm_szprotka(get<1>(ins)); } } return 0; } |