#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 2e5;
struct Data
{
int x, y; ll c;
int l, r;
};
int N, A[MAXN+10], M, S;
Data B[MAXN+10];
struct Node
{
int y, l, r, p;
vector<pll> V;
};
Node nodes[MAXN+10];
vector<int> adj[MAXN+10];
struct BIT
{
int tree[MAXN+10];
void update(int i, int v) { for(; i<=N; i+=(i&-i)) tree[i]=max(tree[i], v); }
int query(int i) { int ret=0; for(; i>0; i-=(i&-i)) ret=max(ret, tree[i]); return ret; }
}bit;
struct SEG
{
ll tree[MAXN*4+10], lazy[MAXN*4+10];
void busy(int node, int tl, int tr)
{
if(lazy[node]==0) return;
tree[node]+=lazy[node]*(tr-tl+1);
if(tl!=tr) lazy[node*2]+=lazy[node], lazy[node*2+1]+=lazy[node];
lazy[node]=0;
}
void update(int node, int tl, int tr, int l, int r, ll v)
{
busy(node, tl, tr);
if(r<tl || tr<l) return;
if(l<=tl && tr<=r)
{
lazy[node]+=v;
busy(node, tl, tr);
return;
}
int mid=tl+tr>>1;
update(node*2, tl, mid, l, r, v);
update(node*2+1, mid+1, tr, l, r, v);
tree[node]=tree[node*2]+tree[node*2+1];
}
ll query(int node, int tl, int tr, int l, int r)
{
busy(node, tl, tr);
if(r<tl || tr<l) return 0;
if(l<=tl && tr<=r) return tree[node];
int mid=tl+tr>>1;
return query(node*2, tl, mid, l, r)+query(node*2+1, mid+1, tr, l, r);
}
}seg;
ll dp[MAXN+10];
void dfs(int now)
{
int i, j;
ll val=0;
vector<int> L, R;
for(int nxt : adj[now])
{
dfs(nxt);
val+=dp[nxt];
L.push_back(nodes[nxt].l);
R.push_back(nodes[nxt].r);
}
dp[now]=val;
for(i=0; i<nodes[now].V.size(); i++)
{
ll t=0;
int x=nodes[now].V[i].first;
int p=lower_bound(R.begin(), R.end(), x)-R.begin();
if(p<adj[now].size() && L[p]<=x && x<=R[p]) t=val-dp[adj[now][p]]+seg.query(1, 1, N, x, x);
else t=val;
dp[now]=max(dp[now], t+nodes[now].V[i].second);
}
for(i=nodes[now].l, j=0; i<=nodes[now].r; i++)
{
for(; j<adj[now].size() && L[j]==i; j++) i=R[j]+1;
if(i>nodes[now].r) continue;
seg.update(1, 1, N, i, i, val);
}
for(int nxt : adj[now]) seg.update(1, 1, N, nodes[nxt].l, nodes[nxt].r, val-dp[nxt]);
}
int main()
{
int i, j;
ll sum=0;
scanf("%d", &N);
for(i=1; i<=N; i++) scanf("%d", &A[i]);
scanf("%d", &M);
for(i=1; i<=M; i++) scanf("%d%d%lld", &B[i].x, &B[i].y, &B[i].c), sum+=B[i].c;
sort(B+1, B+M+1, [&](const Data &p, const Data &q) { return p.x<q.x; });
A[0]=N+1; A[N+1]=N+1;
vector<int> ST;
ST.push_back(0);
for(i=1, j=1; i<=N; i++)
{
while(!ST.empty() && A[ST.back()]<=A[i]) ST.pop_back();
ST.push_back(i);
for(; j<=M && B[j].x==i; j++)
{
int lo=0, hi=ST.size();
while(lo+1<hi)
{
int mid=lo+hi>>1;
if(A[ST[mid]]>=B[j].y) lo=mid;
else hi=mid;
}
B[j].l=ST[lo]+1;
}
}
ST.clear();
ST.push_back(N+1);
for(i=N, j=M; i>=1; i--)
{
while(!ST.empty() && A[ST.back()]<=A[i]) ST.pop_back();
ST.push_back(i);
for(; j>=1 && B[j].x==i; j--)
{
int lo=0, hi=ST.size();
while(lo+1<hi)
{
int mid=lo+hi>>1;
if(A[ST[mid]]>=B[j].y) lo=mid;
else hi=mid;
}
B[j].r=ST[lo]-1;
}
}
ST.clear();
sort(B+1, B+M+1, [&](const Data &p, const Data &q)
{
if(p.y!=q.y) return p.y>q.y;
return p.l<q.l;
});
S++;
nodes[S].y=N+1;
nodes[S].l=1;
nodes[S].r=N;
nodes[S].p=S;
for(i=1; i<=M; i++)
{
if(!(nodes[S].y==B[i].y && nodes[S].l==B[i].l && nodes[S].r==B[i].r))
{
S++;
nodes[S].y=B[i].y;
nodes[S].l=B[i].l;
nodes[S].r=B[i].r;
nodes[S].p=S;
}
nodes[S].V.push_back({B[i].x, B[i].c});
}
sort(nodes+1, nodes+S+1, [&](const Node &p, const Node &q)
{
if(p.l!=q.l) return p.l<q.l;
if(p.r!=q.r) return p.r>q.r;
return p.p<q.p;
});
for(i=1; i<=S; i++)
{
int par=bit.query(N-nodes[i].r+1);
adj[par].push_back(nodes[i].p);
bit.update(N-nodes[i].r+1, nodes[i].p);
}
sort(nodes+1, nodes+S+1, [&](const Node &p, const Node &q) { return p.p<q.p; });
for(i=1; i<=S; i++)
{
sort(adj[i].begin(), adj[i].end(), [&](const int &p, const int &q) { return nodes[p].l<nodes[q].l; });
sort(nodes[i].V.begin(), nodes[i].V.end());
}
dfs(1);
printf("%lld\n", sum-dp[1]);
/*
for(i=1; i<=S; i++)
{
printf("%d =========\n", i);
printf("VAL : %d %d %d\n", nodes[i].y, nodes[i].l, nodes[i].r);
printf("V : "); for(auto it : nodes[i].V) printf("%lld %lld / ", it.first, it.second); printf("\n");
printf("ADJ : "); for(auto it : adj[i]) printf("%d ", it); printf("\n");
}
*/
//for(i=1; i<=M; i++) printf("%d %d %lld : %d %d\n", B[i].x, B[i].y, B[i].c, B[i].l, B[i].r);
}
Compilation message
constellation3.cpp: In member function 'void SEG::update(int, int, int, int, int, ll)':
constellation3.cpp:57:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid=tl+tr>>1;
~~^~~
constellation3.cpp: In member function 'll SEG::query(int, int, int, int, int)':
constellation3.cpp:68:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid=tl+tr>>1;
~~^~~
constellation3.cpp: In function 'void dfs(int)':
constellation3.cpp:90:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0; i<nodes[now].V.size(); i++)
~^~~~~~~~~~~~~~~~~~~~
constellation3.cpp:96:7: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(p<adj[now].size() && L[p]<=x && x<=R[p]) t=val-dp[adj[now][p]]+seg.query(1, 1, N, x, x);
~^~~~~~~~~~~~~~~~
constellation3.cpp:103:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(; j<adj[now].size() && L[j]==i; j++) i=R[j]+1;
~^~~~~~~~~~~~~~~~
constellation3.cpp: In function 'int main()':
constellation3.cpp:136:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid=lo+hi>>1;
~~^~~
constellation3.cpp:155:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid=lo+hi>>1;
~~^~~
constellation3.cpp:117:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &N);
~~~~~^~~~~~~~~~
constellation3.cpp:118:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(i=1; i<=N; i++) scanf("%d", &A[i]);
~~~~~^~~~~~~~~~~~~
constellation3.cpp:119:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &M);
~~~~~^~~~~~~~~~
constellation3.cpp:120:66: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(i=1; i<=M; i++) scanf("%d%d%lld", &B[i].x, &B[i].y, &B[i].c), sum+=B[i].c;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
12928 KB |
Output is correct |
2 |
Correct |
11 ms |
12928 KB |
Output is correct |
3 |
Correct |
11 ms |
12928 KB |
Output is correct |
4 |
Correct |
12 ms |
12952 KB |
Output is correct |
5 |
Correct |
14 ms |
12928 KB |
Output is correct |
6 |
Correct |
11 ms |
12928 KB |
Output is correct |
7 |
Correct |
11 ms |
12928 KB |
Output is correct |
8 |
Correct |
13 ms |
12928 KB |
Output is correct |
9 |
Correct |
11 ms |
12928 KB |
Output is correct |
10 |
Correct |
11 ms |
12928 KB |
Output is correct |
11 |
Correct |
11 ms |
12928 KB |
Output is correct |
12 |
Correct |
11 ms |
12928 KB |
Output is correct |
13 |
Correct |
13 ms |
12928 KB |
Output is correct |
14 |
Correct |
12 ms |
12928 KB |
Output is correct |
15 |
Correct |
11 ms |
12916 KB |
Output is correct |
16 |
Correct |
14 ms |
12928 KB |
Output is correct |
17 |
Correct |
13 ms |
12928 KB |
Output is correct |
18 |
Correct |
12 ms |
12928 KB |
Output is correct |
19 |
Correct |
11 ms |
12928 KB |
Output is correct |
20 |
Correct |
13 ms |
13056 KB |
Output is correct |
21 |
Correct |
11 ms |
12928 KB |
Output is correct |
22 |
Correct |
11 ms |
12928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
12928 KB |
Output is correct |
2 |
Correct |
11 ms |
12928 KB |
Output is correct |
3 |
Correct |
11 ms |
12928 KB |
Output is correct |
4 |
Correct |
12 ms |
12952 KB |
Output is correct |
5 |
Correct |
14 ms |
12928 KB |
Output is correct |
6 |
Correct |
11 ms |
12928 KB |
Output is correct |
7 |
Correct |
11 ms |
12928 KB |
Output is correct |
8 |
Correct |
13 ms |
12928 KB |
Output is correct |
9 |
Correct |
11 ms |
12928 KB |
Output is correct |
10 |
Correct |
11 ms |
12928 KB |
Output is correct |
11 |
Correct |
11 ms |
12928 KB |
Output is correct |
12 |
Correct |
11 ms |
12928 KB |
Output is correct |
13 |
Correct |
13 ms |
12928 KB |
Output is correct |
14 |
Correct |
12 ms |
12928 KB |
Output is correct |
15 |
Correct |
11 ms |
12916 KB |
Output is correct |
16 |
Correct |
14 ms |
12928 KB |
Output is correct |
17 |
Correct |
13 ms |
12928 KB |
Output is correct |
18 |
Correct |
12 ms |
12928 KB |
Output is correct |
19 |
Correct |
11 ms |
12928 KB |
Output is correct |
20 |
Correct |
13 ms |
13056 KB |
Output is correct |
21 |
Correct |
11 ms |
12928 KB |
Output is correct |
22 |
Correct |
11 ms |
12928 KB |
Output is correct |
23 |
Correct |
14 ms |
13184 KB |
Output is correct |
24 |
Correct |
14 ms |
13184 KB |
Output is correct |
25 |
Correct |
14 ms |
13184 KB |
Output is correct |
26 |
Correct |
14 ms |
13184 KB |
Output is correct |
27 |
Correct |
14 ms |
13184 KB |
Output is correct |
28 |
Correct |
15 ms |
13184 KB |
Output is correct |
29 |
Correct |
15 ms |
13184 KB |
Output is correct |
30 |
Correct |
15 ms |
13184 KB |
Output is correct |
31 |
Correct |
15 ms |
13164 KB |
Output is correct |
32 |
Correct |
15 ms |
13312 KB |
Output is correct |
33 |
Correct |
14 ms |
13312 KB |
Output is correct |
34 |
Correct |
14 ms |
13312 KB |
Output is correct |
35 |
Correct |
14 ms |
13312 KB |
Output is correct |
36 |
Correct |
14 ms |
13312 KB |
Output is correct |
37 |
Correct |
14 ms |
13312 KB |
Output is correct |
38 |
Correct |
12 ms |
13312 KB |
Output is correct |
39 |
Correct |
13 ms |
13056 KB |
Output is correct |
40 |
Correct |
14 ms |
13312 KB |
Output is correct |
41 |
Correct |
14 ms |
13056 KB |
Output is correct |
42 |
Correct |
13 ms |
13056 KB |
Output is correct |
43 |
Correct |
14 ms |
13184 KB |
Output is correct |
44 |
Correct |
13 ms |
13056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
12928 KB |
Output is correct |
2 |
Correct |
11 ms |
12928 KB |
Output is correct |
3 |
Correct |
11 ms |
12928 KB |
Output is correct |
4 |
Correct |
12 ms |
12952 KB |
Output is correct |
5 |
Correct |
14 ms |
12928 KB |
Output is correct |
6 |
Correct |
11 ms |
12928 KB |
Output is correct |
7 |
Correct |
11 ms |
12928 KB |
Output is correct |
8 |
Correct |
13 ms |
12928 KB |
Output is correct |
9 |
Correct |
11 ms |
12928 KB |
Output is correct |
10 |
Correct |
11 ms |
12928 KB |
Output is correct |
11 |
Correct |
11 ms |
12928 KB |
Output is correct |
12 |
Correct |
11 ms |
12928 KB |
Output is correct |
13 |
Correct |
13 ms |
12928 KB |
Output is correct |
14 |
Correct |
12 ms |
12928 KB |
Output is correct |
15 |
Correct |
11 ms |
12916 KB |
Output is correct |
16 |
Correct |
14 ms |
12928 KB |
Output is correct |
17 |
Correct |
13 ms |
12928 KB |
Output is correct |
18 |
Correct |
12 ms |
12928 KB |
Output is correct |
19 |
Correct |
11 ms |
12928 KB |
Output is correct |
20 |
Correct |
13 ms |
13056 KB |
Output is correct |
21 |
Correct |
11 ms |
12928 KB |
Output is correct |
22 |
Correct |
11 ms |
12928 KB |
Output is correct |
23 |
Correct |
14 ms |
13184 KB |
Output is correct |
24 |
Correct |
14 ms |
13184 KB |
Output is correct |
25 |
Correct |
14 ms |
13184 KB |
Output is correct |
26 |
Correct |
14 ms |
13184 KB |
Output is correct |
27 |
Correct |
14 ms |
13184 KB |
Output is correct |
28 |
Correct |
15 ms |
13184 KB |
Output is correct |
29 |
Correct |
15 ms |
13184 KB |
Output is correct |
30 |
Correct |
15 ms |
13184 KB |
Output is correct |
31 |
Correct |
15 ms |
13164 KB |
Output is correct |
32 |
Correct |
15 ms |
13312 KB |
Output is correct |
33 |
Correct |
14 ms |
13312 KB |
Output is correct |
34 |
Correct |
14 ms |
13312 KB |
Output is correct |
35 |
Correct |
14 ms |
13312 KB |
Output is correct |
36 |
Correct |
14 ms |
13312 KB |
Output is correct |
37 |
Correct |
14 ms |
13312 KB |
Output is correct |
38 |
Correct |
12 ms |
13312 KB |
Output is correct |
39 |
Correct |
13 ms |
13056 KB |
Output is correct |
40 |
Correct |
14 ms |
13312 KB |
Output is correct |
41 |
Correct |
14 ms |
13056 KB |
Output is correct |
42 |
Correct |
13 ms |
13056 KB |
Output is correct |
43 |
Correct |
14 ms |
13184 KB |
Output is correct |
44 |
Correct |
13 ms |
13056 KB |
Output is correct |
45 |
Correct |
491 ms |
40068 KB |
Output is correct |
46 |
Correct |
465 ms |
39416 KB |
Output is correct |
47 |
Correct |
493 ms |
40152 KB |
Output is correct |
48 |
Correct |
494 ms |
39544 KB |
Output is correct |
49 |
Correct |
491 ms |
39600 KB |
Output is correct |
50 |
Correct |
493 ms |
39480 KB |
Output is correct |
51 |
Correct |
474 ms |
39800 KB |
Output is correct |
52 |
Correct |
485 ms |
40056 KB |
Output is correct |
53 |
Correct |
493 ms |
39544 KB |
Output is correct |
54 |
Correct |
476 ms |
54768 KB |
Output is correct |
55 |
Correct |
460 ms |
54528 KB |
Output is correct |
56 |
Correct |
440 ms |
54004 KB |
Output is correct |
57 |
Correct |
473 ms |
53940 KB |
Output is correct |
58 |
Correct |
390 ms |
53496 KB |
Output is correct |
59 |
Correct |
412 ms |
53632 KB |
Output is correct |
60 |
Correct |
238 ms |
54636 KB |
Output is correct |
61 |
Correct |
304 ms |
32628 KB |
Output is correct |
62 |
Correct |
408 ms |
49600 KB |
Output is correct |
63 |
Correct |
311 ms |
32400 KB |
Output is correct |
64 |
Correct |
283 ms |
32244 KB |
Output is correct |
65 |
Correct |
399 ms |
49392 KB |
Output is correct |
66 |
Correct |
290 ms |
32304 KB |
Output is correct |