#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using lld = long double;
// #ifndef ONLINE_JUDGE
// #include "D:\cpp_codes\debugalgo.h"
// #else
// #define debug(x)
// #endif
const ll inf = 1e18;
void solve()
{
int n,q;
cin>>n>>q;
vector<ll> a(n);
for(int i=0; i<n; i++)
{
cin>>a[i];
}
vector<ll> b(q);
for(int i=0; i<q; i++)
{
cin>>b[i];
if(i) b[i]+=b[i-1];
}
// debug(b);
vector<ll> mx(q,-inf),mn(q,inf);
mx[0] = max(0LL,b[0]);
mn[0] = min(0LL,b[0]);
for(int i=1; i<q; i++)
{
mx[i] = max({0LL,mx[i-1],b[i]});
mn[i] = min({0LL,mn[i-1],b[i]});
}
// debug(mn);
// debug(mx);
vector<pair<ll,pair<ll,ll>>> vright,vleft;
vright.push_back({mx[0],{mx[0],0LL}});
for(int i=1; i<q; i++)
{
vright.push_back({mx[i]-mn[i-1],{mx[i],mn[i-1]}});
}
// debug(vright);
vleft.push_back({-mn[0],{-mn[0],0LL}});
for(int i=1; i<q; i++)
{
vleft.push_back({mx[i-1]-mn[i],{-mn[i],mx[i-1]}});
}
// debug(vleft);
vector<ll> ans(n);
for(int i=0; i<n; i++)
{
ll left=-inf,right=-inf;
if(i==0)
{
left = -mn.back();
}
else
{
ll delta = a[i]-a[i-1];
auto itn = upper_bound(vleft.begin(),vleft.end(),make_pair(delta,make_pair(-1LL,-1LL)));
//intersecting
if(itn!=vleft.end())
{
left = max(left,delta - (*itn).second.second);
}
// no non-intersection
if(itn!=vleft.begin())
{
itn--;
left = max(left,(*itn).second.first);
}
}
if(i==n-1)
{
right = mx.back();
}
else
{
ll delta = a[i+1]-a[i];
auto it = upper_bound(vright.begin(),vright.end(),make_pair(delta,make_pair(-1LL,-1LL)));
//intersecting
if(it!=vright.end())
{
right = max(right, delta + (*it).second.second);
}
//non-intersecting
if(it!=vright.begin())
{
it--;
right = max(right, (*it).second.first);
}
}
ans[i] = left + right;
cout<<ans[i]<<"\n";
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
// #ifndef ONLINE_JUDGE
// freopen("error.txt", "w", stderr);
// #endif
int tt = 1;
// cin >> tt;
while (tt--)
{
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
600 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
600 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 |
600 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
516 KB |
Output is correct |
16 |
Correct |
1 ms |
608 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
600 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
600 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 |
600 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
516 KB |
Output is correct |
16 |
Correct |
1 ms |
608 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
30 ms |
22468 KB |
Output is correct |
21 |
Correct |
28 ms |
23236 KB |
Output is correct |
22 |
Correct |
29 ms |
22724 KB |
Output is correct |
23 |
Correct |
28 ms |
22460 KB |
Output is correct |
24 |
Correct |
36 ms |
22460 KB |
Output is correct |
25 |
Correct |
124 ms |
26260 KB |
Output is correct |
26 |
Correct |
124 ms |
26300 KB |
Output is correct |
27 |
Correct |
121 ms |
25440 KB |
Output is correct |
28 |
Correct |
125 ms |
25580 KB |
Output is correct |
29 |
Correct |
121 ms |
25204 KB |
Output is correct |
30 |
Correct |
111 ms |
25276 KB |
Output is correct |
31 |
Correct |
64 ms |
24764 KB |
Output is correct |
32 |
Correct |
60 ms |
26056 KB |
Output is correct |
33 |
Correct |
13 ms |
3028 KB |
Output is correct |
34 |
Correct |
123 ms |
25028 KB |
Output is correct |
35 |
Correct |
124 ms |
25148 KB |
Output is correct |
36 |
Correct |
125 ms |
25532 KB |
Output is correct |
37 |
Correct |
126 ms |
26380 KB |
Output is correct |
38 |
Correct |
119 ms |
25804 KB |
Output is correct |
39 |
Correct |
110 ms |
26556 KB |
Output is correct |
40 |
Correct |
70 ms |
25788 KB |
Output is correct |
41 |
Correct |
31 ms |
23744 KB |
Output is correct |
42 |
Correct |
63 ms |
26056 KB |
Output is correct |
43 |
Correct |
68 ms |
27732 KB |
Output is correct |
44 |
Correct |
31 ms |
23228 KB |
Output is correct |
45 |
Correct |
63 ms |
25292 KB |
Output is correct |
46 |
Correct |
67 ms |
27084 KB |
Output is correct |
47 |
Correct |
69 ms |
28612 KB |
Output is correct |
48 |
Correct |
73 ms |
27580 KB |
Output is correct |