#include <bits/stdc++.h>
#define int long long
using namespace std;
struct star {
int i, j, c;
};
struct SegTree {
struct node {
long long maxx, lazy;
int leftSon, rightSon;
};
int root, lst, rst;
vector<node> segTree;
int newNode() {
int v = segTree.size();
segTree.push_back( { 0, 0, -1, -1 } );
return v;
}
void init( int l, int r ) {
lst = l;
rst = r;
root = newNode();
}
void propag( int v, int l, int r ) {
segTree[v].maxx += segTree[v].lazy;
if ( l != r ) {
if ( segTree[v].leftSon == -1 )
segTree[v].leftSon = newNode();
segTree[segTree[v].leftSon].lazy += segTree[v].lazy;
if ( segTree[v].rightSon == -1 )
segTree[v].rightSon = newNode();
segTree[segTree[v].rightSon].lazy += segTree[v].lazy;
}
segTree[v].lazy = 0;
}
void updatePoint( int v, int l, int r, int p, long long x ) {
propag( v, l, r );
if ( l > p || r < p )
return;
segTree[v].maxx = max( segTree[v].maxx, x );
if ( l == r )
return;
int mid = (l + r) / 2;
if ( segTree[v].leftSon == -1 )
segTree[v].leftSon = newNode();
updatePoint( segTree[v].leftSon, l, mid, p, x );
if ( segTree[v].leftSon == -1 )
segTree[v].leftSon = newNode();
updatePoint( segTree[v].rightSon, mid + 1, r, p, x );
}
void updatePoint( int p, long long x ) {
updatePoint( root, lst, rst, p, x );
}
void updateInterval( int v, int l, int r, int lu, int ru, long long x ) {
propag( v, l, r );
if ( l > ru || r < lu )
return;
segTree[v].maxx = max( segTree[v].maxx, x );
if ( lu <= l && r <= ru ) {
segTree[v].lazy = x;
propag( v, l, r );
return;
}
int mid = (l + r) / 2;
if ( segTree[v].leftSon == -1 )
segTree[v].leftSon = newNode();
updateInterval( segTree[v].leftSon, l, mid, lu, ru, x );
if ( segTree[v].leftSon == -1 )
segTree[v].leftSon = newNode();
updateInterval( segTree[v].rightSon, mid + 1, r, lu, ru, x );
segTree[v].maxx = max( segTree[segTree[v].leftSon].maxx, segTree[segTree[v].rightSon].maxx );
}
void updateInterval( int l, int r, long long x ) {
updateInterval( root, lst, rst, l, r, x );
}
long long query( int v, int l, int r, int lq, int rq ) {
propag( v, l, r );
if ( l > rq || r < lq )
return 0;
if ( lq <= l && r <= rq )
return segTree[v].maxx;
int mid = (l + r) / 2;
long long a = (segTree[v].leftSon == -1 ? 0 : query( segTree[v].leftSon, l, mid, lq, rq ) );
long long b = (segTree[v].rightSon == -1 ? 0 : query( segTree[v].rightSon, mid + 1, r, lq, rq ) );
return max( a, b );
}
long long query( int r ) {
return query( root, lst, rst, lst, r );
}
};
const int MAX_N = 2e5;
const int MAX_M = 2e5;
int n;
int h[MAX_N + 2], parent[MAX_N + 2], leftSon[MAX_N + 2], rightSon[MAX_N + 2];
long long dp[2002][2002];
star stars[MAX_M];
vector<int> starsByColumn[MAX_N + 2];
set<int> heights[MAX_N + 2];
SegTree ds[MAX_N + 2];
void calcCost( int v ) {
ds[v].init( 0, n + 1 );
if ( v == 0 )
return;
int l = leftSon[v], r = rightSon[v];
calcCost( leftSon[v] );
calcCost( rightSon[v] );
if ( heights[l].size() < heights[r].size() )
swap( l, r );
vector<pair<int, long long>> upd;
heights[r].insert( h[v] );
for ( int x: heights[r] ) {
if ( x <= h[v] )
upd.push_back( { x, ds[l].query( x ) + ds[r].query( x ) } );
else
upd.push_back( { x, ds[l].query( h[v] ) + ds[r].query( x ) } );
}
swap( ds[v], ds[l] );
swap( heights[v], heights[l] );
for ( int x: heights[v] )
heights[v].insert( x );
ds[v].updateInterval( h[v] + 1, n + 1, ds[r].query( h[v] ) );
for ( auto p: upd )
ds[v].updatePoint( p.first, p.second );
for ( int i: starsByColumn[v] ) {
ds[v].updatePoint( stars[i].j, ds[v].query( h[v] ) + stars[i].c );
heights[v].insert( stars[i].j );
}
for ( int x = 0; x <= n; x++ ) {
if ( x <= h[v] )
dp[v][x] = dp[leftSon[v]][x] + dp[rightSon[v]][x];
else
dp[v][x] = max( dp[leftSon[v]][x] + dp[rightSon[v]][h[v]], dp[leftSon[v]][h[v]] + dp[rightSon[v]][x] );
}
for ( int i: starsByColumn[v] )
dp[v][stars[i].j] = max( dp[v][stars[i].j], dp[v][h[v]] + stars[i].c );
for ( int x = 1; x <= n; x++ )
dp[v][x] = max( dp[v][x], dp[v][x - 1] );
}
signed main() {
int m;
long long total = 0;
cin >> n;
for ( int i = 1; i <= n; i++ )
cin >> h[i];
cin >> m;
for ( int i = 0; i < m; i++ ) {
cin >> stars[i].i >> stars[i].j >> stars[i].c;
total += stars[i].c;
}
h[n + 1] = n + 1;
vector<int> stack;
for ( int i = 1; i <= n + 1; i++ ) {
vector<int> path;
while ( !stack.empty() && h[i] >= h[stack.back()] ) {
path.push_back( stack.back() );
stack.pop_back();
}
path.push_back( i );
stack.push_back( i );
for ( int j = 0; j < path.size() - 1; j++ )
parent[path[j]] = path[j + 1];
}
for ( int v = 1; v <= n; v++ ) {
if ( v < parent[v] )
leftSon[parent[v]] = v;
else
rightSon[parent[v]] = v;
}
for ( int i = 0; i < m; i++ )
starsByColumn[stars[i].i].push_back( i );
calcCost( n + 1 );
cout << total - dp[n + 1][n];//ds[n + 1].query( n );
return 0;
}
Compilation message
constellation3.cpp: In function 'int main()':
constellation3.cpp:199:28: 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]
199 | for ( int j = 0; j < path.size() - 1; j++ )
| ~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
38488 KB |
Output is correct |
2 |
Correct |
9 ms |
38492 KB |
Output is correct |
3 |
Correct |
10 ms |
38748 KB |
Output is correct |
4 |
Correct |
9 ms |
38492 KB |
Output is correct |
5 |
Correct |
9 ms |
38492 KB |
Output is correct |
6 |
Correct |
8 ms |
38492 KB |
Output is correct |
7 |
Correct |
9 ms |
38492 KB |
Output is correct |
8 |
Correct |
9 ms |
38748 KB |
Output is correct |
9 |
Correct |
9 ms |
38748 KB |
Output is correct |
10 |
Correct |
9 ms |
38416 KB |
Output is correct |
11 |
Correct |
10 ms |
38296 KB |
Output is correct |
12 |
Correct |
9 ms |
38236 KB |
Output is correct |
13 |
Correct |
9 ms |
38276 KB |
Output is correct |
14 |
Correct |
8 ms |
38236 KB |
Output is correct |
15 |
Correct |
9 ms |
38236 KB |
Output is correct |
16 |
Correct |
17 ms |
38092 KB |
Output is correct |
17 |
Correct |
9 ms |
37980 KB |
Output is correct |
18 |
Correct |
9 ms |
38236 KB |
Output is correct |
19 |
Correct |
8 ms |
38004 KB |
Output is correct |
20 |
Correct |
9 ms |
37980 KB |
Output is correct |
21 |
Correct |
9 ms |
38232 KB |
Output is correct |
22 |
Correct |
8 ms |
38232 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
38488 KB |
Output is correct |
2 |
Correct |
9 ms |
38492 KB |
Output is correct |
3 |
Correct |
10 ms |
38748 KB |
Output is correct |
4 |
Correct |
9 ms |
38492 KB |
Output is correct |
5 |
Correct |
9 ms |
38492 KB |
Output is correct |
6 |
Correct |
8 ms |
38492 KB |
Output is correct |
7 |
Correct |
9 ms |
38492 KB |
Output is correct |
8 |
Correct |
9 ms |
38748 KB |
Output is correct |
9 |
Correct |
9 ms |
38748 KB |
Output is correct |
10 |
Correct |
9 ms |
38416 KB |
Output is correct |
11 |
Correct |
10 ms |
38296 KB |
Output is correct |
12 |
Correct |
9 ms |
38236 KB |
Output is correct |
13 |
Correct |
9 ms |
38276 KB |
Output is correct |
14 |
Correct |
8 ms |
38236 KB |
Output is correct |
15 |
Correct |
9 ms |
38236 KB |
Output is correct |
16 |
Correct |
17 ms |
38092 KB |
Output is correct |
17 |
Correct |
9 ms |
37980 KB |
Output is correct |
18 |
Correct |
9 ms |
38236 KB |
Output is correct |
19 |
Correct |
8 ms |
38004 KB |
Output is correct |
20 |
Correct |
9 ms |
37980 KB |
Output is correct |
21 |
Correct |
9 ms |
38232 KB |
Output is correct |
22 |
Correct |
8 ms |
38232 KB |
Output is correct |
23 |
Correct |
40 ms |
72032 KB |
Output is correct |
24 |
Correct |
31 ms |
72020 KB |
Output is correct |
25 |
Correct |
31 ms |
72016 KB |
Output is correct |
26 |
Correct |
31 ms |
72360 KB |
Output is correct |
27 |
Correct |
31 ms |
72020 KB |
Output is correct |
28 |
Correct |
34 ms |
72020 KB |
Output is correct |
29 |
Correct |
31 ms |
72276 KB |
Output is correct |
30 |
Correct |
32 ms |
72744 KB |
Output is correct |
31 |
Correct |
31 ms |
72016 KB |
Output is correct |
32 |
Correct |
42 ms |
67668 KB |
Output is correct |
33 |
Correct |
40 ms |
68432 KB |
Output is correct |
34 |
Correct |
41 ms |
68180 KB |
Output is correct |
35 |
Correct |
32 ms |
68692 KB |
Output is correct |
36 |
Correct |
38 ms |
69712 KB |
Output is correct |
37 |
Correct |
35 ms |
67132 KB |
Output is correct |
38 |
Correct |
583 ms |
140700 KB |
Output is correct |
39 |
Correct |
28 ms |
66388 KB |
Output is correct |
40 |
Correct |
42 ms |
67068 KB |
Output is correct |
41 |
Correct |
25 ms |
66436 KB |
Output is correct |
42 |
Correct |
30 ms |
66908 KB |
Output is correct |
43 |
Correct |
46 ms |
67032 KB |
Output is correct |
44 |
Correct |
29 ms |
66564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
38488 KB |
Output is correct |
2 |
Correct |
9 ms |
38492 KB |
Output is correct |
3 |
Correct |
10 ms |
38748 KB |
Output is correct |
4 |
Correct |
9 ms |
38492 KB |
Output is correct |
5 |
Correct |
9 ms |
38492 KB |
Output is correct |
6 |
Correct |
8 ms |
38492 KB |
Output is correct |
7 |
Correct |
9 ms |
38492 KB |
Output is correct |
8 |
Correct |
9 ms |
38748 KB |
Output is correct |
9 |
Correct |
9 ms |
38748 KB |
Output is correct |
10 |
Correct |
9 ms |
38416 KB |
Output is correct |
11 |
Correct |
10 ms |
38296 KB |
Output is correct |
12 |
Correct |
9 ms |
38236 KB |
Output is correct |
13 |
Correct |
9 ms |
38276 KB |
Output is correct |
14 |
Correct |
8 ms |
38236 KB |
Output is correct |
15 |
Correct |
9 ms |
38236 KB |
Output is correct |
16 |
Correct |
17 ms |
38092 KB |
Output is correct |
17 |
Correct |
9 ms |
37980 KB |
Output is correct |
18 |
Correct |
9 ms |
38236 KB |
Output is correct |
19 |
Correct |
8 ms |
38004 KB |
Output is correct |
20 |
Correct |
9 ms |
37980 KB |
Output is correct |
21 |
Correct |
9 ms |
38232 KB |
Output is correct |
22 |
Correct |
8 ms |
38232 KB |
Output is correct |
23 |
Correct |
40 ms |
72032 KB |
Output is correct |
24 |
Correct |
31 ms |
72020 KB |
Output is correct |
25 |
Correct |
31 ms |
72016 KB |
Output is correct |
26 |
Correct |
31 ms |
72360 KB |
Output is correct |
27 |
Correct |
31 ms |
72020 KB |
Output is correct |
28 |
Correct |
34 ms |
72020 KB |
Output is correct |
29 |
Correct |
31 ms |
72276 KB |
Output is correct |
30 |
Correct |
32 ms |
72744 KB |
Output is correct |
31 |
Correct |
31 ms |
72016 KB |
Output is correct |
32 |
Correct |
42 ms |
67668 KB |
Output is correct |
33 |
Correct |
40 ms |
68432 KB |
Output is correct |
34 |
Correct |
41 ms |
68180 KB |
Output is correct |
35 |
Correct |
32 ms |
68692 KB |
Output is correct |
36 |
Correct |
38 ms |
69712 KB |
Output is correct |
37 |
Correct |
35 ms |
67132 KB |
Output is correct |
38 |
Correct |
583 ms |
140700 KB |
Output is correct |
39 |
Correct |
28 ms |
66388 KB |
Output is correct |
40 |
Correct |
42 ms |
67068 KB |
Output is correct |
41 |
Correct |
25 ms |
66436 KB |
Output is correct |
42 |
Correct |
30 ms |
66908 KB |
Output is correct |
43 |
Correct |
46 ms |
67032 KB |
Output is correct |
44 |
Correct |
29 ms |
66564 KB |
Output is correct |
45 |
Runtime error |
1012 ms |
185136 KB |
Execution killed with signal 11 |
46 |
Halted |
0 ms |
0 KB |
- |