#include <bits/stdc++.h>
using namespace std;
#define sz(v) int(v.size())
#define ar array
typedef long long ll;
const int N = 5e2+10, MOD = 998244353;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
void solve() {
int n, m; cin >> n >> m;
vector<ll> a(n); for (auto& x : a) cin >> x;
vector<ll> b(n); for (auto& x : b) cin >> x;
vector<int> prv(n, -1), nxt(n, n);
{
vector<int> st;
for (int i = 0; i < n; i++) {
while (sz(st) && b[st.back()] >= b[i]) st.pop_back();
if (sz(st)) prv[i] = st.back();
st.push_back(i);
}
}
{
vector<int> st;
for (int i = n-1; i >= 0; i--) {
while (sz(st) && b[st.back()] > b[i]) st.pop_back();
if (sz(st)) nxt[i] = st.back();
st.push_back(i);
}
}
vector<ll> ps(n);
for (int i = 0; i < n; i++) {
ps[i] = a[i];
if (i) ps[i] += ps[i-1];
}
auto sum = [&](int l, int r) {
return ps[r] - (l ? ps[l-1] : 0LL);
};
while (m--) {
int l, r, cap; cin >> l >> r >> cap, --l, --r;
bool bad = 0;
for (int i = l; i < r; i++) {
if (a[i] > cap) {
bad = 1;
}
}
if (bad) {
cout << -1 << '\n';
continue;
}
ll ans = 0;
for (int i = l; i < r; i++) {
ll lose = (prv[i] >= l ? max(0LL, cap - sum(prv[i], i-1)) : 0LL);
ll cur = min((long long) cap, sum(i, min(r, nxt[i])-1)) - lose;
cur = max(cur, 0LL);
ans += cur * b[i];
}
cout << ans << '\n';
}
}
int main() {
ios::sync_with_stdio(false); cin.tie(0);
int T = 1;
// cin >> T;
while (T--) solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
340 KB |
Output is correct |
2 |
Correct |
12 ms |
440 KB |
Output is correct |
3 |
Correct |
9 ms |
452 KB |
Output is correct |
4 |
Correct |
12 ms |
340 KB |
Output is correct |
5 |
Correct |
12 ms |
340 KB |
Output is correct |
6 |
Correct |
10 ms |
468 KB |
Output is correct |
7 |
Correct |
13 ms |
444 KB |
Output is correct |
8 |
Correct |
12 ms |
340 KB |
Output is correct |
9 |
Correct |
9 ms |
464 KB |
Output is correct |
10 |
Correct |
11 ms |
340 KB |
Output is correct |
11 |
Correct |
12 ms |
468 KB |
Output is correct |
12 |
Correct |
9 ms |
452 KB |
Output is correct |
13 |
Correct |
12 ms |
432 KB |
Output is correct |
14 |
Correct |
10 ms |
420 KB |
Output is correct |
15 |
Correct |
10 ms |
456 KB |
Output is correct |
16 |
Correct |
12 ms |
340 KB |
Output is correct |
17 |
Correct |
9 ms |
340 KB |
Output is correct |
18 |
Correct |
8 ms |
468 KB |
Output is correct |
19 |
Correct |
7 ms |
428 KB |
Output is correct |
20 |
Correct |
28 ms |
428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2540 ms |
2752 KB |
Output is correct |
2 |
Correct |
3107 ms |
2608 KB |
Output is correct |
3 |
Correct |
3070 ms |
2720 KB |
Output is correct |
4 |
Correct |
2360 ms |
2760 KB |
Output is correct |
5 |
Correct |
3075 ms |
2568 KB |
Output is correct |
6 |
Execution timed out |
4075 ms |
6808 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4075 ms |
6748 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
340 KB |
Output is correct |
2 |
Correct |
12 ms |
440 KB |
Output is correct |
3 |
Correct |
9 ms |
452 KB |
Output is correct |
4 |
Correct |
12 ms |
340 KB |
Output is correct |
5 |
Correct |
12 ms |
340 KB |
Output is correct |
6 |
Correct |
10 ms |
468 KB |
Output is correct |
7 |
Correct |
13 ms |
444 KB |
Output is correct |
8 |
Correct |
12 ms |
340 KB |
Output is correct |
9 |
Correct |
9 ms |
464 KB |
Output is correct |
10 |
Correct |
11 ms |
340 KB |
Output is correct |
11 |
Correct |
12 ms |
468 KB |
Output is correct |
12 |
Correct |
9 ms |
452 KB |
Output is correct |
13 |
Correct |
12 ms |
432 KB |
Output is correct |
14 |
Correct |
10 ms |
420 KB |
Output is correct |
15 |
Correct |
10 ms |
456 KB |
Output is correct |
16 |
Correct |
12 ms |
340 KB |
Output is correct |
17 |
Correct |
9 ms |
340 KB |
Output is correct |
18 |
Correct |
8 ms |
468 KB |
Output is correct |
19 |
Correct |
7 ms |
428 KB |
Output is correct |
20 |
Correct |
28 ms |
428 KB |
Output is correct |
21 |
Correct |
2540 ms |
2752 KB |
Output is correct |
22 |
Correct |
3107 ms |
2608 KB |
Output is correct |
23 |
Correct |
3070 ms |
2720 KB |
Output is correct |
24 |
Correct |
2360 ms |
2760 KB |
Output is correct |
25 |
Correct |
3075 ms |
2568 KB |
Output is correct |
26 |
Execution timed out |
4075 ms |
6808 KB |
Time limit exceeded |
27 |
Halted |
0 ms |
0 KB |
- |