#include<iostream>
#include<vector>
#include<algorithm>
#include<utility>
#include<set>
using namespace std;
typedef long long ll;
struct rng{
ll val;
int l,r,x,y;
rng(ll _val,int _l,int _r,int _x,int _y){
val=_val,l=_l,r=_r,x=_x,y=_y;
}
};
bool operator< (const rng &a,const rng &b){
return a.y<b.y;
}
vector<ll> bit;
void modify(int pos,ll num,int n){
while(pos<=n){
bit[pos]+=num;
pos+=pos&-pos;
}
}
ll quer(int pos){
ll ans=0;
while(pos>0){
ans+=bit[pos];
pos-=(pos&-pos);
}
return ans;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n;
ll ans=0;
cin>>n;
vector<int> h(n+1),bases(n+1,-1);
bit.resize(n+1);
for(int i=1;i<=n;i++) cin>>h[i];
vector<vector<int>> st(19,vector<int>(n+1));
for(int i=0;i<20;i++){
for(int j=(1<<i);j<min(1<<(i+1),n+1);j++){
if(bases[j]==-1) bases[j]=i;
}
}
for(int i=1;i<=n;i++) st[0][i]=h[i];
for(int i=1;i<19;i++){
for(int j=1;j<=n;j++) st[i][j]=max(st[i-1][j],st[i-1][min(j+(1<<(i-1)),n)]);
}
auto query=[&](int l,int r){
int len=bases[r-l+1];
return max(st[len][l],st[len][r-(1<<len)+1]);
};
int m;
ll c;
cin>>m;
vector<rng> vec;
for(int i=0;i<m;i++){
int x,y;
cin>>x>>y>>c;
ans+=c;
int lh,rh,l=1,r=x;
while(l<r){
int mid=((l+r)>>1);
if(query(mid,x)>=y) l=mid+1;
else r=mid;
}
lh=l,l=x,r=n;
while(l<r){
int mid=((l+r)>>1)+1;
if(query(x,mid)>=y) r=mid-1;
else l=mid;
}
rh=l;
vec.emplace_back(c,lh,rh,x,y);
}
sort(vec.begin(),vec.end());
set<pair<int,int>> s;
vector<ll> dp(m);
int cnt=0;
auto mod=[&](int l,int r,ll num){
modify(l,num,n);
modify(r+1,-num,n);
};
for(auto &[val,l,r,x,y]:vec){
ll sum=0;
while(!s.empty()){
auto iter=s.lower_bound(make_pair(l,-1));
if(iter==s.end()||(*iter).first>r) break;
mod(vec[(*iter).second].l,vec[(*iter).second].r,-dp[(*iter).second]);
sum+=dp[(*iter).second];
s.erase(iter);
}
mod(l,r,sum);
dp[cnt]=max(quer(x)+val,sum);
cnt++;
s.insert(make_pair(x,cnt-1));
}
for(auto [x,id]:s){
ans-=dp[id];
}
//for(int i=0;i<m;i++) cout<<i<<" "<<vec[i].x<<" "<<vec[i].y<<' '<<dp[i]<<"\n";
cout<<ans<<"\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
520 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
600 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
456 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
520 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
600 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
456 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
2 ms |
604 KB |
Output is correct |
24 |
Correct |
2 ms |
604 KB |
Output is correct |
25 |
Correct |
2 ms |
600 KB |
Output is correct |
26 |
Correct |
2 ms |
600 KB |
Output is correct |
27 |
Correct |
2 ms |
604 KB |
Output is correct |
28 |
Correct |
2 ms |
604 KB |
Output is correct |
29 |
Correct |
2 ms |
724 KB |
Output is correct |
30 |
Correct |
2 ms |
604 KB |
Output is correct |
31 |
Correct |
2 ms |
604 KB |
Output is correct |
32 |
Correct |
2 ms |
604 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
2 ms |
604 KB |
Output is correct |
35 |
Correct |
2 ms |
604 KB |
Output is correct |
36 |
Correct |
2 ms |
768 KB |
Output is correct |
37 |
Correct |
2 ms |
604 KB |
Output is correct |
38 |
Correct |
1 ms |
604 KB |
Output is correct |
39 |
Correct |
2 ms |
600 KB |
Output is correct |
40 |
Correct |
1 ms |
604 KB |
Output is correct |
41 |
Correct |
2 ms |
604 KB |
Output is correct |
42 |
Correct |
2 ms |
704 KB |
Output is correct |
43 |
Correct |
2 ms |
604 KB |
Output is correct |
44 |
Correct |
2 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
520 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
600 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
456 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
2 ms |
604 KB |
Output is correct |
24 |
Correct |
2 ms |
604 KB |
Output is correct |
25 |
Correct |
2 ms |
600 KB |
Output is correct |
26 |
Correct |
2 ms |
600 KB |
Output is correct |
27 |
Correct |
2 ms |
604 KB |
Output is correct |
28 |
Correct |
2 ms |
604 KB |
Output is correct |
29 |
Correct |
2 ms |
724 KB |
Output is correct |
30 |
Correct |
2 ms |
604 KB |
Output is correct |
31 |
Correct |
2 ms |
604 KB |
Output is correct |
32 |
Correct |
2 ms |
604 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
2 ms |
604 KB |
Output is correct |
35 |
Correct |
2 ms |
604 KB |
Output is correct |
36 |
Correct |
2 ms |
768 KB |
Output is correct |
37 |
Correct |
2 ms |
604 KB |
Output is correct |
38 |
Correct |
1 ms |
604 KB |
Output is correct |
39 |
Correct |
2 ms |
600 KB |
Output is correct |
40 |
Correct |
1 ms |
604 KB |
Output is correct |
41 |
Correct |
2 ms |
604 KB |
Output is correct |
42 |
Correct |
2 ms |
704 KB |
Output is correct |
43 |
Correct |
2 ms |
604 KB |
Output is correct |
44 |
Correct |
2 ms |
604 KB |
Output is correct |
45 |
Correct |
328 ms |
30144 KB |
Output is correct |
46 |
Correct |
320 ms |
29232 KB |
Output is correct |
47 |
Correct |
293 ms |
29244 KB |
Output is correct |
48 |
Correct |
321 ms |
30364 KB |
Output is correct |
49 |
Correct |
292 ms |
29292 KB |
Output is correct |
50 |
Correct |
285 ms |
29528 KB |
Output is correct |
51 |
Correct |
298 ms |
28800 KB |
Output is correct |
52 |
Correct |
334 ms |
29752 KB |
Output is correct |
53 |
Correct |
342 ms |
29372 KB |
Output is correct |
54 |
Correct |
244 ms |
28176 KB |
Output is correct |
55 |
Correct |
244 ms |
28540 KB |
Output is correct |
56 |
Correct |
232 ms |
29272 KB |
Output is correct |
57 |
Correct |
243 ms |
28176 KB |
Output is correct |
58 |
Correct |
225 ms |
29244 KB |
Output is correct |
59 |
Correct |
224 ms |
28256 KB |
Output is correct |
60 |
Correct |
133 ms |
28596 KB |
Output is correct |
61 |
Correct |
279 ms |
28856 KB |
Output is correct |
62 |
Correct |
228 ms |
29468 KB |
Output is correct |
63 |
Correct |
261 ms |
28912 KB |
Output is correct |
64 |
Correct |
278 ms |
27508 KB |
Output is correct |
65 |
Correct |
238 ms |
29204 KB |
Output is correct |
66 |
Correct |
313 ms |
28352 KB |
Output is correct |