#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
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--;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
468 KB |
Output is correct |
2 |
Correct |
10 ms |
392 KB |
Output is correct |
3 |
Correct |
8 ms |
472 KB |
Output is correct |
4 |
Correct |
11 ms |
392 KB |
Output is correct |
5 |
Correct |
10 ms |
468 KB |
Output is correct |
6 |
Correct |
8 ms |
512 KB |
Output is correct |
7 |
Correct |
11 ms |
480 KB |
Output is correct |
8 |
Correct |
10 ms |
504 KB |
Output is correct |
9 |
Correct |
11 ms |
428 KB |
Output is correct |
10 |
Correct |
10 ms |
468 KB |
Output is correct |
11 |
Correct |
9 ms |
396 KB |
Output is correct |
12 |
Correct |
8 ms |
468 KB |
Output is correct |
13 |
Correct |
11 ms |
468 KB |
Output is correct |
14 |
Correct |
8 ms |
468 KB |
Output is correct |
15 |
Correct |
9 ms |
468 KB |
Output is correct |
16 |
Correct |
10 ms |
428 KB |
Output is correct |
17 |
Correct |
8 ms |
468 KB |
Output is correct |
18 |
Correct |
8 ms |
496 KB |
Output is correct |
19 |
Correct |
9 ms |
512 KB |
Output is correct |
20 |
Correct |
24 ms |
476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
468 KB |
Output is correct |
2 |
Correct |
10 ms |
392 KB |
Output is correct |
3 |
Correct |
8 ms |
472 KB |
Output is correct |
4 |
Correct |
11 ms |
392 KB |
Output is correct |
5 |
Correct |
10 ms |
468 KB |
Output is correct |
6 |
Correct |
8 ms |
512 KB |
Output is correct |
7 |
Correct |
11 ms |
480 KB |
Output is correct |
8 |
Correct |
10 ms |
504 KB |
Output is correct |
9 |
Correct |
11 ms |
428 KB |
Output is correct |
10 |
Correct |
10 ms |
468 KB |
Output is correct |
11 |
Correct |
9 ms |
396 KB |
Output is correct |
12 |
Correct |
8 ms |
468 KB |
Output is correct |
13 |
Correct |
11 ms |
468 KB |
Output is correct |
14 |
Correct |
8 ms |
468 KB |
Output is correct |
15 |
Correct |
9 ms |
468 KB |
Output is correct |
16 |
Correct |
10 ms |
428 KB |
Output is correct |
17 |
Correct |
8 ms |
468 KB |
Output is correct |
18 |
Correct |
8 ms |
496 KB |
Output is correct |
19 |
Correct |
9 ms |
512 KB |
Output is correct |
20 |
Correct |
24 ms |
476 KB |
Output is correct |
21 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 11 |
22 |
Halted |
0 ms |
0 KB |
- |