// Michał Wiatrowski #include <iostream> #include <vector> #include <algorithm> using namespace std; typedef unsigned long long uint64; // Tak naprawdę to nie drzewo lol class intervalTree { public: intervalTree(int minimumSize) { size = minimumSize; data = new uint64[size + 1]; data[0] = 0; } ~intervalTree() { delete[] data; } void set(int index, uint64 value) { data[index + 1] = value; } void update() { for (int i = 1; i <= size; ++i) data[i] += data[i-1]; } uint64 sum(int begin, int end) { return data[end+1] - data[begin]; } private: uint64 size; uint64* data; }; struct sequence { int begin; int end; uint64 created; uint64 height; sequence() { begin = 0; end = 0; created = 0; height = 0; } sequence(int b, int e, uint64 c, uint64 h) { begin = b; end = e; created = c; height = h; } }; int main() { ios_base::sync_with_stdio(false); int n, m; cin >> n >> m; int* growth = new int[n]; for (int i = 0; i < n; ++i) cin >> growth[i]; sort(growth, growth + n); intervalTree tree(n); for (int i = 0; i < n; ++i) tree.set(i, growth[i]); tree.update(); vector<sequence> sequences(n); sequences[0] = sequence(0, n-1, 0, 0); int numberOfSequences = 1; for (int cutting = 0; cutting < m; ++cutting) { uint64 d, b; cin >> d >> b; // Binsearch po sekwencjach int begin = -1; int end = numberOfSequences; while (end - begin > 1) { int middle = (begin + end) / 2; sequence& s = sequences[middle]; uint64 dt = d - s.created; if (s.height + dt * growth[s.end] > b) end = middle; else begin = middle; } // Nic nie zostało ścięte if (end == numberOfSequences) { cout << 0 << endl; continue; } int cutSequence = end; // Binsearch wewnątrz sekwencji begin = sequences[cutSequence].begin - 1; end = sequences[cutSequence].end; sequence& s = sequences[cutSequence]; while (end - begin > 1) { int middle = (begin + end) / 2; uint64 dt = d - s.created; if (s.height + dt * growth[middle] > b) end = middle; else begin = middle; } int cut = end; // Obliczanie wyniku uint64 result = 0; result += tree.sum(cut, s.end) * (d - s.created); result += (s.end - cut + 1) * (uint64)s.height; result -= (s.end - cut + 1) * (uint64)b; for (int i = cutSequence + 1; i < numberOfSequences; ++i) { sequence& s2 = sequences[i]; result += tree.sum(s2.begin, s2.end) * (d - s2.created); result += (s2.end - s2.begin + 1) * (uint64)s2.height; result -= (s2.end - s2.begin + 1) * (uint64)b; } cout << result << endl; // Aktualizacja pomocniczej struktury danych if (cut == s.begin) { s.end = n-1; s.height = b; s.created = d; numberOfSequences = cutSequence + 1; } else { s.end = cut-1; sequence& s2 = sequences[cutSequence + 1]; s2.begin = cut; s2.end = n-1; s2.height = b; s2.created = d; numberOfSequences = cutSequence + 2; } } delete[] growth; 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 | // Michał Wiatrowski #include <iostream> #include <vector> #include <algorithm> using namespace std; typedef unsigned long long uint64; // Tak naprawdę to nie drzewo lol class intervalTree { public: intervalTree(int minimumSize) { size = minimumSize; data = new uint64[size + 1]; data[0] = 0; } ~intervalTree() { delete[] data; } void set(int index, uint64 value) { data[index + 1] = value; } void update() { for (int i = 1; i <= size; ++i) data[i] += data[i-1]; } uint64 sum(int begin, int end) { return data[end+1] - data[begin]; } private: uint64 size; uint64* data; }; struct sequence { int begin; int end; uint64 created; uint64 height; sequence() { begin = 0; end = 0; created = 0; height = 0; } sequence(int b, int e, uint64 c, uint64 h) { begin = b; end = e; created = c; height = h; } }; int main() { ios_base::sync_with_stdio(false); int n, m; cin >> n >> m; int* growth = new int[n]; for (int i = 0; i < n; ++i) cin >> growth[i]; sort(growth, growth + n); intervalTree tree(n); for (int i = 0; i < n; ++i) tree.set(i, growth[i]); tree.update(); vector<sequence> sequences(n); sequences[0] = sequence(0, n-1, 0, 0); int numberOfSequences = 1; for (int cutting = 0; cutting < m; ++cutting) { uint64 d, b; cin >> d >> b; // Binsearch po sekwencjach int begin = -1; int end = numberOfSequences; while (end - begin > 1) { int middle = (begin + end) / 2; sequence& s = sequences[middle]; uint64 dt = d - s.created; if (s.height + dt * growth[s.end] > b) end = middle; else begin = middle; } // Nic nie zostało ścięte if (end == numberOfSequences) { cout << 0 << endl; continue; } int cutSequence = end; // Binsearch wewnątrz sekwencji begin = sequences[cutSequence].begin - 1; end = sequences[cutSequence].end; sequence& s = sequences[cutSequence]; while (end - begin > 1) { int middle = (begin + end) / 2; uint64 dt = d - s.created; if (s.height + dt * growth[middle] > b) end = middle; else begin = middle; } int cut = end; // Obliczanie wyniku uint64 result = 0; result += tree.sum(cut, s.end) * (d - s.created); result += (s.end - cut + 1) * (uint64)s.height; result -= (s.end - cut + 1) * (uint64)b; for (int i = cutSequence + 1; i < numberOfSequences; ++i) { sequence& s2 = sequences[i]; result += tree.sum(s2.begin, s2.end) * (d - s2.created); result += (s2.end - s2.begin + 1) * (uint64)s2.height; result -= (s2.end - s2.begin + 1) * (uint64)b; } cout << result << endl; // Aktualizacja pomocniczej struktury danych if (cut == s.begin) { s.end = n-1; s.height = b; s.created = d; numberOfSequences = cutSequence + 1; } else { s.end = cut-1; sequence& s2 = sequences[cutSequence + 1]; s2.begin = cut; s2.end = n-1; s2.height = b; s2.created = d; numberOfSequences = cutSequence + 2; } } delete[] growth; return 0; } |