#define DEBUG 0
#include <bits/stdc++.h>
using namespace std;
#if DEBUG
// basic debugging macros
int __i__,__j__;
#define printLine(l) for(__i__=0;__i__<l;__i__++){cout<<"-";}cout<<endl
#define printLine2(l,c) for(__i__=0;__i__<l;__i__++){cout<<c;}cout<<endl
#define printVar(n) cout<<#n<<": "<<n<<endl
#define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl
#define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;}
#define print2dArr2(a,r,c,l) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<setw(l)<<setfill(' ')<<a[__i__][__j__]<<" ";}cout<<endl;}
// advanced debugging class
// debug 1,2,'A',"test";
class _Debug {
public:
template<typename T>
_Debug& operator,(T val) {
cout << val << endl;
return *this;
}
};
#define debug _Debug(),
#else
#define printLine(l)
#define printLine2(l,c)
#define printVar(n)
#define printArr(a,l)
#define print2dArr(a,r,c)
#define print2dArr2(a,r,c,l)
#define debug
#endif
// define
#define MAX_VAL 999999999
#define MAX_VAL_2 999999999999999999LL
#define EPS 1e-6
#define mp make_pair
#define pb push_back
// typedef
typedef unsigned int UI;
typedef long long int LLI;
typedef unsigned long long int ULLI;
typedef unsigned short int US;
typedef pair<int,int> pii;
typedef pair<LLI,LLI> plli;
typedef vector<int> vi;
typedef vector<LLI> vlli;
typedef vector<pii> vpii;
typedef vector<plli> vplli;
// ---------- END OF TEMPLATE ----------
int A[200000],B[200000],S[200000],T[200000],U[200000];
LLI pre[200001];
int l[200000],r[200000];
vi s;
int sparse[200000][18],logg[200001];
int query(int s,int e) {
int l = logg[e-s+1];
return max(sparse[s][l],sparse[e-(1 << l)+1][l]);
}
int main() {
int i;
int N,M;
scanf("%d %d",&N,&M);
for (i = 0; i < N; i++) scanf("%d",&A[i]),pre[i+1] = pre[i]+A[i];
for (i = 0; i < N; i++) scanf("%d",&B[i]);
for (i = 0; i < M; i++) scanf("%d %d %d",&S[i],&T[i],&U[i]),S[i]--,T[i] -= 2;
int j;
for (i = 0; i < N; i++) {
while (!s.empty() && (B[s.back()] > B[i])) s.pop_back();
if (s.empty()) l[i] = -1;
else l[i] = s.back();
s.pb(i);
}
s.clear();
for (i = N-1; i >= 0; i--) {
while (!s.empty() && (B[s.back()] >= B[i])) s.pop_back();
if (s.empty()) r[i] = N;
else r[i] = s.back();
s.pb(i);
}
for (i = 0; i < N; i++) sparse[i][0] = A[i];
for (i = 1; (1 << i) <= N; i++) {
for (j = 0; j <= N-(1 << i); j++) sparse[j][i] = max(sparse[j][i-1],sparse[j+(1 << (i-1))][i-1]);
}
for (i = 2; i <= N; i++) logg[i] = logg[i/2]+1;
for (i = 0; i < M; i++) {
if (query(S[i],T[i]) > U[i]) printf("-1\n");
else {
LLI ans = 0;
for (j = S[i]; j <= T[i]; j++) {
if ((l[j] >= S[i]) && (r[j] <= T[i])) {
LLI a = min(pre[j]-pre[l[j]],(LLI) U[i]);
LLI b = min(pre[r[j]]-pre[j],(LLI) U[i]);
ans += (a+b-min(a+b,(LLI) U[i]))*B[j];
}
else if (l[j] >= S[i]) {
LLI a = min(pre[j]-pre[l[j]],(LLI) U[i]);
LLI b = min(pre[T[i]+1]-pre[j],(LLI) U[i]);
ans += (a+b-min(a+b,(LLI) U[i]))*B[j];
}
else if (r[j] <= T[i]) {
LLI b = min(pre[r[j]]-pre[j],(LLI) U[i]);
ans += b*B[j];
}
else {
LLI b = min(pre[T[i]+1]-pre[j],(LLI) U[i]);
ans += b*B[j];
}
}
printf("%lld\n",ans);
}
}
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:70:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
70 | scanf("%d %d",&N,&M);
| ~~~~~^~~~~~~~~~~~~~~
Main.cpp:71:34: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
71 | for (i = 0; i < N; i++) scanf("%d",&A[i]),pre[i+1] = pre[i]+A[i];
| ~~~~~^~~~~~~~~~~~
Main.cpp:72:34: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
72 | for (i = 0; i < N; i++) scanf("%d",&B[i]);
| ~~~~~^~~~~~~~~~~~
Main.cpp:73:34: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
73 | for (i = 0; i < M; i++) scanf("%d %d %d",&S[i],&T[i],&U[i]),S[i]--,T[i] -= 2;
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
716 KB |
Output is correct |
2 |
Correct |
15 ms |
716 KB |
Output is correct |
3 |
Correct |
9 ms |
716 KB |
Output is correct |
4 |
Correct |
17 ms |
808 KB |
Output is correct |
5 |
Correct |
15 ms |
704 KB |
Output is correct |
6 |
Correct |
10 ms |
828 KB |
Output is correct |
7 |
Correct |
15 ms |
776 KB |
Output is correct |
8 |
Correct |
15 ms |
820 KB |
Output is correct |
9 |
Correct |
9 ms |
820 KB |
Output is correct |
10 |
Correct |
14 ms |
788 KB |
Output is correct |
11 |
Correct |
14 ms |
848 KB |
Output is correct |
12 |
Correct |
9 ms |
844 KB |
Output is correct |
13 |
Correct |
15 ms |
808 KB |
Output is correct |
14 |
Correct |
9 ms |
716 KB |
Output is correct |
15 |
Correct |
10 ms |
808 KB |
Output is correct |
16 |
Correct |
15 ms |
844 KB |
Output is correct |
17 |
Correct |
11 ms |
716 KB |
Output is correct |
18 |
Correct |
10 ms |
716 KB |
Output is correct |
19 |
Correct |
7 ms |
716 KB |
Output is correct |
20 |
Correct |
33 ms |
816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2296 ms |
8308 KB |
Output is correct |
2 |
Correct |
3535 ms |
8120 KB |
Output is correct |
3 |
Correct |
3524 ms |
8472 KB |
Output is correct |
4 |
Correct |
1917 ms |
8336 KB |
Output is correct |
5 |
Correct |
3528 ms |
8460 KB |
Output is correct |
6 |
Execution timed out |
4088 ms |
29280 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4081 ms |
28876 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
716 KB |
Output is correct |
2 |
Correct |
15 ms |
716 KB |
Output is correct |
3 |
Correct |
9 ms |
716 KB |
Output is correct |
4 |
Correct |
17 ms |
808 KB |
Output is correct |
5 |
Correct |
15 ms |
704 KB |
Output is correct |
6 |
Correct |
10 ms |
828 KB |
Output is correct |
7 |
Correct |
15 ms |
776 KB |
Output is correct |
8 |
Correct |
15 ms |
820 KB |
Output is correct |
9 |
Correct |
9 ms |
820 KB |
Output is correct |
10 |
Correct |
14 ms |
788 KB |
Output is correct |
11 |
Correct |
14 ms |
848 KB |
Output is correct |
12 |
Correct |
9 ms |
844 KB |
Output is correct |
13 |
Correct |
15 ms |
808 KB |
Output is correct |
14 |
Correct |
9 ms |
716 KB |
Output is correct |
15 |
Correct |
10 ms |
808 KB |
Output is correct |
16 |
Correct |
15 ms |
844 KB |
Output is correct |
17 |
Correct |
11 ms |
716 KB |
Output is correct |
18 |
Correct |
10 ms |
716 KB |
Output is correct |
19 |
Correct |
7 ms |
716 KB |
Output is correct |
20 |
Correct |
33 ms |
816 KB |
Output is correct |
21 |
Correct |
2296 ms |
8308 KB |
Output is correct |
22 |
Correct |
3535 ms |
8120 KB |
Output is correct |
23 |
Correct |
3524 ms |
8472 KB |
Output is correct |
24 |
Correct |
1917 ms |
8336 KB |
Output is correct |
25 |
Correct |
3528 ms |
8460 KB |
Output is correct |
26 |
Execution timed out |
4088 ms |
29280 KB |
Time limit exceeded |
27 |
Halted |
0 ms |
0 KB |
- |