#include<bits/stdc++.h>
using namespace std;
typedef long double ld;
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 ll INF=1e9+7;
const int LIM=2e5+7;
ll A[LIM], B[LIM], lewo[LIM], prawo[LIM], wynik[LIM], indl[LIM], indr[LIM], trma[4*LIM], N=1;
vector<pair<pair<ll,ll>,pair<ll,ll>>>V;
vector<int>L[LIM], R[LIM];
pair<pair<ll,ll>,ll>pyt[LIM];
pair<ll,ll>tr[4*LIM];
pair<ll,ll>dodaj(pair<ll,ll>a, pair<ll,ll>b) {
return {a.st+b.st, a.nd+b.nd};
}
void upd(int v, pair<ll,ll>x) {
v+=N;
while(v) {
tr[v]=dodaj(tr[v], x);
v/=2;
}
}
pair<ll,ll>cnt(int l, int r) {
l+=N; r+=N;
pair<ll,ll>ans=tr[l];
if(l!=r) ans=dodaj(ans, tr[r]);
while(l/2!=r/2) {
if(l%2==0) ans=dodaj(ans, tr[l+1]);
if(r%2==1) ans=dodaj(ans, tr[r-1]);
l/=2; r/=2;
}
return ans;
}
ll cntma(int l, int r) {
l+=N; r+=N;
ll ans=max(trma[l], trma[r]);
while(l/2!=r/2) {
if(l%2==0) ans=max(ans, trma[l+1]);
if(r%2==1) ans=max(ans, trma[r-1]);
l/=2; r/=2;
}
return ans;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int n, m;
cin >> n >> m;
while(N<=n+3) N*=2;
A[1]=INF;
rep(i, n) {
cin >> A[i+2];
trma[N+i+1]=A[i+2];
A[i+2]+=A[i+1];
}
rep(i, n) cin >> B[i+1];
vector<int>S;
rep(i, n+2) {
while(S.size() && B[S.back()]>B[i]) S.pop_back();
if(S.size()) lewo[i]=S.back();
S.pb(i);
}
S.clear();
for(int i=n+1; i>=0; --i) {
while(S.size() && B[S.back()]>=B[i]) S.pop_back();
if(S.size()) prawo[i]=S.back(); else prawo[i]=n-1;
S.pb(i);
}
rep(i, m) {
cin >> pyt[i].st.st >> pyt[i].st.nd >> pyt[i].nd;
L[pyt[i].st.st].pb(i);
R[pyt[i].st.nd-1].pb(i);
}
S.clear();
vector<ll>pref;
for(int i=n+1; i; --i) {
while(S.size() && B[S.back()]>=B[i]) {
S.pop_back();
pref.pop_back();
}
if(S.size()) pref.pb(B[i]*(A[S.back()]-A[i])+pref.back());
else pref.pb(0);
S.pb(i);
for(auto j : L[i]) {
int po=0, ko=(int)S.size()-1;
while(po<ko) {
int sr=(po+ko)/2;
if(A[S[sr]]<=min(A[i]+pyt[j].nd, A[pyt[j].st.nd])) ko=sr; else po=sr+1;
}
wynik[j]+=pref.back()-pref[po];
wynik[j]+=B[S[po]]*min(min(A[prawo[S[po]]], A[pyt[j].st.nd])-A[S[po]], pyt[j].nd);
indl[j]=S[po]+1;
}
}
S.clear();
rep(i, n+1) {
while(S.size() && B[S.back()]>B[i]) S.pop_back();
S.pb(i);
for(auto j : R[i]) {
int po=0, ko=(int)S.size()-1;
while(po<ko) {
int sr=(po+ko)/2;
if(A[S[sr]]<max(A[pyt[j].st.nd]-pyt[j].nd, A[pyt[j].st.st])) po=sr+1; else ko=sr;
}
if(indl[j]<=S[po]) {
wynik[j]+=B[S[po]]*(A[pyt[j].st.nd]-A[S[po]]);
wynik[j]+=B[S[po]]*min(A[S[po]]-A[lewo[S[po]]]-pyt[j].nd, 0ll);
}
indr[j]=S[po]-1;
}
}
for(int i=1; i<=n; ++i) {
V.pb({{A[i]-A[lewo[i]], -i}, {(A[i]-A[lewo[i]])*B[i], -B[i]}});
V.pb({{A[prawo[i]]-A[i], -i}, {(A[prawo[i]]-A[i])*B[i], -B[i]}});
V.pb({{A[prawo[i]]-A[lewo[i]], -i}, {(A[lewo[i]]-A[prawo[i]])*B[i], B[i]}});
tr[N+i]={0, B[i]};
}
for(int i=N-1; i; --i) {
tr[i]=dodaj(tr[2*i], tr[2*i+1]);
trma[i]=max(trma[2*i], trma[2*i+1]);
}
rep(i, m) if(indl[i]<=indr[i]) V.pb({{pyt[i].nd, i}, {indl[i], indr[i]}});
sort(all(V));
for(auto i : V) {
if(i.st.nd<0) upd(-i.st.nd, i.nd);
else {
pair<ll,ll>x=cnt(i.nd.st, i.nd.nd);
wynik[i.st.nd]+=x.st+x.nd*i.st.st;
}
}
rep(i, m) if(cntma(pyt[i].st.st, pyt[i].st.nd-1)>pyt[i].nd) wynik[i]=-1;
rep(i, m) cout << wynik[i] << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
25820 KB |
Output is correct |
2 |
Correct |
8 ms |
26072 KB |
Output is correct |
3 |
Correct |
8 ms |
25852 KB |
Output is correct |
4 |
Correct |
8 ms |
25820 KB |
Output is correct |
5 |
Correct |
7 ms |
25824 KB |
Output is correct |
6 |
Correct |
7 ms |
25852 KB |
Output is correct |
7 |
Correct |
7 ms |
25792 KB |
Output is correct |
8 |
Correct |
8 ms |
25820 KB |
Output is correct |
9 |
Correct |
7 ms |
25852 KB |
Output is correct |
10 |
Correct |
8 ms |
25824 KB |
Output is correct |
11 |
Correct |
8 ms |
25840 KB |
Output is correct |
12 |
Correct |
8 ms |
25940 KB |
Output is correct |
13 |
Correct |
8 ms |
25908 KB |
Output is correct |
14 |
Correct |
7 ms |
25852 KB |
Output is correct |
15 |
Correct |
7 ms |
25852 KB |
Output is correct |
16 |
Correct |
8 ms |
25972 KB |
Output is correct |
17 |
Correct |
8 ms |
25820 KB |
Output is correct |
18 |
Correct |
7 ms |
25820 KB |
Output is correct |
19 |
Correct |
8 ms |
25852 KB |
Output is correct |
20 |
Correct |
7 ms |
25816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
41424 KB |
Output is correct |
2 |
Correct |
72 ms |
40132 KB |
Output is correct |
3 |
Correct |
65 ms |
41540 KB |
Output is correct |
4 |
Correct |
71 ms |
41676 KB |
Output is correct |
5 |
Correct |
71 ms |
41924 KB |
Output is correct |
6 |
Correct |
348 ms |
88152 KB |
Output is correct |
7 |
Correct |
300 ms |
83904 KB |
Output is correct |
8 |
Correct |
318 ms |
85304 KB |
Output is correct |
9 |
Correct |
321 ms |
87420 KB |
Output is correct |
10 |
Correct |
316 ms |
86084 KB |
Output is correct |
11 |
Correct |
328 ms |
85564 KB |
Output is correct |
12 |
Correct |
326 ms |
87184 KB |
Output is correct |
13 |
Correct |
377 ms |
86192 KB |
Output is correct |
14 |
Correct |
311 ms |
84668 KB |
Output is correct |
15 |
Correct |
168 ms |
82364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
290 ms |
76716 KB |
Output is correct |
2 |
Correct |
287 ms |
78304 KB |
Output is correct |
3 |
Correct |
260 ms |
79800 KB |
Output is correct |
4 |
Correct |
265 ms |
76604 KB |
Output is correct |
5 |
Correct |
253 ms |
78532 KB |
Output is correct |
6 |
Correct |
266 ms |
77540 KB |
Output is correct |
7 |
Correct |
301 ms |
77516 KB |
Output is correct |
8 |
Correct |
256 ms |
77496 KB |
Output is correct |
9 |
Correct |
296 ms |
78440 KB |
Output is correct |
10 |
Correct |
196 ms |
76084 KB |
Output is correct |
11 |
Correct |
293 ms |
78068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
25820 KB |
Output is correct |
2 |
Correct |
8 ms |
26072 KB |
Output is correct |
3 |
Correct |
8 ms |
25852 KB |
Output is correct |
4 |
Correct |
8 ms |
25820 KB |
Output is correct |
5 |
Correct |
7 ms |
25824 KB |
Output is correct |
6 |
Correct |
7 ms |
25852 KB |
Output is correct |
7 |
Correct |
7 ms |
25792 KB |
Output is correct |
8 |
Correct |
8 ms |
25820 KB |
Output is correct |
9 |
Correct |
7 ms |
25852 KB |
Output is correct |
10 |
Correct |
8 ms |
25824 KB |
Output is correct |
11 |
Correct |
8 ms |
25840 KB |
Output is correct |
12 |
Correct |
8 ms |
25940 KB |
Output is correct |
13 |
Correct |
8 ms |
25908 KB |
Output is correct |
14 |
Correct |
7 ms |
25852 KB |
Output is correct |
15 |
Correct |
7 ms |
25852 KB |
Output is correct |
16 |
Correct |
8 ms |
25972 KB |
Output is correct |
17 |
Correct |
8 ms |
25820 KB |
Output is correct |
18 |
Correct |
7 ms |
25820 KB |
Output is correct |
19 |
Correct |
8 ms |
25852 KB |
Output is correct |
20 |
Correct |
7 ms |
25816 KB |
Output is correct |
21 |
Correct |
68 ms |
41424 KB |
Output is correct |
22 |
Correct |
72 ms |
40132 KB |
Output is correct |
23 |
Correct |
65 ms |
41540 KB |
Output is correct |
24 |
Correct |
71 ms |
41676 KB |
Output is correct |
25 |
Correct |
71 ms |
41924 KB |
Output is correct |
26 |
Correct |
348 ms |
88152 KB |
Output is correct |
27 |
Correct |
300 ms |
83904 KB |
Output is correct |
28 |
Correct |
318 ms |
85304 KB |
Output is correct |
29 |
Correct |
321 ms |
87420 KB |
Output is correct |
30 |
Correct |
316 ms |
86084 KB |
Output is correct |
31 |
Correct |
328 ms |
85564 KB |
Output is correct |
32 |
Correct |
326 ms |
87184 KB |
Output is correct |
33 |
Correct |
377 ms |
86192 KB |
Output is correct |
34 |
Correct |
311 ms |
84668 KB |
Output is correct |
35 |
Correct |
168 ms |
82364 KB |
Output is correct |
36 |
Correct |
290 ms |
76716 KB |
Output is correct |
37 |
Correct |
287 ms |
78304 KB |
Output is correct |
38 |
Correct |
260 ms |
79800 KB |
Output is correct |
39 |
Correct |
265 ms |
76604 KB |
Output is correct |
40 |
Correct |
253 ms |
78532 KB |
Output is correct |
41 |
Correct |
266 ms |
77540 KB |
Output is correct |
42 |
Correct |
301 ms |
77516 KB |
Output is correct |
43 |
Correct |
256 ms |
77496 KB |
Output is correct |
44 |
Correct |
296 ms |
78440 KB |
Output is correct |
45 |
Correct |
196 ms |
76084 KB |
Output is correct |
46 |
Correct |
293 ms |
78068 KB |
Output is correct |
47 |
Correct |
343 ms |
84736 KB |
Output is correct |
48 |
Correct |
328 ms |
86216 KB |
Output is correct |
49 |
Correct |
365 ms |
86208 KB |
Output is correct |
50 |
Correct |
330 ms |
85836 KB |
Output is correct |
51 |
Correct |
327 ms |
86632 KB |
Output is correct |
52 |
Correct |
372 ms |
85612 KB |
Output is correct |
53 |
Correct |
341 ms |
84280 KB |
Output is correct |
54 |
Correct |
328 ms |
85932 KB |
Output is correct |
55 |
Correct |
390 ms |
87484 KB |
Output is correct |
56 |
Correct |
225 ms |
80172 KB |
Output is correct |
57 |
Correct |
376 ms |
87004 KB |
Output is correct |