#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(),a.end()
const int LIM=2e5+7;
ll A[LIM], pos[LIM], B[LIM], mi[LIM], mil[LIM];
ll solve(int l, int r, ll u) {
for(int i=l; i<r; ++i) if(A[i]>u) return -1;
int pocz=l;
ll ans=0;
while(l<r) {
ll prawo=min(pos[mi[l]], pos[r]);
if(mil[l]<pocz || pos[l]-pos[mil[l]]>=u) {
ans+=B[l]*min(prawo-pos[l], u);
} else if(prawo-pos[mil[l]]>u) {
ans+=B[l]*min(pos[l]-pos[mil[l]], prawo-pos[mil[l]]-u);
}
++l;
}
return ans;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int n, m;
cin >> n >> m;
rep(i, n) {
cin >> A[i];
pos[i+1]=pos[i]+A[i];
}
rep(i, n) cin >> B[i];
A[n]=-1;
stack<pair<ll,ll>>S;
S.push({-1, n});
for(int i=n-1; i>=0; --i) {
while(S.top().st>B[i]) S.pop();
mi[i]=S.top().nd;
S.push({B[i], i});
}
while(!S.empty()) S.pop();
S.push({-1, -1});
rep(i, n) {
while(S.top().st>=B[i]) S.pop();
mil[i]=S.top().nd;
S.push({B[i], i});
}
while(m--) {
ll l, r, u;
cin >> l >> r >> u; --l; --r;
cout << solve(l, r, u) << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
468 KB |
Output is correct |
2 |
Correct |
9 ms |
636 KB |
Output is correct |
3 |
Correct |
9 ms |
596 KB |
Output is correct |
4 |
Correct |
11 ms |
568 KB |
Output is correct |
5 |
Correct |
9 ms |
600 KB |
Output is correct |
6 |
Correct |
7 ms |
596 KB |
Output is correct |
7 |
Correct |
11 ms |
576 KB |
Output is correct |
8 |
Correct |
9 ms |
596 KB |
Output is correct |
9 |
Correct |
7 ms |
596 KB |
Output is correct |
10 |
Correct |
11 ms |
576 KB |
Output is correct |
11 |
Correct |
9 ms |
592 KB |
Output is correct |
12 |
Correct |
8 ms |
580 KB |
Output is correct |
13 |
Correct |
11 ms |
600 KB |
Output is correct |
14 |
Correct |
7 ms |
600 KB |
Output is correct |
15 |
Correct |
8 ms |
628 KB |
Output is correct |
16 |
Correct |
9 ms |
632 KB |
Output is correct |
17 |
Correct |
7 ms |
592 KB |
Output is correct |
18 |
Correct |
6 ms |
624 KB |
Output is correct |
19 |
Correct |
5 ms |
592 KB |
Output is correct |
20 |
Correct |
19 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1755 ms |
3832 KB |
Output is correct |
2 |
Correct |
3404 ms |
3044 KB |
Output is correct |
3 |
Correct |
2241 ms |
5396 KB |
Output is correct |
4 |
Correct |
1700 ms |
5444 KB |
Output is correct |
5 |
Correct |
2399 ms |
4532 KB |
Output is correct |
6 |
Execution timed out |
4067 ms |
14756 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4033 ms |
8364 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
468 KB |
Output is correct |
2 |
Correct |
9 ms |
636 KB |
Output is correct |
3 |
Correct |
9 ms |
596 KB |
Output is correct |
4 |
Correct |
11 ms |
568 KB |
Output is correct |
5 |
Correct |
9 ms |
600 KB |
Output is correct |
6 |
Correct |
7 ms |
596 KB |
Output is correct |
7 |
Correct |
11 ms |
576 KB |
Output is correct |
8 |
Correct |
9 ms |
596 KB |
Output is correct |
9 |
Correct |
7 ms |
596 KB |
Output is correct |
10 |
Correct |
11 ms |
576 KB |
Output is correct |
11 |
Correct |
9 ms |
592 KB |
Output is correct |
12 |
Correct |
8 ms |
580 KB |
Output is correct |
13 |
Correct |
11 ms |
600 KB |
Output is correct |
14 |
Correct |
7 ms |
600 KB |
Output is correct |
15 |
Correct |
8 ms |
628 KB |
Output is correct |
16 |
Correct |
9 ms |
632 KB |
Output is correct |
17 |
Correct |
7 ms |
592 KB |
Output is correct |
18 |
Correct |
6 ms |
624 KB |
Output is correct |
19 |
Correct |
5 ms |
592 KB |
Output is correct |
20 |
Correct |
19 ms |
596 KB |
Output is correct |
21 |
Correct |
1755 ms |
3832 KB |
Output is correct |
22 |
Correct |
3404 ms |
3044 KB |
Output is correct |
23 |
Correct |
2241 ms |
5396 KB |
Output is correct |
24 |
Correct |
1700 ms |
5444 KB |
Output is correct |
25 |
Correct |
2399 ms |
4532 KB |
Output is correct |
26 |
Execution timed out |
4067 ms |
14756 KB |
Time limit exceeded |
27 |
Halted |
0 ms |
0 KB |
- |