#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ii pair<int,int>
#define fi first
#define se second
const int mxn = 2e5+5;
int n,m;
ll tree[mxn*4];
int h[mxn],pre[mxn],nxt[mxn];
struct star{
int c,r;
ll val;
bool operator < (const star &rhs)const{
return r>rhs.r;
}
};
star stars[mxn];
vector<int>ind[mxn]; //keeping ind
ll query(int pos, int id, int le, int ri){
if(le==ri){
return tree[id];
}
int mid=(le+ri)>>1;
ll res = 0ll;
if(le<=pos&&pos<=ri)res+=tree[id];
if(pos<=mid)res += query(pos,id*2,le,mid);
else res+=query(pos,id*2+1,mid+1,ri);
return res;
}
void upd(int x, int y, ll v, int id, int le, int ri){
if(x>ri||le>y)return;
if(x<=le&&ri<=y){
tree[id]+=v;
return;
}
int mid=(le+ri)>>1;
upd(x,y,v,id*2,le,mid);
upd(x,y,v,id*2+1,mid+1,ri);
return;
}
int main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin>>n;
for(int i=1; i<=n; i++){
cin>>h[i];
h[i]=n-h[i];
}
ll ans = 0ll;
h[0]=h[n+1]=0;
cin>>m;
for(int i=0; i<m; i++){
int c,r; ll v;
cin>>c>>r>>v;
ans += v;
r = n-r+1;
stars[i]={c,r,v};
}
sort(stars,stars+m);
for(int i=0; i<m; i++)
ind[stars[i].c].emplace_back(i);
vector<int>h_ind;
set<ii>ss;
h_ind.push_back(n+1);
ss.insert(ii(h[n+1],n+1));
for(int i=n; i>=1; i--){
while(!h_ind.empty()&&h[h_ind.back()]>=h[i]){
ss.erase({h[h_ind.back()],h_ind.back()});
h_ind.pop_back();
}
/*
cerr<<i<<": af \n";
for(int j:h_ind)cerr<<j<<" ";
cerr<<"\n";
for(ii x:ss)cerr<<x.fi<<" "<<x.se<<"\n";
cerr<<"\n";
// */
for(int j:ind[i]){
auto it = ss.lower_bound({stars[j].r,-1});
it = prev(it);
//cerr<<j<<" "<<stars[j].r<<" "<<it->fi<<" "<<it->se<<"\n";
nxt[j]=it->se-1;
//cerr<<j<<" ... "<<nxt[j]<<"\n";
}
ss.emplace(h[i],i);
h_ind.emplace_back(i);
}
ss=set<ii>();
h_ind=vector<int>();
h_ind.push_back(0);
ss.insert(ii(h[0],0));
for(int i=1; i<=n; i++){
while(!h_ind.empty()&&h[h_ind.back()]>=h[i]){
ss.erase({h[h_ind.back()],h_ind.back()});
h_ind.pop_back();
}
for(int j:ind[i]){
auto it = ss.lower_bound({stars[j].r,-1});
it=prev(it);
pre[j]=it->se+1;
}
ss.emplace(h[i],i);
h_ind.emplace_back(i);
}
/*
for(int i=0; i<m; i++){
cerr<<stars[i].c<<" "<<stars[i].r<<" "<<stars[i].val<<" "<<pre[i]<<" "<<nxt[i]<<"\n";
}
// */
for(int i=0; i<m; i++){
ll val = stars[i].val-query(stars[i].c,1,1,n);
//cerr<<i<<" "<<stars[i].c<<": "<<val<<" "<<stars[i].val<<"\n";
if(val>0){
ans -= val;
//cerr<<"upd "<<pre[i]<<" "<<nxt[i]<<": "<<ans<<"\n";
upd(pre[i],nxt[i],val,1,1,n);
}
}
cout<<ans<<endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
3 ms |
5120 KB |
Output is correct |
3 |
Correct |
3 ms |
5120 KB |
Output is correct |
4 |
Correct |
4 ms |
5120 KB |
Output is correct |
5 |
Correct |
4 ms |
5120 KB |
Output is correct |
6 |
Correct |
4 ms |
5120 KB |
Output is correct |
7 |
Correct |
4 ms |
5120 KB |
Output is correct |
8 |
Correct |
4 ms |
5120 KB |
Output is correct |
9 |
Correct |
4 ms |
5120 KB |
Output is correct |
10 |
Correct |
4 ms |
5120 KB |
Output is correct |
11 |
Correct |
3 ms |
5120 KB |
Output is correct |
12 |
Correct |
4 ms |
5120 KB |
Output is correct |
13 |
Correct |
4 ms |
5120 KB |
Output is correct |
14 |
Correct |
4 ms |
5120 KB |
Output is correct |
15 |
Correct |
4 ms |
5120 KB |
Output is correct |
16 |
Correct |
3 ms |
5120 KB |
Output is correct |
17 |
Correct |
4 ms |
5120 KB |
Output is correct |
18 |
Correct |
4 ms |
5120 KB |
Output is correct |
19 |
Correct |
4 ms |
5120 KB |
Output is correct |
20 |
Correct |
4 ms |
5120 KB |
Output is correct |
21 |
Correct |
4 ms |
5120 KB |
Output is correct |
22 |
Correct |
4 ms |
5120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
3 ms |
5120 KB |
Output is correct |
3 |
Correct |
3 ms |
5120 KB |
Output is correct |
4 |
Correct |
4 ms |
5120 KB |
Output is correct |
5 |
Correct |
4 ms |
5120 KB |
Output is correct |
6 |
Correct |
4 ms |
5120 KB |
Output is correct |
7 |
Correct |
4 ms |
5120 KB |
Output is correct |
8 |
Correct |
4 ms |
5120 KB |
Output is correct |
9 |
Correct |
4 ms |
5120 KB |
Output is correct |
10 |
Correct |
4 ms |
5120 KB |
Output is correct |
11 |
Correct |
3 ms |
5120 KB |
Output is correct |
12 |
Correct |
4 ms |
5120 KB |
Output is correct |
13 |
Correct |
4 ms |
5120 KB |
Output is correct |
14 |
Correct |
4 ms |
5120 KB |
Output is correct |
15 |
Correct |
4 ms |
5120 KB |
Output is correct |
16 |
Correct |
3 ms |
5120 KB |
Output is correct |
17 |
Correct |
4 ms |
5120 KB |
Output is correct |
18 |
Correct |
4 ms |
5120 KB |
Output is correct |
19 |
Correct |
4 ms |
5120 KB |
Output is correct |
20 |
Correct |
4 ms |
5120 KB |
Output is correct |
21 |
Correct |
4 ms |
5120 KB |
Output is correct |
22 |
Correct |
4 ms |
5120 KB |
Output is correct |
23 |
Correct |
5 ms |
5120 KB |
Output is correct |
24 |
Correct |
5 ms |
5120 KB |
Output is correct |
25 |
Correct |
5 ms |
5248 KB |
Output is correct |
26 |
Correct |
5 ms |
5120 KB |
Output is correct |
27 |
Correct |
5 ms |
5120 KB |
Output is correct |
28 |
Correct |
5 ms |
5120 KB |
Output is correct |
29 |
Correct |
5 ms |
5248 KB |
Output is correct |
30 |
Correct |
5 ms |
5120 KB |
Output is correct |
31 |
Correct |
7 ms |
5248 KB |
Output is correct |
32 |
Correct |
5 ms |
5248 KB |
Output is correct |
33 |
Correct |
6 ms |
5248 KB |
Output is correct |
34 |
Correct |
5 ms |
5248 KB |
Output is correct |
35 |
Correct |
6 ms |
5248 KB |
Output is correct |
36 |
Correct |
5 ms |
5120 KB |
Output is correct |
37 |
Correct |
5 ms |
5120 KB |
Output is correct |
38 |
Correct |
5 ms |
5248 KB |
Output is correct |
39 |
Correct |
5 ms |
5120 KB |
Output is correct |
40 |
Correct |
6 ms |
5248 KB |
Output is correct |
41 |
Correct |
5 ms |
5248 KB |
Output is correct |
42 |
Correct |
5 ms |
5120 KB |
Output is correct |
43 |
Correct |
6 ms |
5248 KB |
Output is correct |
44 |
Correct |
5 ms |
5120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
3 ms |
5120 KB |
Output is correct |
3 |
Correct |
3 ms |
5120 KB |
Output is correct |
4 |
Correct |
4 ms |
5120 KB |
Output is correct |
5 |
Correct |
4 ms |
5120 KB |
Output is correct |
6 |
Correct |
4 ms |
5120 KB |
Output is correct |
7 |
Correct |
4 ms |
5120 KB |
Output is correct |
8 |
Correct |
4 ms |
5120 KB |
Output is correct |
9 |
Correct |
4 ms |
5120 KB |
Output is correct |
10 |
Correct |
4 ms |
5120 KB |
Output is correct |
11 |
Correct |
3 ms |
5120 KB |
Output is correct |
12 |
Correct |
4 ms |
5120 KB |
Output is correct |
13 |
Correct |
4 ms |
5120 KB |
Output is correct |
14 |
Correct |
4 ms |
5120 KB |
Output is correct |
15 |
Correct |
4 ms |
5120 KB |
Output is correct |
16 |
Correct |
3 ms |
5120 KB |
Output is correct |
17 |
Correct |
4 ms |
5120 KB |
Output is correct |
18 |
Correct |
4 ms |
5120 KB |
Output is correct |
19 |
Correct |
4 ms |
5120 KB |
Output is correct |
20 |
Correct |
4 ms |
5120 KB |
Output is correct |
21 |
Correct |
4 ms |
5120 KB |
Output is correct |
22 |
Correct |
4 ms |
5120 KB |
Output is correct |
23 |
Correct |
5 ms |
5120 KB |
Output is correct |
24 |
Correct |
5 ms |
5120 KB |
Output is correct |
25 |
Correct |
5 ms |
5248 KB |
Output is correct |
26 |
Correct |
5 ms |
5120 KB |
Output is correct |
27 |
Correct |
5 ms |
5120 KB |
Output is correct |
28 |
Correct |
5 ms |
5120 KB |
Output is correct |
29 |
Correct |
5 ms |
5248 KB |
Output is correct |
30 |
Correct |
5 ms |
5120 KB |
Output is correct |
31 |
Correct |
7 ms |
5248 KB |
Output is correct |
32 |
Correct |
5 ms |
5248 KB |
Output is correct |
33 |
Correct |
6 ms |
5248 KB |
Output is correct |
34 |
Correct |
5 ms |
5248 KB |
Output is correct |
35 |
Correct |
6 ms |
5248 KB |
Output is correct |
36 |
Correct |
5 ms |
5120 KB |
Output is correct |
37 |
Correct |
5 ms |
5120 KB |
Output is correct |
38 |
Correct |
5 ms |
5248 KB |
Output is correct |
39 |
Correct |
5 ms |
5120 KB |
Output is correct |
40 |
Correct |
6 ms |
5248 KB |
Output is correct |
41 |
Correct |
5 ms |
5248 KB |
Output is correct |
42 |
Correct |
5 ms |
5120 KB |
Output is correct |
43 |
Correct |
6 ms |
5248 KB |
Output is correct |
44 |
Correct |
5 ms |
5120 KB |
Output is correct |
45 |
Correct |
290 ms |
18552 KB |
Output is correct |
46 |
Correct |
271 ms |
18296 KB |
Output is correct |
47 |
Correct |
277 ms |
18552 KB |
Output is correct |
48 |
Correct |
271 ms |
18296 KB |
Output is correct |
49 |
Correct |
268 ms |
18296 KB |
Output is correct |
50 |
Correct |
271 ms |
18300 KB |
Output is correct |
51 |
Correct |
276 ms |
18296 KB |
Output is correct |
52 |
Correct |
274 ms |
18552 KB |
Output is correct |
53 |
Correct |
274 ms |
18480 KB |
Output is correct |
54 |
Correct |
423 ms |
21600 KB |
Output is correct |
55 |
Correct |
405 ms |
20532 KB |
Output is correct |
56 |
Correct |
393 ms |
19652 KB |
Output is correct |
57 |
Correct |
393 ms |
19252 KB |
Output is correct |
58 |
Correct |
269 ms |
17392 KB |
Output is correct |
59 |
Correct |
271 ms |
17400 KB |
Output is correct |
60 |
Correct |
218 ms |
25164 KB |
Output is correct |
61 |
Correct |
277 ms |
18488 KB |
Output is correct |
62 |
Correct |
455 ms |
22304 KB |
Output is correct |
63 |
Correct |
264 ms |
18552 KB |
Output is correct |
64 |
Correct |
261 ms |
18168 KB |
Output is correct |
65 |
Correct |
457 ms |
22120 KB |
Output is correct |
66 |
Correct |
265 ms |
18552 KB |
Output is correct |