#include <bits/stdc++.h>
#define IO ios::sync_with_stdio(false);cin.tie(0);
#define debug(x) cerr << #x << " = " << (x) << ' '
#define bug(x) cerr << (x) << ' '
#define endl cerr << '\n'
#define all(v) (v).begin(), (v).end()
#define SZ(v) (ll)(v).size()
#define lowbit(x) (x)&-(x)
#define pb emplace_back
#define F first
#define S second
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
//#define TEST
const int N = 2e5+5;
const ll LLINF = 2e18;
ll a[N], ans[N];
vector<pll> vL, vR;
int main(void)
{ IO
ll n, i, Q, cur = 0, L = 0, R = 0, lb, mid, ub;
cin >> n >> Q;
for (i=1; i <= n; ++i) cin >> a[i];
for (i=1; i <= Q; ++i)
{
ll x; cin >> x, cur += x;
if (cur < L) L = cur, vL.pb(L, R);
if (cur > R) R = cur, vR.pb(L, R);
//debug(L), debug(R), endl;
}
//for (auto p : vL) debug(p.F), debug(p.S), endl; endl;
//for (auto p : vR) debug(p.F), debug(p.S), endl;
a[0] = -LLINF, a[n+1] = LLINF;
for (i=1; i <= n; ++i)
{
if (!vL.empty())
{
lb = 0, ub = SZ(vL)-1, mid;
while (lb < ub)
{
mid = (lb + ub) >> 1;
auto p = vL[mid];
if (a[i-1]+p.S >= a[i]+p.F) ub = mid;
else lb = mid + 1;
}
auto p = vL[lb];// debug(lb);
ll tmp = -p.F - max(a[i-1]+p.S - (a[i]+p.F), 0LL);
if (lb > 0)
{
p = vL[lb - 1];
tmp = max(tmp, -p.F - max(a[i-1]+p.S - (a[i]+p.F), 0LL));
}
ans[i] += tmp;
}
if (!vR.empty())
{
lb = 0, ub = SZ(vR)-1;
while (lb < ub)
{
mid = (lb + ub) >> 1;
auto p = vR[mid];
if (a[i]+p.S >= a[i+1]+p.F) ub = mid;
else lb = mid + 1;
}
auto p = vR[lb]; //debug(lb), endl;
ll tmp = p.S - max(a[i]+p.S - (a[i+1]+p.F), 0LL);
if (lb > 0)
{
p = vR[lb - 1];
tmp = max(tmp, p.S - max(a[i]+p.S - (a[i+1]+p.F), 0LL));
}
ans[i] += tmp;
}
cout << ans[i] << '\n';
}
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:43:39: warning: right operand of comma operator has no effect [-Wunused-value]
43 | lb = 0, ub = SZ(vL)-1, mid;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2596 KB |
Output is correct |
4 |
Correct |
2 ms |
2656 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2596 KB |
Output is correct |
4 |
Correct |
2 ms |
2656 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |