#include <algorithm> #include <cstdint> #include <iostream> #include <optional> #include <vector> struct Data { Data(uint64_t w) : w(w) {} uint64_t w{0}; bool operator<(const Data &other) const { return w < other.w; } }; void Print(const std::vector<Data> &data) { for (const auto &i : data) { std::cerr << i.w << " "; } std::cerr << std::endl; } void Process(const std::vector<Data> &data) { uint64_t s = 0; uint64_t k = 0; std::cin >> s; std::cin >> k; if (k <= s) { std::cout << "0" << std::endl; return; } bool used[400000] = {0}; std::vector<Data>::const_iterator jump[400000]; uint64_t counter = 0; while (1) { std::optional<Data> next; auto iter = std::lower_bound(data.begin(), data.end(), s); if (iter != data.end()) { next = *iter; } const auto start = iter; while (1) { if (iter == data.begin()) { std::cout << "-1" << std::endl; return; } iter--; const auto dist = std::distance(data.begin(), iter); if (used[dist]) { iter = jump[dist]; continue; } s += iter->w; used[dist] = true; counter++; if (k <= s) { std::cout << counter << std::endl; return; } if (next && s > (*next).w) { jump[std::distance(data.begin(), start) - 1] = iter; break; } } } } void Add(std::vector<Data> &data) { uint64_t w = 0; std::cin >> w; auto iter = std::upper_bound(data.begin(), data.end(), Data(w)); data.insert(iter, w); } void Remove(std::vector<Data> &data) { uint64_t w = 0; std::cin >> w; auto iter = std::lower_bound(data.begin(), data.end(), w); data.erase(iter); } int main() { size_t n; std::cin >> n; std::vector<Data> data; data.reserve(400000); for (size_t i = 0; i < n; ++i) { uint64_t w = 0; std::cin >> w; data.push_back(Data(w)); } std::sort(data.begin(), data.end()); // DEBUG // Print(data); // unsigned q = 0; std::cin >> q; for (unsigned i = 0; i < q; ++i) { unsigned event = 0; std::cin >> event; switch (event) { case 1: { Process(data); break; } case 2: { Add(data); // DEBUG // Print(data); break; } case 3: { Remove(data); // DEBUG // Print(data); break; } } } 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 | #include <algorithm> #include <cstdint> #include <iostream> #include <optional> #include <vector> struct Data { Data(uint64_t w) : w(w) {} uint64_t w{0}; bool operator<(const Data &other) const { return w < other.w; } }; void Print(const std::vector<Data> &data) { for (const auto &i : data) { std::cerr << i.w << " "; } std::cerr << std::endl; } void Process(const std::vector<Data> &data) { uint64_t s = 0; uint64_t k = 0; std::cin >> s; std::cin >> k; if (k <= s) { std::cout << "0" << std::endl; return; } bool used[400000] = {0}; std::vector<Data>::const_iterator jump[400000]; uint64_t counter = 0; while (1) { std::optional<Data> next; auto iter = std::lower_bound(data.begin(), data.end(), s); if (iter != data.end()) { next = *iter; } const auto start = iter; while (1) { if (iter == data.begin()) { std::cout << "-1" << std::endl; return; } iter--; const auto dist = std::distance(data.begin(), iter); if (used[dist]) { iter = jump[dist]; continue; } s += iter->w; used[dist] = true; counter++; if (k <= s) { std::cout << counter << std::endl; return; } if (next && s > (*next).w) { jump[std::distance(data.begin(), start) - 1] = iter; break; } } } } void Add(std::vector<Data> &data) { uint64_t w = 0; std::cin >> w; auto iter = std::upper_bound(data.begin(), data.end(), Data(w)); data.insert(iter, w); } void Remove(std::vector<Data> &data) { uint64_t w = 0; std::cin >> w; auto iter = std::lower_bound(data.begin(), data.end(), w); data.erase(iter); } int main() { size_t n; std::cin >> n; std::vector<Data> data; data.reserve(400000); for (size_t i = 0; i < n; ++i) { uint64_t w = 0; std::cin >> w; data.push_back(Data(w)); } std::sort(data.begin(), data.end()); // DEBUG // Print(data); // unsigned q = 0; std::cin >> q; for (unsigned i = 0; i < q; ++i) { unsigned event = 0; std::cin >> event; switch (event) { case 1: { Process(data); break; } case 2: { Add(data); // DEBUG // Print(data); break; } case 3: { Remove(data); // DEBUG // Print(data); break; } } } return 0; } |