This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool mnto(T& a, T b) {return a > b ? a = b, 1 : 0;}
template <class T>
inline bool mxto(T& a, T b) {return a < b ? a = b, 1: 0;}
#define REP(i, s, e) for (int i = s; i < e; i++)
#define RREP(i, s, e) for (int i = s; i >= e; i--)
typedef long long ll;
typedef long double ld;
#define MP make_pair
#define FI first
#define SE second
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
#define MT make_tuple
typedef tuple<int, int, int> iii;
#define ALL(_a) _a.begin(), _a.end()
#define pb push_back
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
#ifdef DEBUG
#define debug(args...) _debug(args)
void _debug(const char* format, ...) {
va_list args;
va_start(args, format);
vprintf(format, args);
va_end(args);
}
#else
#define debug(args...)
#endif
#define INF 1000000005
#define LINF 1000000000000000005
#define MOD 1000000007
#define MAXN 200005
int n, q;
ll x[MAXN], w[MAXN];
ll psum[MAXN], pmx[MAXN], pmn[MAXN], l[MAXN], r[MAXN];
int main() {
scanf("%d%d", &n, &q);
REP (i, 1, n + 1) {
scanf("%lld", &x[i]);
}
x[0] = -LINF;
x[n + 1] = LINF;
REP (i, 1, q + 1) {
scanf("%lld", &w[i]);
psum[i] = psum[i - 1] + w[i];
pmx[i] = max(pmx[i - 1], psum[i]);
pmn[i] = min(pmn[i - 1], psum[i]);
debug("%d: %lld %lld %lld\n", i, psum[i], pmx[i], pmn[i]);
}
REP (i, 1, n + 2) {
ll gp = x[i] - x[i - 1];
int res = -1;
int lo = 1, hi = q, mid;
while (lo <= hi) {
mid = lo + hi >> 1;
if (pmx[mid] - pmn[mid] > gp) {
res = mid;
hi = mid - 1;
} else {
lo = mid + 1;
}
}
debug("%d\n", res);
if (res == -1) {
r[i - 1] = pmx[q];
l[i] = pmn[q];
} else {
assert(w[res] != 0);
if (w[res] > 0) {
l[i] = pmn[res - 1];
r[i - 1] = gp + l[i];
} else {
r[i - 1] = pmx[res - 1];
l[i] = r[i - 1] - gp;
}
}
}
REP (i, 1, n + 1) {
printf("%lld\n", r[i] - l[i]);
}
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 (stderr)
Main.cpp: In function 'int main()':
Main.cpp:65:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
65 | mid = lo + hi >> 1;
| ~~~^~~~
Main.cpp:47:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
47 | scanf("%d%d", &n, &q);
| ~~~~~^~~~~~~~~~~~~~~~
Main.cpp:49:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
49 | scanf("%lld", &x[i]);
| ~~~~~^~~~~~~~~~~~~~~
Main.cpp:54:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
54 | scanf("%lld", &w[i]);
| ~~~~~^~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |