#include <iostream>
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll maxn = 2*1e5+5, INF = 4e18+9;
void solve(){
ll n, Q;
cin >> n >> Q;
vector<ll> a(n+2), q(Q+1);
a[0] = -1e18;
a[n+1] = 1e18;
for(int i = 1; i <= n; i++){
cin >> a[i];
}
for(int i = 1; i <= Q; i++){
cin >> q[i];
}
vector<pair<ll, ll>> b(n+1);
for(int i = 1; i <= n; i++){
b[i] = {a[i]-a[i-1], a[i+1]-a[i]};
}
vector<ll> mn(Q+1, 0), mx(Q+1, 0);
ll sum = 0;
for(int i = 1; i <= Q; i++){
sum += q[i];
mn[i] = mn[i-1];
mx[i] = mx[i-1];
if(sum > mx[i]){
mx[i] = sum;
}
if(sum < mn[i]){
mn[i] = sum;
}
}
vector<ll> f(Q+1);
for(int i = 1; i <= Q; i++){
f[i] = mx[i]-mn[i];
}
vector<ll> ans(n+1, 0);
for(int i = 1; i <= n; i++){
int l, r, mid;
ll x;
l = 1; r = Q;
x = b[i].first;
while(l <= r){
mid = (l+r)/2;
if(x <= f[mid]){
r = mid-1;
}else{
l = mid+1;
}
}
//cout << l << " " << f[l] << " " << x << " " << mn[l] << "\n";
if(l == Q+1){
ans[i] += mn[Q]*-1;
}else{
if(mn[l] != mn[l-1]){
ans[i] += mn[l]*-1-(f[l]-x);
}else{
ans[i] += mn[l]*-1;
}
}
//cout << ans[i] << "\n";
l = 1; r = Q;
x = b[i].second;
while(l <= r){
mid = (l+r)/2;
if(x <= f[mid]){
r = mid-1;
}else{
l = mid+1;
}
}
//cout << l << " " << f[l] << " " << x << " " << mx[l] << "\n";
if(l == Q+1){
ans[i] += mx[Q];
}else{
if(mx[l] != mx[l-1]){
ans[i] += mx[l]-(f[l]-x);
}else{
ans[i] += mx[l];
}
}
}
for(int i = 1; i <= n; i++){
cout << ans[i] << "\n";
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
600 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
2 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
600 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
456 KB |
Output is correct |
15 |
Correct |
1 ms |
600 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
2 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
600 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
2 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
600 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
456 KB |
Output is correct |
15 |
Correct |
1 ms |
600 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
2 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
600 KB |
Output is correct |
20 |
Correct |
32 ms |
8784 KB |
Output is correct |
21 |
Correct |
17 ms |
8540 KB |
Output is correct |
22 |
Correct |
18 ms |
8284 KB |
Output is correct |
23 |
Correct |
19 ms |
8304 KB |
Output is correct |
24 |
Correct |
24 ms |
9044 KB |
Output is correct |
25 |
Correct |
95 ms |
18244 KB |
Output is correct |
26 |
Correct |
99 ms |
18048 KB |
Output is correct |
27 |
Correct |
91 ms |
17748 KB |
Output is correct |
28 |
Correct |
94 ms |
18040 KB |
Output is correct |
29 |
Correct |
92 ms |
17428 KB |
Output is correct |
30 |
Correct |
76 ms |
16908 KB |
Output is correct |
31 |
Correct |
46 ms |
16236 KB |
Output is correct |
32 |
Correct |
50 ms |
16464 KB |
Output is correct |
33 |
Correct |
8 ms |
2140 KB |
Output is correct |
34 |
Correct |
91 ms |
18584 KB |
Output is correct |
35 |
Correct |
94 ms |
18028 KB |
Output is correct |
36 |
Correct |
95 ms |
18248 KB |
Output is correct |
37 |
Correct |
95 ms |
18004 KB |
Output is correct |
38 |
Correct |
103 ms |
17748 KB |
Output is correct |
39 |
Correct |
80 ms |
18008 KB |
Output is correct |
40 |
Correct |
52 ms |
17904 KB |
Output is correct |
41 |
Correct |
21 ms |
9304 KB |
Output is correct |
42 |
Correct |
48 ms |
16468 KB |
Output is correct |
43 |
Correct |
72 ms |
19576 KB |
Output is correct |
44 |
Correct |
19 ms |
9292 KB |
Output is correct |
45 |
Correct |
50 ms |
18004 KB |
Output is correct |
46 |
Correct |
57 ms |
19836 KB |
Output is correct |
47 |
Correct |
67 ms |
20068 KB |
Output is correct |
48 |
Correct |
59 ms |
20048 KB |
Output is correct |