#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
#define MAXN 200007
int n ;
int a[ MAXN ] ;
vector < pair < int , int > > at_row[ MAXN ] ;
vector < int > unblock[ MAXN ] ;
pair < int , int > range[ MAXN ] ;
int prv[ MAXN ] , rnk[ MAXN ] ;
bool emp[ MAXN ] ;
int mx[ MAXN ] ;
class Tree {
public :
ll tr[ 4 * MAXN ] , lazy[ 4 * MAXN ] ;
void push_lazy ( int where , int IL , int IR ) {
tr[ where ] += lazy[ where ] ;
if ( IL != IR ) {
lazy[ 2 * where ] += lazy[ where ] ;
lazy[ 2 * where + 1 ] += lazy[ where ] ;
}
lazy[ where ] = 0 ;
}
void init ( int where , int IL , int IR ) {
tr[ where ] = 0 ;
if ( IL == IR ) { return ; }
int mid = ( IL + IR ) / 2 ;
init ( 2 * where , IL , mid ) ;
init ( 2 * where + 1 , mid + 1 , IR ) ;
}
void update ( int where , int IL , int IR , int CURL , int CURR , ll add ) {
push_lazy ( where , IL , IR ) ;
if ( IL > IR || CURL > CURR ) { return ; }
if ( IR < CURL || CURR < IL ) { return ; }
if ( CURL <= IL && IR <= CURR ) {
lazy[ where ] += add ;
push_lazy ( where , IL , IR ) ;
return ;
}
int mid = ( IL + IR ) / 2 ;
update ( 2 * where , IL , mid , CURL , CURR , add ) ;
update ( 2 * where + 1 , mid + 1 , IR , CURL , CURR , add ) ;
tr[ where ] = max ( tr[ 2 * where ] , tr[ 2 * where + 1 ] ) ;
}
ll query ( int where , int IL , int IR , int CURL , int CURR ) {
push_lazy ( where , IL , IR ) ;
if ( IL > IR || CURL > CURR ) { return 0 ; }
if ( IR < CURL || CURR < IL ) { return 0 ; }
if ( CURL <= IL && IR <= CURR ) { return tr[ where ] ; }
int mid = ( IL + IR ) / 2 ;
return max ( query ( 2 * where , IL , mid , CURL , CURR ) , query ( 2 * where + 1 , mid + 1 , IR , CURL , CURR ) ) ;
}
};
Tree w ;
ll ans ;
int get ( int x ) {
if ( prv[ x ] == -1 ) { return x ; }
int y = get ( prv[ x ] ) ;
prv[ x ] = y ;
return y ;
}
void unite ( int x , int y ) {
int k1 = get ( x ) ;
int k2 = get ( y ) ;
if ( k1 != k2 ) {
if ( rnk[ k1 ] >= rnk[ k2 ] ) {
rnk[ k1 ] += ( rnk[ k1 ] == rnk[ k2 ] ) ;
range[ k1 ].first = min ( range[ k1 ].first , range[ k2 ].first ) ;
range[ k1 ].second = max ( range[ k1 ].second , range[ k2 ].second ) ;
prv[ k2 ] = k1 ;
}
else {
range[ k2 ].first = min ( range[ k1 ].first , range[ k2 ].first ) ;
range[ k2 ].second = max ( range[ k1 ].second , range[ k2 ].second ) ;
prv[ k1 ] = k2 ;
}
}
}
void input ( ) {
cin >> n ;
for ( int i = 1 ; i <= n ; ++ i ) {
cin >> a[ i ] ;
unblock[ a[ i ] + 1 ].push_back ( i ) ;
}
int m ; cin >> m ;
for ( int i = 1 ; i <= m ; ++ i ) {
int x , y , val ;
cin >> x >> y >> val ;
at_row[ y ].push_back ( { x , val } ) ;
ans += val ;
}
for ( int i = 1 ; i <= n ; ++ i ) {
range[ i ] = { i , i } ;
prv[ i ] = -1 , rnk[ i ] = 0 ;
}
}
void solve ( ) {
w.init ( 1 , 1 , n ) ;
for ( int i = 1 ; i <= n ; ++ i ) {
for ( auto pos : unblock[ i ] ) {
ll upd_l = 0 , upd_r = 0 ;
ll sm = 0 ;
if ( emp[ pos - 1 ] == true ) {
int root = get ( pos - 1 ) ;
upd_r = w.query ( 1 , 1 , n , range[ root ].first , range[ root ].second ) ;
sm += upd_r ;
}
if ( emp[ pos + 1 ] == true ) {
int root = get ( pos + 1 ) ;
upd_l = w.query ( 1 , 1 , n , range[ root ].first , range[ root ].second ) ;
sm += upd_l ;
}
if ( emp[ pos - 1 ] == true ) {
int root = get ( pos - 1 ) ;
w.update ( 1 , 1 , n , range[ root ].first , range[ root ].second , upd_l ) ;
unite ( pos - 1 , pos ) ;
}
if ( emp[ pos + 1 ] == true ) {
int root = get ( pos + 1 ) ;
w.update ( 1 , 1 , n , range[ root ].first , range[ root ].second , upd_r ) ;
unite ( pos , pos + 1 ) ;
}
w.update ( 1 , 1 , n , pos , pos , sm ) ;
emp[ pos ] = true ;
}
for ( auto [ x , val ] : at_row[ i ] ) {
if ( mx[ x ] < val ) {
w.update ( 1 , 1 , n , x , x , val - mx[ x ] ) ;
mx[ x ] = val ;
}
}
}
for ( int i = 1 ; i <= n ; ++ i ) {
if ( get ( i ) == i && emp[ i ] == true ) {
ans -= w.query ( 1 , 1 , n , range[ i ].first , range[ i ].second ) ;
}
}
cout << ans << "\n" ;
}
int main ( ) {
//freopen ( "dictionary.in" , "r" , stdin ) ;
ios_base :: sync_with_stdio ( false ) ;
cin.tie ( NULL ) ;
int t = 1 ;
// cin >> t ;
while ( t -- ) {
input ( ) ;
solve ( ) ;
}
return 0 ;
}
Compilation message
constellation3.cpp: In function 'void solve()':
constellation3.cpp:135:20: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
135 | for ( auto [ x , val ] : at_row[ i ] ) {
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9728 KB |
Output is correct |
5 |
Correct |
5 ms |
9768 KB |
Output is correct |
6 |
Correct |
6 ms |
9728 KB |
Output is correct |
7 |
Correct |
5 ms |
9760 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
6 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
5 ms |
9684 KB |
Output is correct |
12 |
Correct |
5 ms |
9724 KB |
Output is correct |
13 |
Correct |
5 ms |
9812 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
15 |
Correct |
5 ms |
9724 KB |
Output is correct |
16 |
Correct |
5 ms |
9684 KB |
Output is correct |
17 |
Correct |
6 ms |
9684 KB |
Output is correct |
18 |
Correct |
6 ms |
9684 KB |
Output is correct |
19 |
Correct |
5 ms |
9684 KB |
Output is correct |
20 |
Correct |
5 ms |
9684 KB |
Output is correct |
21 |
Correct |
5 ms |
9684 KB |
Output is correct |
22 |
Correct |
5 ms |
9732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9728 KB |
Output is correct |
5 |
Correct |
5 ms |
9768 KB |
Output is correct |
6 |
Correct |
6 ms |
9728 KB |
Output is correct |
7 |
Correct |
5 ms |
9760 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
6 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
5 ms |
9684 KB |
Output is correct |
12 |
Correct |
5 ms |
9724 KB |
Output is correct |
13 |
Correct |
5 ms |
9812 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
15 |
Correct |
5 ms |
9724 KB |
Output is correct |
16 |
Correct |
5 ms |
9684 KB |
Output is correct |
17 |
Correct |
6 ms |
9684 KB |
Output is correct |
18 |
Correct |
6 ms |
9684 KB |
Output is correct |
19 |
Correct |
5 ms |
9684 KB |
Output is correct |
20 |
Correct |
5 ms |
9684 KB |
Output is correct |
21 |
Correct |
5 ms |
9684 KB |
Output is correct |
22 |
Correct |
5 ms |
9732 KB |
Output is correct |
23 |
Correct |
7 ms |
9940 KB |
Output is correct |
24 |
Correct |
7 ms |
9988 KB |
Output is correct |
25 |
Correct |
8 ms |
9940 KB |
Output is correct |
26 |
Correct |
8 ms |
9940 KB |
Output is correct |
27 |
Correct |
8 ms |
9940 KB |
Output is correct |
28 |
Correct |
8 ms |
9988 KB |
Output is correct |
29 |
Correct |
8 ms |
9940 KB |
Output is correct |
30 |
Correct |
7 ms |
9940 KB |
Output is correct |
31 |
Correct |
8 ms |
9996 KB |
Output is correct |
32 |
Correct |
10 ms |
9960 KB |
Output is correct |
33 |
Correct |
8 ms |
9940 KB |
Output is correct |
34 |
Correct |
7 ms |
9916 KB |
Output is correct |
35 |
Correct |
7 ms |
9940 KB |
Output is correct |
36 |
Correct |
7 ms |
9940 KB |
Output is correct |
37 |
Correct |
8 ms |
10068 KB |
Output is correct |
38 |
Correct |
7 ms |
9940 KB |
Output is correct |
39 |
Correct |
6 ms |
9864 KB |
Output is correct |
40 |
Correct |
7 ms |
9940 KB |
Output is correct |
41 |
Correct |
8 ms |
9864 KB |
Output is correct |
42 |
Correct |
8 ms |
9940 KB |
Output is correct |
43 |
Correct |
8 ms |
9940 KB |
Output is correct |
44 |
Correct |
8 ms |
9940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9728 KB |
Output is correct |
5 |
Correct |
5 ms |
9768 KB |
Output is correct |
6 |
Correct |
6 ms |
9728 KB |
Output is correct |
7 |
Correct |
5 ms |
9760 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
6 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
5 ms |
9684 KB |
Output is correct |
12 |
Correct |
5 ms |
9724 KB |
Output is correct |
13 |
Correct |
5 ms |
9812 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
15 |
Correct |
5 ms |
9724 KB |
Output is correct |
16 |
Correct |
5 ms |
9684 KB |
Output is correct |
17 |
Correct |
6 ms |
9684 KB |
Output is correct |
18 |
Correct |
6 ms |
9684 KB |
Output is correct |
19 |
Correct |
5 ms |
9684 KB |
Output is correct |
20 |
Correct |
5 ms |
9684 KB |
Output is correct |
21 |
Correct |
5 ms |
9684 KB |
Output is correct |
22 |
Correct |
5 ms |
9732 KB |
Output is correct |
23 |
Correct |
7 ms |
9940 KB |
Output is correct |
24 |
Correct |
7 ms |
9988 KB |
Output is correct |
25 |
Correct |
8 ms |
9940 KB |
Output is correct |
26 |
Correct |
8 ms |
9940 KB |
Output is correct |
27 |
Correct |
8 ms |
9940 KB |
Output is correct |
28 |
Correct |
8 ms |
9988 KB |
Output is correct |
29 |
Correct |
8 ms |
9940 KB |
Output is correct |
30 |
Correct |
7 ms |
9940 KB |
Output is correct |
31 |
Correct |
8 ms |
9996 KB |
Output is correct |
32 |
Correct |
10 ms |
9960 KB |
Output is correct |
33 |
Correct |
8 ms |
9940 KB |
Output is correct |
34 |
Correct |
7 ms |
9916 KB |
Output is correct |
35 |
Correct |
7 ms |
9940 KB |
Output is correct |
36 |
Correct |
7 ms |
9940 KB |
Output is correct |
37 |
Correct |
8 ms |
10068 KB |
Output is correct |
38 |
Correct |
7 ms |
9940 KB |
Output is correct |
39 |
Correct |
6 ms |
9864 KB |
Output is correct |
40 |
Correct |
7 ms |
9940 KB |
Output is correct |
41 |
Correct |
8 ms |
9864 KB |
Output is correct |
42 |
Correct |
8 ms |
9940 KB |
Output is correct |
43 |
Correct |
8 ms |
9940 KB |
Output is correct |
44 |
Correct |
8 ms |
9940 KB |
Output is correct |
45 |
Correct |
451 ms |
35972 KB |
Output is correct |
46 |
Correct |
422 ms |
35836 KB |
Output is correct |
47 |
Correct |
426 ms |
36024 KB |
Output is correct |
48 |
Correct |
448 ms |
35836 KB |
Output is correct |
49 |
Correct |
422 ms |
35584 KB |
Output is correct |
50 |
Correct |
417 ms |
35500 KB |
Output is correct |
51 |
Correct |
413 ms |
35600 KB |
Output is correct |
52 |
Correct |
440 ms |
36180 KB |
Output is correct |
53 |
Correct |
418 ms |
35860 KB |
Output is correct |
54 |
Correct |
358 ms |
38280 KB |
Output is correct |
55 |
Correct |
346 ms |
38056 KB |
Output is correct |
56 |
Correct |
383 ms |
37756 KB |
Output is correct |
57 |
Correct |
338 ms |
37836 KB |
Output is correct |
58 |
Correct |
316 ms |
32724 KB |
Output is correct |
59 |
Correct |
317 ms |
32732 KB |
Output is correct |
60 |
Correct |
253 ms |
36444 KB |
Output is correct |
61 |
Correct |
312 ms |
31352 KB |
Output is correct |
62 |
Correct |
362 ms |
35332 KB |
Output is correct |
63 |
Correct |
307 ms |
31008 KB |
Output is correct |
64 |
Correct |
292 ms |
30716 KB |
Output is correct |
65 |
Correct |
375 ms |
35416 KB |
Output is correct |
66 |
Correct |
306 ms |
30728 KB |
Output is correct |