#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
#define int long long
int arr[200100];
priority_queue<pair<int, pair<int, int>>>smalltolarge[200100];
int L[200100];
int R[200100];
vector<int>treelink[200100];
int dp[200100];
int N;
int fwtree[200100];
void upd(int n,int r)
{
while (n <= N)
{
fwtree[n] += r;
n += n & -n;
}
}
int get(int n)
{
int ans = 0;
while (n)
{
ans+=fwtree[n];
n -= n & -n;
}
return ans;
}
void rupd(int s, int e, int r)
{
upd(s, r);
upd(e+1, -r);
}
int uf[200100];
int find(int n)
{
return uf[n] == n ? n : uf[n] = find(uf[n]);
}
int unio(int n, int m)
{
n = find(n);
m = find(m);
if (n == m)
return n;
if (smalltolarge[n].size() > smalltolarge[m].size())
swap(n, m);
uf[n] = m;
while (smalltolarge[n].size())
{
smalltolarge[m].push(smalltolarge[n].top());
smalltolarge[n].pop();
}
return m;
}
int treedp(int n)
{
int i;
for (i = 0; i < treelink[n].size(); i++)
{
dp[n] += treedp(treelink[n][i]);
}
for (i = 0; i < treelink[n].size(); i++)
{
int nex = treelink[n][i];
unio(n, nex);
if (nex < n)
rupd(n, R[n] - 1, dp[nex]);
else
rupd(L[n] + 1, n, dp[nex]);
}
int cov = N+5;
cov = min(cov, arr[L[n]]);
cov = min(cov, arr[R[n]]);
int curn = find(n);
while (smalltolarge[curn].size() && smalltolarge[curn].top().first >= -cov)
{
auto no = smalltolarge[curn].top();
dp[n] = max(dp[n], get(no.second.first) + no.second.second);
smalltolarge[curn].pop();
}
return dp[n];
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> N;
int i;
arr[0] = 1LL << 60;
arr[N+1] = 1LL << 60;
pair<int, int>root = { 0,0 };
for (i = 1; i <= N; i++)
{
uf[i] = i;
cin >> arr[i];
root = max(root, { arr[i],i });
}
int M;
cin >> M;
int su = 0;
for (i = 0; i < M; i++)
{
int a, b, c;
cin >> a >> b >> c;
su += c;
smalltolarge[a].push({ -b,{a,c} });
}
for (i = 1; i <= N; i++)
{
R[i] = N + 1;
}
stack<pair<int,int>>calstack;
for (i = 1; i <= N; i++)
{
int last = -1;
while (calstack.size() && calstack.top().first <= arr[i])
{
last = calstack.top().second;
R[calstack.top().second] = i;
calstack.pop();
}
if(last!=-1)
treelink[i].push_back(last);
calstack.push({ arr[i],i });
}
int last = -1;
while (calstack.size())
{
if (last != -1)
treelink[calstack.top().second].push_back(last);
calstack.pop();
}
for (i = N; i > 0; i--)
{
int last = -1;
while (calstack.size() && calstack.top().first < arr[i])
{
last = calstack.top().second;
L[calstack.top().second] = i;
calstack.pop();
}
if (last != -1)
treelink[i].push_back(last);
calstack.push({ arr[i],i });
}
cout << su - treedp(root.second);
}
Compilation message
constellation3.cpp: In function 'long long int treedp(long long int)':
constellation3.cpp:63:16: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | for (i = 0; i < treelink[n].size(); i++)
| ~~^~~~~~~~~~~~~~~~~~~~
constellation3.cpp:67:16: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | for (i = 0; i < treelink[n].size(); i++)
| ~~^~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
11340 KB |
Output is correct |
2 |
Correct |
8 ms |
11316 KB |
Output is correct |
3 |
Correct |
7 ms |
11312 KB |
Output is correct |
4 |
Correct |
8 ms |
11340 KB |
Output is correct |
5 |
Correct |
10 ms |
11340 KB |
Output is correct |
6 |
Correct |
8 ms |
11304 KB |
Output is correct |
7 |
Correct |
7 ms |
11340 KB |
Output is correct |
8 |
Correct |
7 ms |
11340 KB |
Output is correct |
9 |
Correct |
7 ms |
11340 KB |
Output is correct |
10 |
Correct |
9 ms |
11340 KB |
Output is correct |
11 |
Correct |
9 ms |
11340 KB |
Output is correct |
12 |
Correct |
7 ms |
11340 KB |
Output is correct |
13 |
Correct |
8 ms |
11368 KB |
Output is correct |
14 |
Correct |
7 ms |
11308 KB |
Output is correct |
15 |
Correct |
7 ms |
11320 KB |
Output is correct |
16 |
Correct |
9 ms |
11340 KB |
Output is correct |
17 |
Correct |
9 ms |
11280 KB |
Output is correct |
18 |
Correct |
7 ms |
11340 KB |
Output is correct |
19 |
Correct |
8 ms |
11304 KB |
Output is correct |
20 |
Correct |
10 ms |
11340 KB |
Output is correct |
21 |
Correct |
9 ms |
11340 KB |
Output is correct |
22 |
Correct |
9 ms |
11308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
11340 KB |
Output is correct |
2 |
Correct |
8 ms |
11316 KB |
Output is correct |
3 |
Correct |
7 ms |
11312 KB |
Output is correct |
4 |
Correct |
8 ms |
11340 KB |
Output is correct |
5 |
Correct |
10 ms |
11340 KB |
Output is correct |
6 |
Correct |
8 ms |
11304 KB |
Output is correct |
7 |
Correct |
7 ms |
11340 KB |
Output is correct |
8 |
Correct |
7 ms |
11340 KB |
Output is correct |
9 |
Correct |
7 ms |
11340 KB |
Output is correct |
10 |
Correct |
9 ms |
11340 KB |
Output is correct |
11 |
Correct |
9 ms |
11340 KB |
Output is correct |
12 |
Correct |
7 ms |
11340 KB |
Output is correct |
13 |
Correct |
8 ms |
11368 KB |
Output is correct |
14 |
Correct |
7 ms |
11308 KB |
Output is correct |
15 |
Correct |
7 ms |
11320 KB |
Output is correct |
16 |
Correct |
9 ms |
11340 KB |
Output is correct |
17 |
Correct |
9 ms |
11280 KB |
Output is correct |
18 |
Correct |
7 ms |
11340 KB |
Output is correct |
19 |
Correct |
8 ms |
11304 KB |
Output is correct |
20 |
Correct |
10 ms |
11340 KB |
Output is correct |
21 |
Correct |
9 ms |
11340 KB |
Output is correct |
22 |
Correct |
9 ms |
11308 KB |
Output is correct |
23 |
Correct |
10 ms |
11568 KB |
Output is correct |
24 |
Correct |
16 ms |
11484 KB |
Output is correct |
25 |
Correct |
14 ms |
11468 KB |
Output is correct |
26 |
Correct |
11 ms |
11500 KB |
Output is correct |
27 |
Correct |
11 ms |
11596 KB |
Output is correct |
28 |
Correct |
11 ms |
11512 KB |
Output is correct |
29 |
Correct |
12 ms |
11520 KB |
Output is correct |
30 |
Correct |
14 ms |
11596 KB |
Output is correct |
31 |
Correct |
10 ms |
11468 KB |
Output is correct |
32 |
Correct |
9 ms |
11724 KB |
Output is correct |
33 |
Correct |
9 ms |
11724 KB |
Output is correct |
34 |
Correct |
9 ms |
11700 KB |
Output is correct |
35 |
Correct |
9 ms |
11596 KB |
Output is correct |
36 |
Correct |
10 ms |
11804 KB |
Output is correct |
37 |
Correct |
9 ms |
11812 KB |
Output is correct |
38 |
Correct |
9 ms |
11824 KB |
Output is correct |
39 |
Correct |
9 ms |
11596 KB |
Output is correct |
40 |
Correct |
9 ms |
11732 KB |
Output is correct |
41 |
Correct |
16 ms |
11596 KB |
Output is correct |
42 |
Correct |
17 ms |
11596 KB |
Output is correct |
43 |
Correct |
9 ms |
11744 KB |
Output is correct |
44 |
Correct |
15 ms |
11528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
11340 KB |
Output is correct |
2 |
Correct |
8 ms |
11316 KB |
Output is correct |
3 |
Correct |
7 ms |
11312 KB |
Output is correct |
4 |
Correct |
8 ms |
11340 KB |
Output is correct |
5 |
Correct |
10 ms |
11340 KB |
Output is correct |
6 |
Correct |
8 ms |
11304 KB |
Output is correct |
7 |
Correct |
7 ms |
11340 KB |
Output is correct |
8 |
Correct |
7 ms |
11340 KB |
Output is correct |
9 |
Correct |
7 ms |
11340 KB |
Output is correct |
10 |
Correct |
9 ms |
11340 KB |
Output is correct |
11 |
Correct |
9 ms |
11340 KB |
Output is correct |
12 |
Correct |
7 ms |
11340 KB |
Output is correct |
13 |
Correct |
8 ms |
11368 KB |
Output is correct |
14 |
Correct |
7 ms |
11308 KB |
Output is correct |
15 |
Correct |
7 ms |
11320 KB |
Output is correct |
16 |
Correct |
9 ms |
11340 KB |
Output is correct |
17 |
Correct |
9 ms |
11280 KB |
Output is correct |
18 |
Correct |
7 ms |
11340 KB |
Output is correct |
19 |
Correct |
8 ms |
11304 KB |
Output is correct |
20 |
Correct |
10 ms |
11340 KB |
Output is correct |
21 |
Correct |
9 ms |
11340 KB |
Output is correct |
22 |
Correct |
9 ms |
11308 KB |
Output is correct |
23 |
Correct |
10 ms |
11568 KB |
Output is correct |
24 |
Correct |
16 ms |
11484 KB |
Output is correct |
25 |
Correct |
14 ms |
11468 KB |
Output is correct |
26 |
Correct |
11 ms |
11500 KB |
Output is correct |
27 |
Correct |
11 ms |
11596 KB |
Output is correct |
28 |
Correct |
11 ms |
11512 KB |
Output is correct |
29 |
Correct |
12 ms |
11520 KB |
Output is correct |
30 |
Correct |
14 ms |
11596 KB |
Output is correct |
31 |
Correct |
10 ms |
11468 KB |
Output is correct |
32 |
Correct |
9 ms |
11724 KB |
Output is correct |
33 |
Correct |
9 ms |
11724 KB |
Output is correct |
34 |
Correct |
9 ms |
11700 KB |
Output is correct |
35 |
Correct |
9 ms |
11596 KB |
Output is correct |
36 |
Correct |
10 ms |
11804 KB |
Output is correct |
37 |
Correct |
9 ms |
11812 KB |
Output is correct |
38 |
Correct |
9 ms |
11824 KB |
Output is correct |
39 |
Correct |
9 ms |
11596 KB |
Output is correct |
40 |
Correct |
9 ms |
11732 KB |
Output is correct |
41 |
Correct |
16 ms |
11596 KB |
Output is correct |
42 |
Correct |
17 ms |
11596 KB |
Output is correct |
43 |
Correct |
9 ms |
11744 KB |
Output is correct |
44 |
Correct |
15 ms |
11528 KB |
Output is correct |
45 |
Correct |
243 ms |
40552 KB |
Output is correct |
46 |
Correct |
222 ms |
40120 KB |
Output is correct |
47 |
Correct |
239 ms |
40444 KB |
Output is correct |
48 |
Correct |
222 ms |
40160 KB |
Output is correct |
49 |
Correct |
226 ms |
40116 KB |
Output is correct |
50 |
Correct |
236 ms |
39812 KB |
Output is correct |
51 |
Correct |
228 ms |
40052 KB |
Output is correct |
52 |
Correct |
235 ms |
40604 KB |
Output is correct |
53 |
Correct |
242 ms |
40132 KB |
Output is correct |
54 |
Correct |
281 ms |
55864 KB |
Output is correct |
55 |
Correct |
287 ms |
52012 KB |
Output is correct |
56 |
Correct |
359 ms |
49956 KB |
Output is correct |
57 |
Correct |
288 ms |
48856 KB |
Output is correct |
58 |
Correct |
260 ms |
54972 KB |
Output is correct |
59 |
Correct |
301 ms |
55128 KB |
Output is correct |
60 |
Correct |
170 ms |
69252 KB |
Output is correct |
61 |
Correct |
310 ms |
52584 KB |
Output is correct |
62 |
Correct |
285 ms |
59012 KB |
Output is correct |
63 |
Correct |
295 ms |
50264 KB |
Output is correct |
64 |
Correct |
279 ms |
51340 KB |
Output is correct |
65 |
Correct |
277 ms |
59336 KB |
Output is correct |
66 |
Correct |
289 ms |
51948 KB |
Output is correct |