#include <algorithm> #include <cstdio> #include <vector> using namespace std; typedef long long int LL; const LL MAX_TIME = 1000000000007LL; const int MAX_T = 524288; const int MAX_N = 200007; struct Node { LL value; int div; int lastOven; } node[MAX_N]; pair<int, int> interval[MAX_N]; pair<LL, int> tree[MAX_T], oven[MAX_N]; LL t[MAX_N], result[MAX_N]; LL sum, decrease; int n, m, M = 1, lastUpdate, minusAmount; void readInput() { scanf("%d %d", &n, &m); for (int i = 0; i < n; ++i) { scanf("%lld", &t[i]); node[i].div = 1; interval[i].first = interval[i].second = -1; } node[0].value = t[0]; for (int i = 0; i < n; ++i) { node[i].value = t[i] - t[i - 1]; } for (int i = 0; i < m; ++i) { scanf("%lld", &oven[i].first); oven[i].second = i; } sort(oven, oven + m); } void initTree() { while (M < n) { M *= 2; } for (int i = M; i < M + n; ++i) { tree[i].first = node[i - M].value + 1; tree[i].second = i - M; } for (int i = M + n; i < 2 * M; ++i) { tree[i] = make_pair(MAX_TIME, 0); } for (int i = M - 1; i > 0; --i) { tree[i] = min(tree[2 * i], tree[2 * i + 1]); } } void updateTree(int node) { node /= 2; while (node > 0) { tree[node] = min(tree[2 * node], tree[2 * node + 1]); node /= 2; } } void updateState() { ++minusAmount; pair<LL, int> root = tree[1]; tree[M + root.second].first = MAX_TIME; updateTree(M + root.second); int mid = root.second; node[mid].value -= (root.first - node[mid].lastOven) * node[mid].div; sum += abs(node[mid].value); if (mid != 0 && interval[mid - 1].first != -1) { int left = interval[mid - 1].first; ++interval[left].second; interval[mid].first = left; decrease += (mid - left + 1); } else { interval[mid].first = interval[mid].second = mid; ++decrease; } if (mid != n - 1 && interval[mid + 1].second != -1) { int left = interval[mid].first; int right = interval[mid + 1].second; interval[left].second = right; interval[right].first = left; if (right != n - 1) { node[right + 1].value -= (root.first - node[right + 1].lastOven) * node[right + 1].div; node[right + 1].lastOven = root.first; node[right + 1].value += node[mid].value; node[right + 1].div = right - interval[right].first + 2; if (node[right + 1].value < 0) { tree[M + right + 1].first = root.first; } else { tree[M + right + 1].first = root.first + (node[right + 1].value / node[right + 1].div) + 1; } updateTree(M + right + 1); } decrease += (LL)(mid - left + 1) * (LL)(right - mid); sum += (right - mid) * abs(node[mid].value); } else if (mid != n - 1) { node[mid + 1].value -= (root.first - node[mid + 1].lastOven) * node[mid + 1].div; node[mid + 1].lastOven = root.first; node[mid + 1].value += node[mid].value; node[mid + 1].div = mid - interval[mid].first + 2; if (node[mid + 1].value < 0) { tree[M + mid + 1].first = root.first; } else { tree[M + mid + 1].first = root.first + (node[mid + 1].value / node[mid + 1].div) + 1; } updateTree(M + mid + 1); } } void solve() { for (int i = 0; i < m; ++i) { LL o = oven[i].first; while (tree[1].first <= oven[i].first) { if (tree[1].first != lastUpdate) { sum += (tree[1].first - lastUpdate) * decrease; lastUpdate = tree[1].first; } updateState(); } if (i != 0 && oven[i].first == oven[i - 1].first) { result[oven[i].second] = result[oven[i - 1].second]; } else { sum += (oven[i].first - lastUpdate) * decrease; lastUpdate = oven[i].first; result[oven[i].second] = sum; } } } void printResult() { for (int i = 0; i < m; ++i) { printf("%lld\n", result[i]); } } int main() { readInput(); initTree(); solve(); printResult(); 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 | #include <algorithm> #include <cstdio> #include <vector> using namespace std; typedef long long int LL; const LL MAX_TIME = 1000000000007LL; const int MAX_T = 524288; const int MAX_N = 200007; struct Node { LL value; int div; int lastOven; } node[MAX_N]; pair<int, int> interval[MAX_N]; pair<LL, int> tree[MAX_T], oven[MAX_N]; LL t[MAX_N], result[MAX_N]; LL sum, decrease; int n, m, M = 1, lastUpdate, minusAmount; void readInput() { scanf("%d %d", &n, &m); for (int i = 0; i < n; ++i) { scanf("%lld", &t[i]); node[i].div = 1; interval[i].first = interval[i].second = -1; } node[0].value = t[0]; for (int i = 0; i < n; ++i) { node[i].value = t[i] - t[i - 1]; } for (int i = 0; i < m; ++i) { scanf("%lld", &oven[i].first); oven[i].second = i; } sort(oven, oven + m); } void initTree() { while (M < n) { M *= 2; } for (int i = M; i < M + n; ++i) { tree[i].first = node[i - M].value + 1; tree[i].second = i - M; } for (int i = M + n; i < 2 * M; ++i) { tree[i] = make_pair(MAX_TIME, 0); } for (int i = M - 1; i > 0; --i) { tree[i] = min(tree[2 * i], tree[2 * i + 1]); } } void updateTree(int node) { node /= 2; while (node > 0) { tree[node] = min(tree[2 * node], tree[2 * node + 1]); node /= 2; } } void updateState() { ++minusAmount; pair<LL, int> root = tree[1]; tree[M + root.second].first = MAX_TIME; updateTree(M + root.second); int mid = root.second; node[mid].value -= (root.first - node[mid].lastOven) * node[mid].div; sum += abs(node[mid].value); if (mid != 0 && interval[mid - 1].first != -1) { int left = interval[mid - 1].first; ++interval[left].second; interval[mid].first = left; decrease += (mid - left + 1); } else { interval[mid].first = interval[mid].second = mid; ++decrease; } if (mid != n - 1 && interval[mid + 1].second != -1) { int left = interval[mid].first; int right = interval[mid + 1].second; interval[left].second = right; interval[right].first = left; if (right != n - 1) { node[right + 1].value -= (root.first - node[right + 1].lastOven) * node[right + 1].div; node[right + 1].lastOven = root.first; node[right + 1].value += node[mid].value; node[right + 1].div = right - interval[right].first + 2; if (node[right + 1].value < 0) { tree[M + right + 1].first = root.first; } else { tree[M + right + 1].first = root.first + (node[right + 1].value / node[right + 1].div) + 1; } updateTree(M + right + 1); } decrease += (LL)(mid - left + 1) * (LL)(right - mid); sum += (right - mid) * abs(node[mid].value); } else if (mid != n - 1) { node[mid + 1].value -= (root.first - node[mid + 1].lastOven) * node[mid + 1].div; node[mid + 1].lastOven = root.first; node[mid + 1].value += node[mid].value; node[mid + 1].div = mid - interval[mid].first + 2; if (node[mid + 1].value < 0) { tree[M + mid + 1].first = root.first; } else { tree[M + mid + 1].first = root.first + (node[mid + 1].value / node[mid + 1].div) + 1; } updateTree(M + mid + 1); } } void solve() { for (int i = 0; i < m; ++i) { LL o = oven[i].first; while (tree[1].first <= oven[i].first) { if (tree[1].first != lastUpdate) { sum += (tree[1].first - lastUpdate) * decrease; lastUpdate = tree[1].first; } updateState(); } if (i != 0 && oven[i].first == oven[i - 1].first) { result[oven[i].second] = result[oven[i - 1].second]; } else { sum += (oven[i].first - lastUpdate) * decrease; lastUpdate = oven[i].first; result[oven[i].second] = sum; } } } void printResult() { for (int i = 0; i < m; ++i) { printf("%lld\n", result[i]); } } int main() { readInput(); initTree(); solve(); printResult(); return 0; } |