#include <cstdio> #include <algorithm> static const int MAX_N = 500009; int N, M; int velocity[MAX_N]; int prefix_sum[MAX_N]; long long current_height[MAX_N]; long long previous_time = 0; struct range { int right; int height; int time; }; range ranges[MAX_N]; int ranges_size; long long volume(long long time, long long height) { long long delta = time - previous_time; previous_time = time; long long result = 0; for (int i = 0; i < N; ++i) { current_height[i] += velocity[i] * delta; if (current_height[i] > height) { result += current_height[i] - height; current_height[i] = height; } } return result; } int main() { scanf("%d%d", &N, &M); int pref = 0; for (int i = 0; i < N; ++i) { scanf("%d", velocity + i); pref += velocity[i]; prefix_sum[i] = pref; } std::sort(velocity, velocity + N); ranges_size = 1; ranges[0].right = N; ranges[0].height = 0; ranges[0].time = 0; while (M--) { long long time; long long height; scanf("%lld%lld", &time, &height); printf("%lld\n", volume(time, height)); } 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 | #include <cstdio> #include <algorithm> static const int MAX_N = 500009; int N, M; int velocity[MAX_N]; int prefix_sum[MAX_N]; long long current_height[MAX_N]; long long previous_time = 0; struct range { int right; int height; int time; }; range ranges[MAX_N]; int ranges_size; long long volume(long long time, long long height) { long long delta = time - previous_time; previous_time = time; long long result = 0; for (int i = 0; i < N; ++i) { current_height[i] += velocity[i] * delta; if (current_height[i] > height) { result += current_height[i] - height; current_height[i] = height; } } return result; } int main() { scanf("%d%d", &N, &M); int pref = 0; for (int i = 0; i < N; ++i) { scanf("%d", velocity + i); pref += velocity[i]; prefix_sum[i] = pref; } std::sort(velocity, velocity + N); ranges_size = 1; ranges[0].right = N; ranges[0].height = 0; ranges[0].time = 0; while (M--) { long long time; long long height; scanf("%lld%lld", &time, &height); printf("%lld\n", volume(time, height)); } return 0; } |