#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 =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 |
4 ms |
5120 KB |
Output is correct |
3 |
Correct |
4 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 |
5 ms |
5120 KB |
Output is correct |
11 |
Correct |
4 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 |
5 ms |
5120 KB |
Output is correct |
15 |
Correct |
4 ms |
5120 KB |
Output is correct |
16 |
Correct |
4 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 |
5 ms |
5248 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 |
4 ms |
5120 KB |
Output is correct |
3 |
Correct |
4 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 |
5 ms |
5120 KB |
Output is correct |
11 |
Correct |
4 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 |
5 ms |
5120 KB |
Output is correct |
15 |
Correct |
4 ms |
5120 KB |
Output is correct |
16 |
Correct |
4 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 |
5 ms |
5248 KB |
Output is correct |
22 |
Correct |
4 ms |
5120 KB |
Output is correct |
23 |
Correct |
5 ms |
5248 KB |
Output is correct |
24 |
Correct |
5 ms |
5248 KB |
Output is correct |
25 |
Correct |
7 ms |
5248 KB |
Output is correct |
26 |
Correct |
5 ms |
5248 KB |
Output is correct |
27 |
Correct |
6 ms |
5248 KB |
Output is correct |
28 |
Correct |
5 ms |
5248 KB |
Output is correct |
29 |
Correct |
6 ms |
5248 KB |
Output is correct |
30 |
Correct |
6 ms |
5248 KB |
Output is correct |
31 |
Correct |
5 ms |
5248 KB |
Output is correct |
32 |
Correct |
6 ms |
5248 KB |
Output is correct |
33 |
Correct |
6 ms |
5248 KB |
Output is correct |
34 |
Correct |
6 ms |
5248 KB |
Output is correct |
35 |
Correct |
6 ms |
5248 KB |
Output is correct |
36 |
Correct |
5 ms |
5248 KB |
Output is correct |
37 |
Correct |
5 ms |
5248 KB |
Output is correct |
38 |
Correct |
5 ms |
5280 KB |
Output is correct |
39 |
Correct |
5 ms |
5248 KB |
Output is correct |
40 |
Correct |
6 ms |
5248 KB |
Output is correct |
41 |
Correct |
6 ms |
5248 KB |
Output is correct |
42 |
Correct |
5 ms |
5248 KB |
Output is correct |
43 |
Correct |
6 ms |
5248 KB |
Output is correct |
44 |
Correct |
5 ms |
5248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
4 ms |
5120 KB |
Output is correct |
3 |
Correct |
4 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 |
5 ms |
5120 KB |
Output is correct |
11 |
Correct |
4 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 |
5 ms |
5120 KB |
Output is correct |
15 |
Correct |
4 ms |
5120 KB |
Output is correct |
16 |
Correct |
4 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 |
5 ms |
5248 KB |
Output is correct |
22 |
Correct |
4 ms |
5120 KB |
Output is correct |
23 |
Correct |
5 ms |
5248 KB |
Output is correct |
24 |
Correct |
5 ms |
5248 KB |
Output is correct |
25 |
Correct |
7 ms |
5248 KB |
Output is correct |
26 |
Correct |
5 ms |
5248 KB |
Output is correct |
27 |
Correct |
6 ms |
5248 KB |
Output is correct |
28 |
Correct |
5 ms |
5248 KB |
Output is correct |
29 |
Correct |
6 ms |
5248 KB |
Output is correct |
30 |
Correct |
6 ms |
5248 KB |
Output is correct |
31 |
Correct |
5 ms |
5248 KB |
Output is correct |
32 |
Correct |
6 ms |
5248 KB |
Output is correct |
33 |
Correct |
6 ms |
5248 KB |
Output is correct |
34 |
Correct |
6 ms |
5248 KB |
Output is correct |
35 |
Correct |
6 ms |
5248 KB |
Output is correct |
36 |
Correct |
5 ms |
5248 KB |
Output is correct |
37 |
Correct |
5 ms |
5248 KB |
Output is correct |
38 |
Correct |
5 ms |
5280 KB |
Output is correct |
39 |
Correct |
5 ms |
5248 KB |
Output is correct |
40 |
Correct |
6 ms |
5248 KB |
Output is correct |
41 |
Correct |
6 ms |
5248 KB |
Output is correct |
42 |
Correct |
5 ms |
5248 KB |
Output is correct |
43 |
Correct |
6 ms |
5248 KB |
Output is correct |
44 |
Correct |
5 ms |
5248 KB |
Output is correct |
45 |
Correct |
289 ms |
24056 KB |
Output is correct |
46 |
Correct |
283 ms |
23800 KB |
Output is correct |
47 |
Correct |
292 ms |
24184 KB |
Output is correct |
48 |
Correct |
285 ms |
23672 KB |
Output is correct |
49 |
Correct |
279 ms |
23928 KB |
Output is correct |
50 |
Correct |
283 ms |
23672 KB |
Output is correct |
51 |
Correct |
300 ms |
23800 KB |
Output is correct |
52 |
Correct |
293 ms |
24312 KB |
Output is correct |
53 |
Correct |
288 ms |
23800 KB |
Output is correct |
54 |
Correct |
428 ms |
27200 KB |
Output is correct |
55 |
Correct |
408 ms |
26080 KB |
Output is correct |
56 |
Correct |
391 ms |
25072 KB |
Output is correct |
57 |
Correct |
379 ms |
24876 KB |
Output is correct |
58 |
Correct |
271 ms |
22776 KB |
Output is correct |
59 |
Correct |
273 ms |
22904 KB |
Output is correct |
60 |
Correct |
228 ms |
29776 KB |
Output is correct |
61 |
Correct |
273 ms |
23672 KB |
Output is correct |
62 |
Correct |
473 ms |
27960 KB |
Output is correct |
63 |
Correct |
266 ms |
23672 KB |
Output is correct |
64 |
Correct |
284 ms |
23416 KB |
Output is correct |
65 |
Correct |
468 ms |
27624 KB |
Output is correct |
66 |
Correct |
267 ms |
23928 KB |
Output is correct |