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 <stdio.h>
#define N 3000
#define M 3000
#define N_ (1 << 12) /* N_ = pow2(ceil(log2(N))) */
long long min(long long a, long long b) { return a < b ? a : b; }
long long max(long long a, long long b) { return a > b ? a : b; }
int st[N_ * 2], n_;
void pul(int i) {
int l = i << 1, r = l | 1;
st[i] = max(st[l], st[r]);
}
void build(long long *aa, int n) {
int i;
n_ = 1;
while (n_ < n)
n_ <<= 1;
for (i = 0; i < n; i++)
st[n_ + i] = aa[i];
for (i = n_ - 1; i > 0; i--)
pul(i);
}
int query(int l, int r) {
int x = 0;
for (l += n_, r += n_; l <= r; l >>= 1, r >>= 1) {
if ((l & 1) == 1)
x = max(x, st[l++]);
if ((r & 1) == 0)
x = max(x, st[r--]);
}
return x;
}
int main() {
static long long xx[N + 1];
static int cc[N], pp[N], qq[N], qu[N];
int n, m, i, cnt;
scanf("%d%d", &n, &m);
for (i = 1; i <= n; i++)
scanf("%lld", &xx[i]);
build(xx + 1, n);
for (i = 1; i <= n; i++)
xx[i] += xx[i - 1];
for (i = 0; i < n; i++)
scanf("%d", &cc[i]);
cnt = 0;
for (i = 0; i < n; i++) {
while (cnt && cc[qu[cnt - 1]] > cc[i])
cnt--;
pp[i] = cnt == 0 ? -1 : qu[cnt - 1];
qu[cnt++] = i;
}
cnt = 0;
for (i = n - 1; i >= 0; i--) {
while (cnt && cc[qu[cnt - 1]] >= cc[i])
cnt--;
qq[i] = cnt == 0 ? n : qu[cnt - 1];
qu[cnt++] = i;
}
while (m--) {
int l, r, t;
long long ans;
scanf("%d%d%d", &l, &r, &t), l--, r--;
if (query(l, r - 1) > t) {
printf("-1\n");
continue;
}
ans = 0;
for (i = l; i < r; i++) {
long long xl = pp[i] < l ? xx[i] : max(xx[i], xx[pp[i]] + t);
long long xr = min(xx[i] + t, qq[i] < r ? xx[qq[i]] : xx[r]);
ans += max(xr - xl, 0) * cc[i];
}
printf("%lld\n", ans);
}
return 0;
}
Compilation message (stderr)
Main.c: In function 'main':
Main.c:47:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
47 | scanf("%d%d", &n, &m);
| ^~~~~~~~~~~~~~~~~~~~~
Main.c:49:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
49 | scanf("%lld", &xx[i]);
| ^~~~~~~~~~~~~~~~~~~~~
Main.c:54:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
54 | scanf("%d", &cc[i]);
| ^~~~~~~~~~~~~~~~~~~
Main.c:73:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
73 | scanf("%d%d%d", &l, &r, &t), l--, r--;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |