#include <cstdio> #include <algorithm> #include <numeric> #include <utility> typedef unsigned long long ullong; const int MAX_N = 200009; const int MAX_M = 200009; ullong t[MAX_N]; int d[MAX_M]; int idx[MAX_M]; ullong at[MAX_N]; std::pair<int, int> sdp[MAX_M]; int sd[MAX_M]; ullong wt[MAX_M]; struct info { int oven; int client; }; info chk[MAX_N]; struct { int num; ullong sub; } wf[MAX_M]; int tree[MAX_N*4]; int tree_size; int left(int n) { return n*2+1; } int right(int n) { return n*2+2; } void init_tree(int size) { for (tree_size = 1; tree_size < size; tree_size *= 2); } int lookup(int n) { int node = 0; int rv = 0; for (int split = tree_size/2; split > 0; split /= 2) { if (n < split) { rv = std::max(rv, tree[node]); node = left(node); } else { n -= split; node = right(node); } } return std::max(rv, tree[node]); } void add(int n, int val) { int node = 0; for (int split = tree_size/2; split > 0; split /= 2) { if (n < split) { node = left(node); } else { n -= split; tree[node] = val; node = right(node); } } tree[node] = val; } int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) { scanf("%llu", &t[i]); } for (int i = 0; i < m; ++i) { scanf("%d", &d[i]); sdp[i] = std::make_pair(d[i], i); } std::adjacent_difference(t, t+n+1, t); std::partial_sum(t, t+n+1, at); std::sort(sdp, sdp+m); int um = 0; for (int i = 0; i < m; ++i) { if (sdp[i].first != sdp[i-1].first) { sd[um++] = sdp[i].first; } idx[sdp[i].second] = um-1; } init_tree(um+10); for (int i = 1; i <= n; ++i) { chk[i].client = i; int l = 0; int r = um; while (l < r) { int c = l+(r-l)/2; int last = lookup(c); if (((ullong)sd[c])*(i-last) <= at[i]-at[last]) { l = c+1; } else { r = c; } } chk[i].oven = l; if (l > 0) { add(l-1, i); } } std::sort(chk+1, chk+n+1, [](const info& a, const info& b) { if (a.oven == b.oven) return a.client > b.client; return a.oven < b.oven; }); ullong times = 0; ullong sub = 0; for (int i = 1; i <= n; ++i) { for (int j = chk[i-1].oven; j < chk[i].oven; ++j) { wt[j] = sd[j]*times-sub; } if (chk[i].oven == um) { break; } int client = chk[i].client; wf[client].sub = t[client]+wf[client-1].sub; wf[client].num = wf[client+1].num+wf[client-1].num+1; times += ((ullong)wf[client-1].num+1)*(wf[client+1].num+1); sub += wf[client].sub*(wf[client+1].num+1); if (wf[client+1].num > 0) { wf[client+wf[client+1].num].sub += wf[client].sub; } wf[client-wf[client-1].num].num = wf[client].num; wf[client+wf[client+1].num].num = wf[client].num; } for (int j = chk[n].oven; j < um; ++j) { wt[j] = sd[j]*times-sub; } for (int i = 0; i < m; ++i) { printf("%llu\n", wt[idx[i]]); } }
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 | #include <cstdio> #include <algorithm> #include <numeric> #include <utility> typedef unsigned long long ullong; const int MAX_N = 200009; const int MAX_M = 200009; ullong t[MAX_N]; int d[MAX_M]; int idx[MAX_M]; ullong at[MAX_N]; std::pair<int, int> sdp[MAX_M]; int sd[MAX_M]; ullong wt[MAX_M]; struct info { int oven; int client; }; info chk[MAX_N]; struct { int num; ullong sub; } wf[MAX_M]; int tree[MAX_N*4]; int tree_size; int left(int n) { return n*2+1; } int right(int n) { return n*2+2; } void init_tree(int size) { for (tree_size = 1; tree_size < size; tree_size *= 2); } int lookup(int n) { int node = 0; int rv = 0; for (int split = tree_size/2; split > 0; split /= 2) { if (n < split) { rv = std::max(rv, tree[node]); node = left(node); } else { n -= split; node = right(node); } } return std::max(rv, tree[node]); } void add(int n, int val) { int node = 0; for (int split = tree_size/2; split > 0; split /= 2) { if (n < split) { node = left(node); } else { n -= split; tree[node] = val; node = right(node); } } tree[node] = val; } int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) { scanf("%llu", &t[i]); } for (int i = 0; i < m; ++i) { scanf("%d", &d[i]); sdp[i] = std::make_pair(d[i], i); } std::adjacent_difference(t, t+n+1, t); std::partial_sum(t, t+n+1, at); std::sort(sdp, sdp+m); int um = 0; for (int i = 0; i < m; ++i) { if (sdp[i].first != sdp[i-1].first) { sd[um++] = sdp[i].first; } idx[sdp[i].second] = um-1; } init_tree(um+10); for (int i = 1; i <= n; ++i) { chk[i].client = i; int l = 0; int r = um; while (l < r) { int c = l+(r-l)/2; int last = lookup(c); if (((ullong)sd[c])*(i-last) <= at[i]-at[last]) { l = c+1; } else { r = c; } } chk[i].oven = l; if (l > 0) { add(l-1, i); } } std::sort(chk+1, chk+n+1, [](const info& a, const info& b) { if (a.oven == b.oven) return a.client > b.client; return a.oven < b.oven; }); ullong times = 0; ullong sub = 0; for (int i = 1; i <= n; ++i) { for (int j = chk[i-1].oven; j < chk[i].oven; ++j) { wt[j] = sd[j]*times-sub; } if (chk[i].oven == um) { break; } int client = chk[i].client; wf[client].sub = t[client]+wf[client-1].sub; wf[client].num = wf[client+1].num+wf[client-1].num+1; times += ((ullong)wf[client-1].num+1)*(wf[client+1].num+1); sub += wf[client].sub*(wf[client+1].num+1); if (wf[client+1].num > 0) { wf[client+wf[client+1].num].sub += wf[client].sub; } wf[client-wf[client-1].num].num = wf[client].num; wf[client+wf[client+1].num].num = wf[client].num; } for (int j = chk[n].oven; j < um; ++j) { wt[j] = sd[j]*times-sub; } for (int i = 0; i < m; ++i) { printf("%llu\n", wt[idx[i]]); } } |