#include <bits/stdc++.h>
#define se second
#define fs first
#define mp make_pair
#define pb push_back
#define ll long long
#define ii pair<ll,ll>
#define ld long double
#define SZ(v) (int)v.size()
#define ALL(v) v.begin(), v.end()
#define bit(msk, i) ((msk >> i) & 1)
#define iter(id, v) for(auto id : v)
#define rep(i,m,n) for(int i=(m); i<=(n); i++)
#define reb(i,m,n) for(int i=(m); i>=(n); i--)
using namespace std;
mt19937_64 rd(chrono :: steady_clock :: now().time_since_epoch().count());
ll Rand(ll l, ll r) { return uniform_int_distribution<ll> (l, r)(rd); }
const int N = 2e5 + 7;
const int Mod = 1e9 + 7;
const int szBL = 320;
const ll INF = 1e18;
const int BASE = 137;
int n, Q;
ll a[N];
ll qr[N];
ll premx[N], premn[N];
void solution () {
cin >> n >> Q;
rep (i, 1, n) cin >> a[i];
ll pre = 0;
rep (i, 1, Q) {
cin >> qr[i];
pre += qr[i];
// fenmx.update (i, pre);
// fenmn.update (i, pre);
premx[i] = max(premx[i - 1], pre);
premn[i] = min(premn[i - 1], pre);
// cout << pre <<"\n";
}
// cout << fenmn.get(Q) <<" aa\n";
// return;
a[n + 1] = INF;
a[0] = -INF;
rep (i, 1, n) {
ll R; {
ll lf = a[i], rt = INF;
while (lf < rt) {
ll X = lf + rt + 1 >> 1;
int posR; {
int lf = 0, rt = Q;
while (lf < rt) {
int mid = lf + rt >> 1;
if (a[i] + premx[mid] >= X) rt = mid;
else lf = mid + 1;
}
if (a[i] + premx[rt] >= X) posR = rt;
else posR = -1;
}
if (posR == -1 || a[i + 1] + premn[posR] < X) rt = X - 1;
else lf = X;
}
R = lf;
}
ll L; {
ll lf = -INF, rt = a[i];
while (lf < rt) {
ll X = lf + rt >> 1;
int posL; {
int lf = 0, rt = Q;
while (lf < rt) {
int mid = (lf + rt) / 2;
if (a[i] + premn[mid] <= X) rt = mid;
else lf = mid + 1;
}
if (a[i] + premn[rt] <= X) posL = rt;
else posL = -1;
}
// cout << i << " "<<X << " "<<a[i] + fenmn.get(Q) <<"\n";
if (posL == -1 || a[i - 1] + premx[posL] > X) lf = X + 1;
else rt = X;
}
L = lf;
}
cout << R - L<<"\n";
}
}
#define file(name) freopen(name".inp", "r", stdin); \
freopen(name".out", "w", stdout);
int main () {
// file("c");
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
ll num_Test = 1;
// cin >> num_Test;
while(num_Test--)
solution();
}
Compilation message
Main.cpp: In function 'void solution()':
Main.cpp:53:32: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
53 | ll X = lf + rt + 1 >> 1;
| ~~~~~~~~^~~
Main.cpp:57:38: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
57 | int mid = lf + rt >> 1;
| ~~~^~~~
Main.cpp:72:27: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
72 | ll X = lf + rt >> 1;
| ~~~^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
5 ms |
604 KB |
Output is correct |
5 |
Correct |
5 ms |
604 KB |
Output is correct |
6 |
Correct |
5 ms |
348 KB |
Output is correct |
7 |
Correct |
4 ms |
348 KB |
Output is correct |
8 |
Correct |
4 ms |
348 KB |
Output is correct |
9 |
Correct |
5 ms |
348 KB |
Output is correct |
10 |
Correct |
3 ms |
348 KB |
Output is correct |
11 |
Correct |
3 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
5 ms |
604 KB |
Output is correct |
16 |
Correct |
5 ms |
604 KB |
Output is correct |
17 |
Correct |
4 ms |
604 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
3 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
5 ms |
604 KB |
Output is correct |
5 |
Correct |
5 ms |
604 KB |
Output is correct |
6 |
Correct |
5 ms |
348 KB |
Output is correct |
7 |
Correct |
4 ms |
348 KB |
Output is correct |
8 |
Correct |
4 ms |
348 KB |
Output is correct |
9 |
Correct |
5 ms |
348 KB |
Output is correct |
10 |
Correct |
3 ms |
348 KB |
Output is correct |
11 |
Correct |
3 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
5 ms |
604 KB |
Output is correct |
16 |
Correct |
5 ms |
604 KB |
Output is correct |
17 |
Correct |
4 ms |
604 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
3 ms |
348 KB |
Output is correct |
20 |
Correct |
19 ms |
7140 KB |
Output is correct |
21 |
Correct |
17 ms |
6996 KB |
Output is correct |
22 |
Correct |
17 ms |
6748 KB |
Output is correct |
23 |
Correct |
25 ms |
6588 KB |
Output is correct |
24 |
Correct |
107 ms |
6908 KB |
Output is correct |
25 |
Correct |
913 ms |
11788 KB |
Output is correct |
26 |
Correct |
903 ms |
11860 KB |
Output is correct |
27 |
Correct |
905 ms |
11604 KB |
Output is correct |
28 |
Correct |
924 ms |
11720 KB |
Output is correct |
29 |
Correct |
824 ms |
11096 KB |
Output is correct |
30 |
Correct |
624 ms |
10580 KB |
Output is correct |
31 |
Correct |
459 ms |
10064 KB |
Output is correct |
32 |
Correct |
481 ms |
10320 KB |
Output is correct |
33 |
Correct |
83 ms |
1504 KB |
Output is correct |
34 |
Correct |
797 ms |
12136 KB |
Output is correct |
35 |
Correct |
795 ms |
11604 KB |
Output is correct |
36 |
Correct |
916 ms |
11976 KB |
Output is correct |
37 |
Correct |
903 ms |
11652 KB |
Output is correct |
38 |
Correct |
890 ms |
11516 KB |
Output is correct |
39 |
Correct |
850 ms |
11784 KB |
Output is correct |
40 |
Correct |
485 ms |
11756 KB |
Output is correct |
41 |
Correct |
18 ms |
7772 KB |
Output is correct |
42 |
Correct |
460 ms |
10288 KB |
Output is correct |
43 |
Correct |
441 ms |
13500 KB |
Output is correct |
44 |
Correct |
18 ms |
7772 KB |
Output is correct |
45 |
Correct |
496 ms |
11684 KB |
Output is correct |
46 |
Correct |
441 ms |
13432 KB |
Output is correct |
47 |
Correct |
443 ms |
13744 KB |
Output is correct |
48 |
Correct |
451 ms |
13908 KB |
Output is correct |