#include <iostream> #include <cstdio> #include <cmath> #include <vector> #include <algorithm> using namespace std; int main() { int n, m; scanf("%d%d", &n, &m); vector<pair<int, int> > v; long long int times[200001]; long long int sumTimes = 0; for (int i = 0; i < n; i++) { scanf("%lld", ×[i]); sumTimes += times[i]; if (times[i] / (i + 1) > 1000001) v.push_back(make_pair(1000001, i)); else v.push_back(make_pair(times[i] / (i + 1), i)); } sort(v.begin(), v.end()); for (int i = 0; i < m; i++) { int x; scanf("%d", &x); std::vector<pair<int, int> >::iterator low; low = lower_bound(v.begin(), v.end(), make_pair(x, -1)); int indx = low - v.begin(); vector<int> indexes; long long int result = 0; if (indx < v.size()) { for (int j = indx; j < v.size(); j++) indexes.push_back(v[j].second); if (indexes.size() > 0) sort(indexes.begin(), indexes.end()); long long int maxSoFar = 0; for (int j = 0; j < indexes.size() - 1; j++) { if (times[indexes[j]] - (indexes[j] + 1) * x > maxSoFar) maxSoFar = times[indexes[j]] - (indexes[j] + 1) * x; result += maxSoFar * (indexes[j + 1] - indexes[j]); } if (indexes.size() > 0) { if (times[indexes[indexes.size() - 1]] - (indexes[indexes.size() - 1] + 1) * x > maxSoFar) maxSoFar = times[indexes[indexes.size() - 1]] - (indexes[indexes.size() - 1] + 1) * x; result += maxSoFar * (n - indexes[indexes.size() - 1]); } } long long int nLong = n; long long int xLong = x; result += nLong*(nLong + 1) / 2 * xLong; result -= sumTimes; printf("%lld\n", result); } }
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 | #include <iostream> #include <cstdio> #include <cmath> #include <vector> #include <algorithm> using namespace std; int main() { int n, m; scanf("%d%d", &n, &m); vector<pair<int, int> > v; long long int times[200001]; long long int sumTimes = 0; for (int i = 0; i < n; i++) { scanf("%lld", ×[i]); sumTimes += times[i]; if (times[i] / (i + 1) > 1000001) v.push_back(make_pair(1000001, i)); else v.push_back(make_pair(times[i] / (i + 1), i)); } sort(v.begin(), v.end()); for (int i = 0; i < m; i++) { int x; scanf("%d", &x); std::vector<pair<int, int> >::iterator low; low = lower_bound(v.begin(), v.end(), make_pair(x, -1)); int indx = low - v.begin(); vector<int> indexes; long long int result = 0; if (indx < v.size()) { for (int j = indx; j < v.size(); j++) indexes.push_back(v[j].second); if (indexes.size() > 0) sort(indexes.begin(), indexes.end()); long long int maxSoFar = 0; for (int j = 0; j < indexes.size() - 1; j++) { if (times[indexes[j]] - (indexes[j] + 1) * x > maxSoFar) maxSoFar = times[indexes[j]] - (indexes[j] + 1) * x; result += maxSoFar * (indexes[j + 1] - indexes[j]); } if (indexes.size() > 0) { if (times[indexes[indexes.size() - 1]] - (indexes[indexes.size() - 1] + 1) * x > maxSoFar) maxSoFar = times[indexes[indexes.size() - 1]] - (indexes[indexes.size() - 1] + 1) * x; result += maxSoFar * (n - indexes[indexes.size() - 1]); } } long long int nLong = n; long long int xLong = x; result += nLong*(nLong + 1) / 2 * xLong; result -= sumTimes; printf("%lld\n", result); } } |