#include <bits/stdc++.h>
#ifdef NON_SUBMIT
#define TEST(n) (n)
#define tout cerr
#else
#define TEST(n) ((void)0)
#define tout cin
#endif
using namespace std;
const int SZ=1<<18;
vector<int> adj[200002], U[200002], P;
vector<pair<int,int>> Q[200002];
int node_cnt, L[200002][18], R[200002][18], A[200002], numL[200002], numR[200002], LR[200002], RR[200002], tree[2*SZ];
long long Ltree[200003], Rtree[200003], LD[200002], RD[200002];
void set_tree(int n, int v) {for(tree[n+=SZ]=v;n>>=1;) tree[n]=max(tree[2*n],tree[2*n+1]);}
void add_tree(long long *tree, int n, long long v) {for(;n<200003;n+=n&-n) tree[n]+=v;}
void add_tree(long long *tree, int s, int e, long long v) {add_tree(tree,s,v); add_tree(tree,e+1,-v);}
void dfs(int c, int *num, int *R)
{
num[c]=++node_cnt;
for(auto n: adj[c]) dfs(n,num,R);
R[c]=node_cnt;
}
int get_lpos(int v)
{
int bit=1, s=0, e=SZ-1;
while(s<e) {
int m=(s+e)>>1;
if(tree[2*bit+1]>=v) tie(bit,s)=make_pair(2*bit+1,m+1);
else tie(bit,e)=make_pair(2*bit,m);
}
return s;
}
int get_rpos(int v)
{
int bit=1, s=0, e=SZ-1;
while(s<e) {
int m=(s+e)>>1;
if(tree[2*bit]>=v) tie(bit,e)=make_pair(2*bit,m);
else tie(bit,s)=make_pair(2*bit+1,m+1);
}
return s;
}
int get_max(int s, int e)
{
int ret=-1;
for(s+=SZ,e+=SZ;s<=e;s>>=1,e>>=1) {
if(s&1) ret=max(ret,tree[s++]);
if(~e&1) ret=max(ret,tree[e--]);
}
return ret;
}
void get_mp(int n1, int n2, int v, int bit=1, int s=0, int e=SZ-1)
{
int m=(s+e)>>1;
if(n2<n1 || n2<s || e<n1 || tree[bit]<v) return;
if(s==e) {
P.push_back(m);
return;
}
get_mp(n1,n2,v,2*bit,s,m);
get_mp(n1,n2,v,2*bit+1,m+1,e);
}
long long get_sum(long long *tree, int n)
{
long long ret=0;
for(;n;n-=n&-n) ret+=tree[n];
return ret;
}
long long get_LD(int x)
{
if(R[x][0]-x<2) return LD[x];
long long temp=0; P.clear();
get_mp(x+1,R[x][0]-1,get_max(x+1,R[x][0]-1));
temp=RD[P[0]];
for(auto p: P) temp+=LD[p];
return LD[x]=min(LD[x],temp);
}
long long get_RD(int x)
{
if(x-L[x][0]<2) return RD[x];
long long temp=0; P.clear();
get_mp(L[x][0]+1,x-1,get_max(L[x][0]+1,x-1));
temp=RD[P[0]];
for(auto p: P) temp+=LD[p];
return RD[x]=min(RD[x],temp);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
TEST(freopen("input.txt","r",stdin));
TEST(freopen("output.txt","w",stdout));
TEST(freopen("debug.txt","w",stderr));
int N, M;
long long S=0;
cin>>N; A[0]=A[N+1]=0x7fffffff;
for(int i=1;i<=N;i++) {
cin>>A[i];
U[A[i]].push_back(i);
}
cin>>M;
for(int i=0;i<M;i++) {
int x, y, c;
cin>>x>>y>>c;
Q[y].emplace_back(x,c);
S+=c;
}
set_tree(0,A[0]);
for(int i=0;++i<=N;) {
adj[L[i][0]=get_lpos(A[i])].push_back(i);
set_tree(i,A[i]);
}
dfs(0,numL,LR); numL[N+1]=LR[N+1]=++node_cnt;
memset(tree,0,sizeof(tree));
set_tree(N+1,A[0]); R[N+1][0]=N+1;
for(int i=N+1;--i;) {
adj[R[i][0]=get_rpos(A[i])].push_back(i);
adj[i].clear();
set_tree(i,A[i]);
}
set_tree(0,A[0]);
node_cnt=0; dfs(N+1,numR,RR); numR[0]=RR[0]=++node_cnt;
R[0][0]=N+1; L[N+1][0]=0;
for(int j=1;j<18;j++) for(int i=N+2;--i>=0;) {
L[i][j]=L[L[i][j-1]][j-1];
R[i][j]=R[R[i][j-1]][j-1];
}
for(int y=0;++y<=N;) {
for(auto[x,c]: Q[y]) {
int l=x, r=x;
long long v;
for(int j=18;--j>=0;) {
if(A[L[l][j]]<y) l=L[l][j];
if(A[R[r][j]]<y) r=R[r][j];
}
l=L[l][0]; r=R[r][0];
v=get_sum(Ltree,numL[x])+get_sum(Rtree,numR[x])-c;
LD[l]=min(LD[l],v); RD[r]=min(RD[r],v);
}
for(auto x: U[y]) {
add_tree(Ltree,numL[x],LR[x],get_RD(x));
add_tree(Rtree,numR[x],RR[x],get_LD(x));
}
}
cout<<S+get_LD(0)<<'\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
16588 KB |
Output is correct |
2 |
Correct |
10 ms |
16632 KB |
Output is correct |
3 |
Correct |
10 ms |
16588 KB |
Output is correct |
4 |
Correct |
10 ms |
16588 KB |
Output is correct |
5 |
Correct |
10 ms |
16668 KB |
Output is correct |
6 |
Correct |
10 ms |
16588 KB |
Output is correct |
7 |
Correct |
10 ms |
16588 KB |
Output is correct |
8 |
Correct |
10 ms |
16588 KB |
Output is correct |
9 |
Correct |
10 ms |
16588 KB |
Output is correct |
10 |
Correct |
10 ms |
16672 KB |
Output is correct |
11 |
Correct |
12 ms |
16588 KB |
Output is correct |
12 |
Correct |
10 ms |
16572 KB |
Output is correct |
13 |
Correct |
10 ms |
16588 KB |
Output is correct |
14 |
Correct |
10 ms |
16588 KB |
Output is correct |
15 |
Correct |
10 ms |
16648 KB |
Output is correct |
16 |
Correct |
10 ms |
16588 KB |
Output is correct |
17 |
Correct |
10 ms |
16588 KB |
Output is correct |
18 |
Correct |
10 ms |
16588 KB |
Output is correct |
19 |
Correct |
10 ms |
16676 KB |
Output is correct |
20 |
Correct |
10 ms |
16588 KB |
Output is correct |
21 |
Correct |
10 ms |
16676 KB |
Output is correct |
22 |
Correct |
10 ms |
16588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
16588 KB |
Output is correct |
2 |
Correct |
10 ms |
16632 KB |
Output is correct |
3 |
Correct |
10 ms |
16588 KB |
Output is correct |
4 |
Correct |
10 ms |
16588 KB |
Output is correct |
5 |
Correct |
10 ms |
16668 KB |
Output is correct |
6 |
Correct |
10 ms |
16588 KB |
Output is correct |
7 |
Correct |
10 ms |
16588 KB |
Output is correct |
8 |
Correct |
10 ms |
16588 KB |
Output is correct |
9 |
Correct |
10 ms |
16588 KB |
Output is correct |
10 |
Correct |
10 ms |
16672 KB |
Output is correct |
11 |
Correct |
12 ms |
16588 KB |
Output is correct |
12 |
Correct |
10 ms |
16572 KB |
Output is correct |
13 |
Correct |
10 ms |
16588 KB |
Output is correct |
14 |
Correct |
10 ms |
16588 KB |
Output is correct |
15 |
Correct |
10 ms |
16648 KB |
Output is correct |
16 |
Correct |
10 ms |
16588 KB |
Output is correct |
17 |
Correct |
10 ms |
16588 KB |
Output is correct |
18 |
Correct |
10 ms |
16588 KB |
Output is correct |
19 |
Correct |
10 ms |
16676 KB |
Output is correct |
20 |
Correct |
10 ms |
16588 KB |
Output is correct |
21 |
Correct |
10 ms |
16676 KB |
Output is correct |
22 |
Correct |
10 ms |
16588 KB |
Output is correct |
23 |
Correct |
13 ms |
16972 KB |
Output is correct |
24 |
Correct |
14 ms |
17004 KB |
Output is correct |
25 |
Correct |
13 ms |
17020 KB |
Output is correct |
26 |
Correct |
13 ms |
16972 KB |
Output is correct |
27 |
Correct |
14 ms |
17032 KB |
Output is correct |
28 |
Correct |
13 ms |
17060 KB |
Output is correct |
29 |
Correct |
14 ms |
17068 KB |
Output is correct |
30 |
Correct |
13 ms |
16972 KB |
Output is correct |
31 |
Correct |
13 ms |
17036 KB |
Output is correct |
32 |
Correct |
13 ms |
17100 KB |
Output is correct |
33 |
Correct |
14 ms |
17100 KB |
Output is correct |
34 |
Correct |
13 ms |
17100 KB |
Output is correct |
35 |
Correct |
13 ms |
17100 KB |
Output is correct |
36 |
Correct |
12 ms |
17100 KB |
Output is correct |
37 |
Correct |
12 ms |
17100 KB |
Output is correct |
38 |
Correct |
13 ms |
17196 KB |
Output is correct |
39 |
Correct |
13 ms |
17044 KB |
Output is correct |
40 |
Correct |
13 ms |
17100 KB |
Output is correct |
41 |
Correct |
12 ms |
17024 KB |
Output is correct |
42 |
Correct |
12 ms |
17048 KB |
Output is correct |
43 |
Correct |
13 ms |
17108 KB |
Output is correct |
44 |
Correct |
12 ms |
16968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
16588 KB |
Output is correct |
2 |
Correct |
10 ms |
16632 KB |
Output is correct |
3 |
Correct |
10 ms |
16588 KB |
Output is correct |
4 |
Correct |
10 ms |
16588 KB |
Output is correct |
5 |
Correct |
10 ms |
16668 KB |
Output is correct |
6 |
Correct |
10 ms |
16588 KB |
Output is correct |
7 |
Correct |
10 ms |
16588 KB |
Output is correct |
8 |
Correct |
10 ms |
16588 KB |
Output is correct |
9 |
Correct |
10 ms |
16588 KB |
Output is correct |
10 |
Correct |
10 ms |
16672 KB |
Output is correct |
11 |
Correct |
12 ms |
16588 KB |
Output is correct |
12 |
Correct |
10 ms |
16572 KB |
Output is correct |
13 |
Correct |
10 ms |
16588 KB |
Output is correct |
14 |
Correct |
10 ms |
16588 KB |
Output is correct |
15 |
Correct |
10 ms |
16648 KB |
Output is correct |
16 |
Correct |
10 ms |
16588 KB |
Output is correct |
17 |
Correct |
10 ms |
16588 KB |
Output is correct |
18 |
Correct |
10 ms |
16588 KB |
Output is correct |
19 |
Correct |
10 ms |
16676 KB |
Output is correct |
20 |
Correct |
10 ms |
16588 KB |
Output is correct |
21 |
Correct |
10 ms |
16676 KB |
Output is correct |
22 |
Correct |
10 ms |
16588 KB |
Output is correct |
23 |
Correct |
13 ms |
16972 KB |
Output is correct |
24 |
Correct |
14 ms |
17004 KB |
Output is correct |
25 |
Correct |
13 ms |
17020 KB |
Output is correct |
26 |
Correct |
13 ms |
16972 KB |
Output is correct |
27 |
Correct |
14 ms |
17032 KB |
Output is correct |
28 |
Correct |
13 ms |
17060 KB |
Output is correct |
29 |
Correct |
14 ms |
17068 KB |
Output is correct |
30 |
Correct |
13 ms |
16972 KB |
Output is correct |
31 |
Correct |
13 ms |
17036 KB |
Output is correct |
32 |
Correct |
13 ms |
17100 KB |
Output is correct |
33 |
Correct |
14 ms |
17100 KB |
Output is correct |
34 |
Correct |
13 ms |
17100 KB |
Output is correct |
35 |
Correct |
13 ms |
17100 KB |
Output is correct |
36 |
Correct |
12 ms |
17100 KB |
Output is correct |
37 |
Correct |
12 ms |
17100 KB |
Output is correct |
38 |
Correct |
13 ms |
17196 KB |
Output is correct |
39 |
Correct |
13 ms |
17044 KB |
Output is correct |
40 |
Correct |
13 ms |
17100 KB |
Output is correct |
41 |
Correct |
12 ms |
17024 KB |
Output is correct |
42 |
Correct |
12 ms |
17048 KB |
Output is correct |
43 |
Correct |
13 ms |
17108 KB |
Output is correct |
44 |
Correct |
12 ms |
16968 KB |
Output is correct |
45 |
Correct |
636 ms |
66500 KB |
Output is correct |
46 |
Correct |
595 ms |
65860 KB |
Output is correct |
47 |
Correct |
634 ms |
65128 KB |
Output is correct |
48 |
Correct |
621 ms |
66708 KB |
Output is correct |
49 |
Correct |
622 ms |
64952 KB |
Output is correct |
50 |
Correct |
593 ms |
64964 KB |
Output is correct |
51 |
Correct |
615 ms |
65112 KB |
Output is correct |
52 |
Correct |
611 ms |
66116 KB |
Output is correct |
53 |
Correct |
610 ms |
66012 KB |
Output is correct |
54 |
Correct |
621 ms |
72164 KB |
Output is correct |
55 |
Correct |
593 ms |
69912 KB |
Output is correct |
56 |
Correct |
544 ms |
69128 KB |
Output is correct |
57 |
Correct |
543 ms |
68140 KB |
Output is correct |
58 |
Correct |
389 ms |
68292 KB |
Output is correct |
59 |
Correct |
396 ms |
67908 KB |
Output is correct |
60 |
Correct |
391 ms |
79068 KB |
Output is correct |
61 |
Correct |
471 ms |
67780 KB |
Output is correct |
62 |
Correct |
594 ms |
75716 KB |
Output is correct |
63 |
Correct |
442 ms |
66880 KB |
Output is correct |
64 |
Correct |
448 ms |
66244 KB |
Output is correct |
65 |
Correct |
545 ms |
76280 KB |
Output is correct |
66 |
Correct |
446 ms |
66456 KB |
Output is correct |