#include<bits/stdc++.h>
#define int int64_t
#define double long double
using namespace std;
const double eps=1e-6;
int32_t main()
{
int n,q; cin >> n >> q;
vector<int> pos(n),d(n,1e18);
for(int i=0;i<n;++i) cin >> pos[i];
for(int i=0;i<n-1;++i) d[i]=abs(pos[i+1]-pos[i]);
vector<int> Q;
int tmp,tmp_now=0,POS_MAX=0,NEG_MIN=0;
for(int i=0;i<q;++i){
cin >> tmp;
tmp_now+=tmp;
if(tmp_now>=0 && tmp_now>POS_MAX){
if(!Q.empty() && Q.back()>=0 && tmp_now>Q.back()) Q.pop_back();
Q.emplace_back(tmp_now);
}
if(tmp_now<0 && tmp_now<NEG_MIN){
if(!Q.empty() && Q.back()<=0 && tmp_now<Q.back()) Q.pop_back();
Q.emplace_back(tmp_now);
}
POS_MAX=max(POS_MAX,tmp_now);
NEG_MIN=min(NEG_MIN,tmp_now);
}
map<int,pair<int,int>> MAP;
MAP[0]={0,0};
int POS=0, NEG=0,TOTAL;
for(int i=0;i<Q.size();++i){
if(Q[i]>0)
POS=Q[i];
else
NEG=-Q[i];
TOTAL=NEG+POS;
MAP[TOTAL]={POS,NEG};
}
MAP[1e18]={1e18,1e18};
int pre=0,ans;
for(int i=0;i<n;++i){
if(i==0)
pre=-NEG_MIN;
if(i==n-1){
pre+=POS_MAX;
cout << pre << '\n';
break;
}
pair<int,int> now_pair=MAP.upper_bound(d[i])->second,pre_pair=prev(MAP.upper_bound(d[i]))->second;
if(now_pair.first==pre_pair.first){
ans=pre+pre_pair.first;
pre=d[i]-pre_pair.first;
}
else if(now_pair.second==pre_pair.second){
ans=pre+d[i]-pre_pair.second;
pre=pre_pair.second;
}
else{
ans=pre+pre_pair.first;
pre=pre_pair.second;
}
cout << ans << '\n';
}
return 0;
}
/*
4 3
-2 3 5 8
2
-4
7
10 10
-56 -43 -39 -31 -22 -5 0 12 18 22
-3
0
5
-4
-2
10
-13
-1
9
6
*/
Compilation message
Main.cpp: In function 'int32_t main()':
Main.cpp:37:18: warning: comparison of integer expressions of different signedness: 'int64_t' {aka 'long int'} and 'std::vector<long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
37 | for(int i=0;i<Q.size();++i){
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
3 ms |
332 KB |
Output is correct |
5 |
Correct |
3 ms |
332 KB |
Output is correct |
6 |
Correct |
3 ms |
332 KB |
Output is correct |
7 |
Correct |
3 ms |
332 KB |
Output is correct |
8 |
Correct |
3 ms |
432 KB |
Output is correct |
9 |
Correct |
3 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
2 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
3 ms |
332 KB |
Output is correct |
16 |
Correct |
3 ms |
460 KB |
Output is correct |
17 |
Correct |
3 ms |
332 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
2 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
3 ms |
332 KB |
Output is correct |
5 |
Correct |
3 ms |
332 KB |
Output is correct |
6 |
Correct |
3 ms |
332 KB |
Output is correct |
7 |
Correct |
3 ms |
332 KB |
Output is correct |
8 |
Correct |
3 ms |
432 KB |
Output is correct |
9 |
Correct |
3 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
2 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
3 ms |
332 KB |
Output is correct |
16 |
Correct |
3 ms |
460 KB |
Output is correct |
17 |
Correct |
3 ms |
332 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
2 ms |
332 KB |
Output is correct |
20 |
Correct |
121 ms |
7724 KB |
Output is correct |
21 |
Correct |
113 ms |
7508 KB |
Output is correct |
22 |
Correct |
106 ms |
7560 KB |
Output is correct |
23 |
Correct |
104 ms |
7604 KB |
Output is correct |
24 |
Correct |
119 ms |
7992 KB |
Output is correct |
25 |
Correct |
321 ms |
12216 KB |
Output is correct |
26 |
Correct |
336 ms |
12128 KB |
Output is correct |
27 |
Correct |
345 ms |
11444 KB |
Output is correct |
28 |
Correct |
302 ms |
10820 KB |
Output is correct |
29 |
Correct |
296 ms |
7868 KB |
Output is correct |
30 |
Correct |
191 ms |
4448 KB |
Output is correct |
31 |
Correct |
169 ms |
4056 KB |
Output is correct |
32 |
Correct |
166 ms |
4036 KB |
Output is correct |
33 |
Correct |
29 ms |
1476 KB |
Output is correct |
34 |
Correct |
233 ms |
5468 KB |
Output is correct |
35 |
Correct |
239 ms |
5732 KB |
Output is correct |
36 |
Correct |
321 ms |
11704 KB |
Output is correct |
37 |
Correct |
315 ms |
10016 KB |
Output is correct |
38 |
Correct |
318 ms |
11432 KB |
Output is correct |
39 |
Correct |
319 ms |
11416 KB |
Output is correct |
40 |
Correct |
261 ms |
11360 KB |
Output is correct |
41 |
Correct |
113 ms |
640 KB |
Output is correct |
42 |
Correct |
202 ms |
4036 KB |
Output is correct |
43 |
Correct |
314 ms |
19260 KB |
Output is correct |
44 |
Correct |
171 ms |
14512 KB |
Output is correct |
45 |
Correct |
266 ms |
11420 KB |
Output is correct |
46 |
Correct |
335 ms |
19380 KB |
Output is correct |
47 |
Correct |
249 ms |
5332 KB |
Output is correct |
48 |
Correct |
251 ms |
5316 KB |
Output is correct |