#include <algorithm> #include <cstdio> #include <cstdlib> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <vector> #include <cmath> #include <cstring> #include <string> #include <iostream> #include <complex> #include <sstream> #include <cassert> using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef long double LD; typedef vector<int> VI; typedef pair<int,int> PII; #define REP(i,n) for(int i=0;i<(n);++i) #define SIZE(c) ((int)((c).size())) #define FOR(i,a,b) for (int i=(a); i<(b); ++i) #define FOREACH(i,x) for (__typeof((x).begin()) i=(x).begin(); i!=(x).end(); ++i) #define FORD(i,a,b) for (int i=(a)-1; i>=(b); --i) #define ALL(v) (v).begin(), (v).end() #define pb push_back #define mp make_pair #define st first #define nd second int N, M; LL D[1000000]; LL W[1000000]; int L[1500000], R[1500000]; // Ile urośnie w trakcie dnia LL day[1500000]; // Tyle wynosiła suma... LL sum[1500000]; // ... w dniu LL at_time[1500000]; // A tyle równa wysokość, o ile != -1 LL h[1500000]; // Wysokosc lewego ara w dniu at_time LL lefth[1500000]; void init() { REP(i,N) L[N+i] = R[N+i] = i; FORD(i,N,0) { day[i] = day[2*i] + day[2*i+1]; L[i] = L[2*i]; R[i] = R[2*i+1]; } REP(i,2*N) h[i] = -1; } void push(int v) { if (h[v] != -1) { lefth[2*v] = lefth[2*v+1] = h[2*v] = h[2*v+1] = h[v]; at_time[2*v] = at_time[2*v+1] = at_time[v]; sum[2*v] = sum[2*v+1] = sum[v] / 2; h[v] = -1; } } LL get_(LL T, int A, int B, int v = 1) { if (A > R[v] || B < L[v]) return 0; if (A <= L[v] && R[v] <= B) return sum[v] + (T - at_time[v]) * day[v]; push(v); return get_(T, A, B, 2*v) + get_(T, A, B, 2*v+1); } LL getleft(LL T, int v) { return lefth[v] + day[N+L[v]] * (T - at_time[v]); } void set_(LL T, LL H, int A, int B, int v = 1) { if (A > R[v] || B < L[v]) return; if (A <= L[v] && R[v] <= B) { lefth[v] = h[v] = H; at_time[v] = T; sum[v] = (R[v] - L[v] + 1) * H; return; } push(v); set_(T, H, A, B, 2*v); set_(T, H, A, B, 2*v+1); at_time[v] = T; lefth[v] = getleft(T, 2*v); sum[v] = get_(T, L[2*v], R[2*v], 2*v) + get_(T, L[2*v+1], R[2*v+1], 2*v+1); } int first(LL T, LL H, int v = 1) { if (getleft(T, v) > H) return L[v]; if (v >= N) return R[v] + 1; push(v); if (getleft(T, 2*v+1) > H) return first(T, H, 2*v); else return first(T, H, 2*v+1); } int main() { scanf("%d%d",&N,&M); int K = 1; while (K < N) K *= 2; REP(i,N) scanf("%lld", &day[K+i]); sort(day+K, day+2*K); N = K; init(); REP(i,M) scanf("%lld%lld", &D[i+1], &W[i+1]); FOR(i,1,M+1) { LL T = D[i]; LL H = W[i]; int f = first(T, H); LL result = get_(T, f, N-1) - (N - f) * H; printf("%lld\n", result); set_(T, H, f, N-1); } }
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 | #include <algorithm> #include <cstdio> #include <cstdlib> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <vector> #include <cmath> #include <cstring> #include <string> #include <iostream> #include <complex> #include <sstream> #include <cassert> using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef long double LD; typedef vector<int> VI; typedef pair<int,int> PII; #define REP(i,n) for(int i=0;i<(n);++i) #define SIZE(c) ((int)((c).size())) #define FOR(i,a,b) for (int i=(a); i<(b); ++i) #define FOREACH(i,x) for (__typeof((x).begin()) i=(x).begin(); i!=(x).end(); ++i) #define FORD(i,a,b) for (int i=(a)-1; i>=(b); --i) #define ALL(v) (v).begin(), (v).end() #define pb push_back #define mp make_pair #define st first #define nd second int N, M; LL D[1000000]; LL W[1000000]; int L[1500000], R[1500000]; // Ile urośnie w trakcie dnia LL day[1500000]; // Tyle wynosiła suma... LL sum[1500000]; // ... w dniu LL at_time[1500000]; // A tyle równa wysokość, o ile != -1 LL h[1500000]; // Wysokosc lewego ara w dniu at_time LL lefth[1500000]; void init() { REP(i,N) L[N+i] = R[N+i] = i; FORD(i,N,0) { day[i] = day[2*i] + day[2*i+1]; L[i] = L[2*i]; R[i] = R[2*i+1]; } REP(i,2*N) h[i] = -1; } void push(int v) { if (h[v] != -1) { lefth[2*v] = lefth[2*v+1] = h[2*v] = h[2*v+1] = h[v]; at_time[2*v] = at_time[2*v+1] = at_time[v]; sum[2*v] = sum[2*v+1] = sum[v] / 2; h[v] = -1; } } LL get_(LL T, int A, int B, int v = 1) { if (A > R[v] || B < L[v]) return 0; if (A <= L[v] && R[v] <= B) return sum[v] + (T - at_time[v]) * day[v]; push(v); return get_(T, A, B, 2*v) + get_(T, A, B, 2*v+1); } LL getleft(LL T, int v) { return lefth[v] + day[N+L[v]] * (T - at_time[v]); } void set_(LL T, LL H, int A, int B, int v = 1) { if (A > R[v] || B < L[v]) return; if (A <= L[v] && R[v] <= B) { lefth[v] = h[v] = H; at_time[v] = T; sum[v] = (R[v] - L[v] + 1) * H; return; } push(v); set_(T, H, A, B, 2*v); set_(T, H, A, B, 2*v+1); at_time[v] = T; lefth[v] = getleft(T, 2*v); sum[v] = get_(T, L[2*v], R[2*v], 2*v) + get_(T, L[2*v+1], R[2*v+1], 2*v+1); } int first(LL T, LL H, int v = 1) { if (getleft(T, v) > H) return L[v]; if (v >= N) return R[v] + 1; push(v); if (getleft(T, 2*v+1) > H) return first(T, H, 2*v); else return first(T, H, 2*v+1); } int main() { scanf("%d%d",&N,&M); int K = 1; while (K < N) K *= 2; REP(i,N) scanf("%lld", &day[K+i]); sort(day+K, day+2*K); N = K; init(); REP(i,M) scanf("%lld%lld", &D[i+1], &W[i+1]); FOR(i,1,M+1) { LL T = D[i]; LL H = W[i]; int f = first(T, H); LL result = get_(T, f, N-1) - (N - f) * H; printf("%lld\n", result); set_(T, H, f, N-1); } } |