#include <iostream>
#include <algorithm>
#include <vector>
#include <unordered_map>
using namespace std;
struct Star {
long long c;
int x, y;
};
const int MAXN=2e5+10;
int n, m;
unordered_map <long long, int> um;
Star s[MAXN];
int a[MAXN], sp[MAXN][20], stepen[MAXN];
int in[MAXN], out[MAXN], in_tree[MAXN], cnt_in;
long long dp[MAXN], tree[4*MAXN];
vector <int> v[MAXN], zv[MAXN];
int cnt_tree;
void sparce_table() {
for (int i=0;i<=n+1;i++) sp[i][0]=i;
for (int i=1;(1<<i)<=n+2;i++) {
for (int j=0;j+(1<<i)-1<=n+1;j++) {
int i1=sp[j][i-1];
int i2=sp[j + (1 << (i-1))][i-1];
if (a[i1]>=a[i2]) sp[j][i]=i1;
else sp[j][i]=i2;
}
}
int cur_st=0;
for (int i=1;i<=n+2;i++) {
if ((1 << (cur_st+1))<i) cur_st++;
stepen[i]=cur_st;
}
}
int find_max(int l, int r) {
if (l>r) return -1;
int st=stepen[r-l+1];
int i1, i2;
i1=sp[l][st];
i2=sp[r - (1 << st)+1][st];
if (a[i1]>=a[i2]) return i1;
return i2;
}
long long encode(long long l, long long r) {
return l*((long long)n+2ll)+r;
}
void find_tree(int idx, int l, int r) {
if (l+1>=r) return;
um[encode(l,r)]=idx;
cnt_in++;
in[idx]=cnt_in;
int cur_ind=find_max(l+1,r-1);
int last=l, kopie=cur_ind;
while (cur_ind!=-1 && a[cur_ind]==a[kopie]) {
in_tree[cur_ind]=idx;
if (last+1<cur_ind) {
cnt_tree++;
v[idx].push_back(cnt_tree);
find_tree(cnt_tree,last,cur_ind);
}
last=cur_ind;
cur_ind=find_max(cur_ind+1,r-1);
}
if (last+1<r) {
cnt_tree++;
v[idx].push_back(cnt_tree);
find_tree(cnt_tree,last,r);
}
out[idx]=cnt_in;
}
void update(int node, int l, int r, int ql, int qr, long long ch) {
if (ql>r || qr<l) return;
if (l>=ql && r<=qr) {
tree[node]+=ch;
return;
}
int mid=(l+r)/2;
update(node*2,l,mid,ql,qr,ch);
update(node*2+1,mid+1,r,ql,qr,ch);
}
long long query(int node, int l, int r, int qind) {
if (l==r) return tree[node];
int mid=(l+r)/2;
if (qind<=mid) return query(node*2,l,mid,qind)+tree[node];
return query(node*2+1,mid+1,r,qind)+tree[node];
}
long long find_dist(int col, int par) {
int x=in_tree[col];
return query(1,1,cnt_in,in[x])-query(1,1,cnt_in,in[par]);
}
void find_dp(int x) {
long long sum_ch=0;
for (auto i:v[x]) {
find_dp(i);
sum_ch+=dp[i];
}
dp[x]=sum_ch;
for (auto i:zv[x]) {
dp[x]=max(dp[x],s[i].c+sum_ch+find_dist(s[i].x,x));
}
update(1,1,cnt_in,in[x],out[x],sum_ch-dp[x]);
}
int main () {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int i=1;i<=n;i++) cin >> a[i];
a[0]=a[n+1]=n+1;
sparce_table();
find_tree(0,0,n+1);
long long sum_st=0;
cin >> m;
for (int i=0;i<m;i++) {
cin >> s[i].x >> s[i].y >> s[i].c;
sum_st+=s[i].c;
int l, r, mid, grl, grr;
l=0; r=s[i].x-1;
while (l<=r) {
mid=(l+r)/2;
if (a[find_max(mid,s[i].x-1)]<s[i].y) r=mid-1;
else l=mid+1;
}
grl=r;
l=s[i].x+1; r=n+1;
while (l<=r) {
mid=(l+r)/2;
if (a[find_max(s[i].x+1,mid)]<s[i].y) l=mid+1;
else r=mid-1;
}
grr=l;
zv[um[encode(grl,grr)]].push_back(i);
}
find_dp(0);
cout << sum_st-dp[0] << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
16976 KB |
Output is correct |
2 |
Correct |
3 ms |
16988 KB |
Output is correct |
3 |
Correct |
3 ms |
16988 KB |
Output is correct |
4 |
Correct |
3 ms |
16988 KB |
Output is correct |
5 |
Correct |
3 ms |
16988 KB |
Output is correct |
6 |
Correct |
4 ms |
16988 KB |
Output is correct |
7 |
Correct |
4 ms |
16988 KB |
Output is correct |
8 |
Correct |
3 ms |
16988 KB |
Output is correct |
9 |
Correct |
4 ms |
16988 KB |
Output is correct |
10 |
Correct |
3 ms |
17052 KB |
Output is correct |
11 |
Correct |
4 ms |
17080 KB |
Output is correct |
12 |
Correct |
5 ms |
16988 KB |
Output is correct |
13 |
Correct |
4 ms |
17048 KB |
Output is correct |
14 |
Correct |
3 ms |
17240 KB |
Output is correct |
15 |
Correct |
3 ms |
16988 KB |
Output is correct |
16 |
Correct |
3 ms |
16988 KB |
Output is correct |
17 |
Correct |
3 ms |
16988 KB |
Output is correct |
18 |
Correct |
3 ms |
16988 KB |
Output is correct |
19 |
Correct |
4 ms |
16988 KB |
Output is correct |
20 |
Correct |
3 ms |
17084 KB |
Output is correct |
21 |
Correct |
3 ms |
16988 KB |
Output is correct |
22 |
Correct |
3 ms |
16984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
16976 KB |
Output is correct |
2 |
Correct |
3 ms |
16988 KB |
Output is correct |
3 |
Correct |
3 ms |
16988 KB |
Output is correct |
4 |
Correct |
3 ms |
16988 KB |
Output is correct |
5 |
Correct |
3 ms |
16988 KB |
Output is correct |
6 |
Correct |
4 ms |
16988 KB |
Output is correct |
7 |
Correct |
4 ms |
16988 KB |
Output is correct |
8 |
Correct |
3 ms |
16988 KB |
Output is correct |
9 |
Correct |
4 ms |
16988 KB |
Output is correct |
10 |
Correct |
3 ms |
17052 KB |
Output is correct |
11 |
Correct |
4 ms |
17080 KB |
Output is correct |
12 |
Correct |
5 ms |
16988 KB |
Output is correct |
13 |
Correct |
4 ms |
17048 KB |
Output is correct |
14 |
Correct |
3 ms |
17240 KB |
Output is correct |
15 |
Correct |
3 ms |
16988 KB |
Output is correct |
16 |
Correct |
3 ms |
16988 KB |
Output is correct |
17 |
Correct |
3 ms |
16988 KB |
Output is correct |
18 |
Correct |
3 ms |
16988 KB |
Output is correct |
19 |
Correct |
4 ms |
16988 KB |
Output is correct |
20 |
Correct |
3 ms |
17084 KB |
Output is correct |
21 |
Correct |
3 ms |
16988 KB |
Output is correct |
22 |
Correct |
3 ms |
16984 KB |
Output is correct |
23 |
Correct |
5 ms |
17496 KB |
Output is correct |
24 |
Correct |
6 ms |
17244 KB |
Output is correct |
25 |
Correct |
5 ms |
17244 KB |
Output is correct |
26 |
Correct |
5 ms |
17244 KB |
Output is correct |
27 |
Correct |
6 ms |
17244 KB |
Output is correct |
28 |
Correct |
5 ms |
17244 KB |
Output is correct |
29 |
Correct |
7 ms |
17244 KB |
Output is correct |
30 |
Correct |
5 ms |
17244 KB |
Output is correct |
31 |
Correct |
7 ms |
17244 KB |
Output is correct |
32 |
Correct |
5 ms |
17244 KB |
Output is correct |
33 |
Correct |
5 ms |
17244 KB |
Output is correct |
34 |
Correct |
5 ms |
17320 KB |
Output is correct |
35 |
Correct |
9 ms |
17244 KB |
Output is correct |
36 |
Correct |
4 ms |
16988 KB |
Output is correct |
37 |
Correct |
5 ms |
17080 KB |
Output is correct |
38 |
Correct |
5 ms |
17500 KB |
Output is correct |
39 |
Correct |
5 ms |
16988 KB |
Output is correct |
40 |
Correct |
6 ms |
17324 KB |
Output is correct |
41 |
Correct |
5 ms |
16984 KB |
Output is correct |
42 |
Correct |
6 ms |
17212 KB |
Output is correct |
43 |
Correct |
5 ms |
17244 KB |
Output is correct |
44 |
Correct |
6 ms |
17080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
16976 KB |
Output is correct |
2 |
Correct |
3 ms |
16988 KB |
Output is correct |
3 |
Correct |
3 ms |
16988 KB |
Output is correct |
4 |
Correct |
3 ms |
16988 KB |
Output is correct |
5 |
Correct |
3 ms |
16988 KB |
Output is correct |
6 |
Correct |
4 ms |
16988 KB |
Output is correct |
7 |
Correct |
4 ms |
16988 KB |
Output is correct |
8 |
Correct |
3 ms |
16988 KB |
Output is correct |
9 |
Correct |
4 ms |
16988 KB |
Output is correct |
10 |
Correct |
3 ms |
17052 KB |
Output is correct |
11 |
Correct |
4 ms |
17080 KB |
Output is correct |
12 |
Correct |
5 ms |
16988 KB |
Output is correct |
13 |
Correct |
4 ms |
17048 KB |
Output is correct |
14 |
Correct |
3 ms |
17240 KB |
Output is correct |
15 |
Correct |
3 ms |
16988 KB |
Output is correct |
16 |
Correct |
3 ms |
16988 KB |
Output is correct |
17 |
Correct |
3 ms |
16988 KB |
Output is correct |
18 |
Correct |
3 ms |
16988 KB |
Output is correct |
19 |
Correct |
4 ms |
16988 KB |
Output is correct |
20 |
Correct |
3 ms |
17084 KB |
Output is correct |
21 |
Correct |
3 ms |
16988 KB |
Output is correct |
22 |
Correct |
3 ms |
16984 KB |
Output is correct |
23 |
Correct |
5 ms |
17496 KB |
Output is correct |
24 |
Correct |
6 ms |
17244 KB |
Output is correct |
25 |
Correct |
5 ms |
17244 KB |
Output is correct |
26 |
Correct |
5 ms |
17244 KB |
Output is correct |
27 |
Correct |
6 ms |
17244 KB |
Output is correct |
28 |
Correct |
5 ms |
17244 KB |
Output is correct |
29 |
Correct |
7 ms |
17244 KB |
Output is correct |
30 |
Correct |
5 ms |
17244 KB |
Output is correct |
31 |
Correct |
7 ms |
17244 KB |
Output is correct |
32 |
Correct |
5 ms |
17244 KB |
Output is correct |
33 |
Correct |
5 ms |
17244 KB |
Output is correct |
34 |
Correct |
5 ms |
17320 KB |
Output is correct |
35 |
Correct |
9 ms |
17244 KB |
Output is correct |
36 |
Correct |
4 ms |
16988 KB |
Output is correct |
37 |
Correct |
5 ms |
17080 KB |
Output is correct |
38 |
Correct |
5 ms |
17500 KB |
Output is correct |
39 |
Correct |
5 ms |
16988 KB |
Output is correct |
40 |
Correct |
6 ms |
17324 KB |
Output is correct |
41 |
Correct |
5 ms |
16984 KB |
Output is correct |
42 |
Correct |
6 ms |
17212 KB |
Output is correct |
43 |
Correct |
5 ms |
17244 KB |
Output is correct |
44 |
Correct |
6 ms |
17080 KB |
Output is correct |
45 |
Correct |
622 ms |
61820 KB |
Output is correct |
46 |
Correct |
512 ms |
61816 KB |
Output is correct |
47 |
Correct |
563 ms |
61824 KB |
Output is correct |
48 |
Correct |
530 ms |
61580 KB |
Output is correct |
49 |
Correct |
530 ms |
60404 KB |
Output is correct |
50 |
Correct |
534 ms |
60284 KB |
Output is correct |
51 |
Correct |
523 ms |
61272 KB |
Output is correct |
52 |
Correct |
563 ms |
61976 KB |
Output is correct |
53 |
Correct |
584 ms |
61728 KB |
Output is correct |
54 |
Correct |
690 ms |
76204 KB |
Output is correct |
55 |
Correct |
660 ms |
71528 KB |
Output is correct |
56 |
Correct |
650 ms |
68976 KB |
Output is correct |
57 |
Correct |
637 ms |
67448 KB |
Output is correct |
58 |
Correct |
374 ms |
41720 KB |
Output is correct |
59 |
Correct |
343 ms |
42100 KB |
Output is correct |
60 |
Correct |
194 ms |
84120 KB |
Output is correct |
61 |
Correct |
459 ms |
54956 KB |
Output is correct |
62 |
Correct |
627 ms |
72900 KB |
Output is correct |
63 |
Correct |
390 ms |
50684 KB |
Output is correct |
64 |
Correct |
405 ms |
52208 KB |
Output is correct |
65 |
Correct |
606 ms |
72844 KB |
Output is correct |
66 |
Correct |
443 ms |
50676 KB |
Output is correct |