#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int a[200200], b[200200];
ll S[200200], ans;
struct Seg{
pair<int, int> tree[400400];
int sz;
void init(int n){
sz = n;
for (int i=sz;i<sz*2;i++) tree[i] = {b[i-sz], i-sz};
for (int i=sz-1;i;i--) tree[i] = min(tree[i<<1], tree[i<<1|1]);
}
pair<int, int> query(int l, int r){
pair<int, int> ret = {2e9, 0};
for (l+=sz, r+=sz;l<r;l>>=1, r>>=1){
if (l&1) ret = min(ret, tree[l++]);
if (r&1) ret = min(ret, tree[--r]);
}
return ret;
}
}tree;
void dnc(int s, int t, int u, int h){
if (ans==-1) return;
if (s>=t) return;
auto p = tree.query(s, t);
ll need = min(S[t-1] - S[p.second-1], (ll)u);
ll cur = max(h - (S[p.second-1] - S[s-1]), 0LL);
need = max(need, cur);
if (a[p.second]>u) {ans = -1; return;}
ans += (need-cur) * p.first;
//printf(" %d %d %d %d: %lld %lld / %d %d\n", s, t, u, h, need, cur, p.first, p.second);
dnc(s, p.second, u, h);
dnc(p.second+1, t, u, need - a[p.second]);
}
int main(){
int n, q;
scanf("%d %d", &n, &q);
for (int i=1;i<=n;i++) scanf("%d", a+i);
for (int i=1;i<=n;i++) scanf("%d", b+i);
for (int i=1;i<=n;i++) S[i] = S[i-1] + a[i];
tree.init(n+1);
while(q--){
int s, t, u;
scanf("%d %d %d", &s, &t, &u);
ans = 0;
dnc(s, t, u, 0);
printf("%lld\n", ans);
}
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:44:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
44 | scanf("%d %d", &n, &q);
| ~~~~~^~~~~~~~~~~~~~~~~
Main.cpp:45:33: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
45 | for (int i=1;i<=n;i++) scanf("%d", a+i);
| ~~~~~^~~~~~~~~~~
Main.cpp:46:33: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
46 | for (int i=1;i<=n;i++) scanf("%d", b+i);
| ~~~~~^~~~~~~~~~~
Main.cpp:53:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
53 | scanf("%d %d %d", &s, &t, &u);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
92 ms |
3704 KB |
Output is correct |
2 |
Correct |
172 ms |
3572 KB |
Output is correct |
3 |
Correct |
173 ms |
3788 KB |
Output is correct |
4 |
Correct |
91 ms |
3532 KB |
Output is correct |
5 |
Correct |
187 ms |
3792 KB |
Output is correct |
6 |
Correct |
184 ms |
3820 KB |
Output is correct |
7 |
Correct |
96 ms |
3532 KB |
Output is correct |
8 |
Correct |
183 ms |
3576 KB |
Output is correct |
9 |
Correct |
194 ms |
3868 KB |
Output is correct |
10 |
Correct |
86 ms |
3588 KB |
Output is correct |
11 |
Correct |
168 ms |
3564 KB |
Output is correct |
12 |
Correct |
179 ms |
4188 KB |
Output is correct |
13 |
Correct |
68 ms |
3584 KB |
Output is correct |
14 |
Correct |
186 ms |
3800 KB |
Output is correct |
15 |
Correct |
177 ms |
3808 KB |
Output is correct |
16 |
Correct |
187 ms |
3692 KB |
Output is correct |
17 |
Correct |
61 ms |
3568 KB |
Output is correct |
18 |
Correct |
119 ms |
3572 KB |
Output is correct |
19 |
Correct |
123 ms |
3796 KB |
Output is correct |
20 |
Correct |
478 ms |
3660 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4050 ms |
8984 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4100 ms |
9096 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
92 ms |
3704 KB |
Output is correct |
2 |
Correct |
172 ms |
3572 KB |
Output is correct |
3 |
Correct |
173 ms |
3788 KB |
Output is correct |
4 |
Correct |
91 ms |
3532 KB |
Output is correct |
5 |
Correct |
187 ms |
3792 KB |
Output is correct |
6 |
Correct |
184 ms |
3820 KB |
Output is correct |
7 |
Correct |
96 ms |
3532 KB |
Output is correct |
8 |
Correct |
183 ms |
3576 KB |
Output is correct |
9 |
Correct |
194 ms |
3868 KB |
Output is correct |
10 |
Correct |
86 ms |
3588 KB |
Output is correct |
11 |
Correct |
168 ms |
3564 KB |
Output is correct |
12 |
Correct |
179 ms |
4188 KB |
Output is correct |
13 |
Correct |
68 ms |
3584 KB |
Output is correct |
14 |
Correct |
186 ms |
3800 KB |
Output is correct |
15 |
Correct |
177 ms |
3808 KB |
Output is correct |
16 |
Correct |
187 ms |
3692 KB |
Output is correct |
17 |
Correct |
61 ms |
3568 KB |
Output is correct |
18 |
Correct |
119 ms |
3572 KB |
Output is correct |
19 |
Correct |
123 ms |
3796 KB |
Output is correct |
20 |
Correct |
478 ms |
3660 KB |
Output is correct |
21 |
Execution timed out |
4050 ms |
8984 KB |
Time limit exceeded |
22 |
Halted |
0 ms |
0 KB |
- |