#include<bits/stdc++.h>
#define rep(i,k,n) for(ll i= (ll) k;i< (ll) n;i++)
#define all(v) (v).begin(), (v).end()
#define SZ(v) (int)((v).size())
#define pb push_back
#define ft first
#define sd second
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const long long INF = 1e18L + 1;
const int IINF = 1e9 + 1;
using namespace std;
template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<'='<<h<<endl; }
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
while(*sdbg!=',')cerr<<*sdbg++;cerr<<'='<<h<<','; _dbg(sdbg+1, a...);
}
#undef LOCAL
#ifdef LOCAL
#define DBG(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define DBG(...) (__VA_ARGS__)
#define cerr if(0)cout
#endif
const ll N = 1e6 + 2;
struct seq {
ll pocz, dl, alive, next;
};
int n, m;
vector<seq> v;
vector<vector<pair<int, int>>> crash(N);
vector<ll> wyn(N);
ll when(ll p1, ll dl1, ll p2) {
ll dist = p2 - p1;
ll t = min(dist / dl1 + 1, N - 1);
return t;
}
int main() {
#ifndef LOCAL
ios_base::sync_with_stdio(0);
cin.tie(0);
#endif
cin >> n >> m;
v.pb({0, 1, 1, 1});
rep (i, 0, n) {
ll ti;
cin >> ti;
v.pb({ti, 1, 1, i + 2});
}
v.pb({INF, 1, 0, -1});
rep (i, 0, n) {
crash[when(v[i].pocz, v[i].dl, v[i + 1].pocz)].pb({i, i + 1});
}
ll x = 0, dx = 0;
rep (d, 1, N) {
x += dx;
queue<pair<int, int>> q;
for (auto pp: crash[d]) {
q.push(pp);
}
while (!q.empty()) {
auto pp = q.front();
q.pop();
ll i1 = pp.ft, i2 = pp.sd;
if (!v[i1].alive or !v[i2].alive) {
continue;
}
ll dist = v[i2].pocz - v[i1].pocz;
x += (d * v[i1].dl - dist) * v[i2].dl;
dx += v[i1].dl * v[i2].dl;
seq news = {v[i1].pocz, v[i1].dl + v[i2].dl, 1, v[i2].next};
v[i1] = news;
v[i2].alive = 0;
ll it = when(v[i1].pocz, v[i1].dl, v[v[i1].next].pocz);
if (it == d) {
q.push({i1, v[i1].next});
} else {
crash[it].pb({i1, v[i1].next});
}
}
wyn[d] = x;
}
rep (i, 0, m) {
ll di;
cin >> di;
cout << wyn[di] << "\n";
}
}
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 | #include<bits/stdc++.h> #define rep(i,k,n) for(ll i= (ll) k;i< (ll) n;i++) #define all(v) (v).begin(), (v).end() #define SZ(v) (int)((v).size()) #define pb push_back #define ft first #define sd second typedef long long ll; typedef unsigned long long ull; typedef long double ld; const long long INF = 1e18L + 1; const int IINF = 1e9 + 1; using namespace std; template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<'='<<h<<endl; } template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while(*sdbg!=',')cerr<<*sdbg++;cerr<<'='<<h<<','; _dbg(sdbg+1, a...); } #undef LOCAL #ifdef LOCAL #define DBG(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #else #define DBG(...) (__VA_ARGS__) #define cerr if(0)cout #endif const ll N = 1e6 + 2; struct seq { ll pocz, dl, alive, next; }; int n, m; vector<seq> v; vector<vector<pair<int, int>>> crash(N); vector<ll> wyn(N); ll when(ll p1, ll dl1, ll p2) { ll dist = p2 - p1; ll t = min(dist / dl1 + 1, N - 1); return t; } int main() { #ifndef LOCAL ios_base::sync_with_stdio(0); cin.tie(0); #endif cin >> n >> m; v.pb({0, 1, 1, 1}); rep (i, 0, n) { ll ti; cin >> ti; v.pb({ti, 1, 1, i + 2}); } v.pb({INF, 1, 0, -1}); rep (i, 0, n) { crash[when(v[i].pocz, v[i].dl, v[i + 1].pocz)].pb({i, i + 1}); } ll x = 0, dx = 0; rep (d, 1, N) { x += dx; queue<pair<int, int>> q; for (auto pp: crash[d]) { q.push(pp); } while (!q.empty()) { auto pp = q.front(); q.pop(); ll i1 = pp.ft, i2 = pp.sd; if (!v[i1].alive or !v[i2].alive) { continue; } ll dist = v[i2].pocz - v[i1].pocz; x += (d * v[i1].dl - dist) * v[i2].dl; dx += v[i1].dl * v[i2].dl; seq news = {v[i1].pocz, v[i1].dl + v[i2].dl, 1, v[i2].next}; v[i1] = news; v[i2].alive = 0; ll it = when(v[i1].pocz, v[i1].dl, v[v[i1].next].pocz); if (it == d) { q.push({i1, v[i1].next}); } else { crash[it].pb({i1, v[i1].next}); } } wyn[d] = x; } rep (i, 0, m) { ll di; cin >> di; cout << wyn[di] << "\n"; } } |
English