#include<bits/stdc++.h> using namespace std; # ifdef DEB # define debug(...) fprintf(stderr, __VA_ARGS__) # else # define debug(...) #endif #define F first #define S second #define MP make_pair #define PB push_back #define LL long long #define LD long double #define PII pair<int, int> #define PLL pair<LL, LL> #define pb pop_back #define VI vector<int> #define VPI vector<PII> #define FOR(i,a,b) for(int i = (a); i <= (b); i++) #define FORD(i,a,b) for(int i = (a); i >= (b); i--) #define RE(i,n) FOR(i,1,n) #define REP(i,n) FOR(i,0,(int)(n)-1) #define ALL(x) (x).begin(), (x).end() #define siz(V) ((int)V.size()) const int M = 1024 * 256; int n, m, D[2 * M]; LL diff[M], T[M], R[M], d, sub; vector<PII> Q; bool done[M]; set<pair<LL, int>> secik; void add(int v, int w) { v += M; D[v] = w; while (v > 1) { v /= 2; D[v] = D[2 * v] + D[2 * v + 1]; } } int getL(int v) { v += M; while (D[v] == 0) { if (v % 2 == 0) { v--; } v /= 2; } while (v < M) { if (D[2 * v + 1] > 0) { v = 2 * v + 1; } else { v = 2 * v; } } return v - M; } int getR(int v) { v += M; while (D[v] == 0) { if (v % 2 == 1) { v++; } v /= 2; } while (v < M) { if (D[2 * v] > 0) { v = 2 * v; } else { v = 2 * v + 1; } } return v - M; } void change(int ind, LL sum, LL len) { LL dd = (sum + len - 1) / len; secik.insert(MP(dd, ind)); } void process(int ind) { add(ind, 0); int lef = getL(ind - 1); int rig = getR(ind + 1); LL subP = T[ind] - T[lef]; LL plusD = (LL)(ind - lef) * (rig - ind); d += plusD; sub += subP * (rig - ind); if (rig != n + 1) { change(rig, T[rig] - T[lef], rig - lef); } } int main() { //ios_base::sync_with_stdio(0); scanf("%d%d", &n, &m); for (int i = 0; i <= n + 1; i++) { D[i + M] = 1; } for (int i = M - 1; i > 0; i--) { D[i] = D[2 * i] + D[2 * i + 1]; } LL prev = 0; for (int i = 1; i <= n; i++) { scanf("%lld", T + i); diff[i] = T[i] - prev; prev = T[i]; secik.insert(MP(diff[i], i)); } for (int i = 1; i <= m; i++) { int a; scanf("%d", &a); Q.PB(MP(a, i)); } sort(ALL(Q)); for (auto q : Q) { while (!secik.empty() && secik.begin()->F <= (LL)q.F) { int ind = secik.begin()->S; secik.erase(secik.begin()); if (!done[ind]) { done[ind] = true; process(ind); } } R[q.S] = d * q.F - sub; } for (int i = 1; i <= m; i++) { printf("%lld\n", R[i]); } 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 150 151 152 153 154 155 156 157 158 159 160 161 | #include<bits/stdc++.h> using namespace std; # ifdef DEB # define debug(...) fprintf(stderr, __VA_ARGS__) # else # define debug(...) #endif #define F first #define S second #define MP make_pair #define PB push_back #define LL long long #define LD long double #define PII pair<int, int> #define PLL pair<LL, LL> #define pb pop_back #define VI vector<int> #define VPI vector<PII> #define FOR(i,a,b) for(int i = (a); i <= (b); i++) #define FORD(i,a,b) for(int i = (a); i >= (b); i--) #define RE(i,n) FOR(i,1,n) #define REP(i,n) FOR(i,0,(int)(n)-1) #define ALL(x) (x).begin(), (x).end() #define siz(V) ((int)V.size()) const int M = 1024 * 256; int n, m, D[2 * M]; LL diff[M], T[M], R[M], d, sub; vector<PII> Q; bool done[M]; set<pair<LL, int>> secik; void add(int v, int w) { v += M; D[v] = w; while (v > 1) { v /= 2; D[v] = D[2 * v] + D[2 * v + 1]; } } int getL(int v) { v += M; while (D[v] == 0) { if (v % 2 == 0) { v--; } v /= 2; } while (v < M) { if (D[2 * v + 1] > 0) { v = 2 * v + 1; } else { v = 2 * v; } } return v - M; } int getR(int v) { v += M; while (D[v] == 0) { if (v % 2 == 1) { v++; } v /= 2; } while (v < M) { if (D[2 * v] > 0) { v = 2 * v; } else { v = 2 * v + 1; } } return v - M; } void change(int ind, LL sum, LL len) { LL dd = (sum + len - 1) / len; secik.insert(MP(dd, ind)); } void process(int ind) { add(ind, 0); int lef = getL(ind - 1); int rig = getR(ind + 1); LL subP = T[ind] - T[lef]; LL plusD = (LL)(ind - lef) * (rig - ind); d += plusD; sub += subP * (rig - ind); if (rig != n + 1) { change(rig, T[rig] - T[lef], rig - lef); } } int main() { //ios_base::sync_with_stdio(0); scanf("%d%d", &n, &m); for (int i = 0; i <= n + 1; i++) { D[i + M] = 1; } for (int i = M - 1; i > 0; i--) { D[i] = D[2 * i] + D[2 * i + 1]; } LL prev = 0; for (int i = 1; i <= n; i++) { scanf("%lld", T + i); diff[i] = T[i] - prev; prev = T[i]; secik.insert(MP(diff[i], i)); } for (int i = 1; i <= m; i++) { int a; scanf("%d", &a); Q.PB(MP(a, i)); } sort(ALL(Q)); for (auto q : Q) { while (!secik.empty() && secik.begin()->F <= (LL)q.F) { int ind = secik.begin()->S; secik.erase(secik.begin()); if (!done[ind]) { done[ind] = true; process(ind); } } R[q.S] = d * q.F - sub; } for (int i = 1; i <= m; i++) { printf("%lld\n", R[i]); } return 0; } |