답안 #496030

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
496030 2021-12-20T12:41:41 Z vinnipuh01 Birthday gift (IZhO18_treearray) C++17
100 / 100
1154 ms 73408 KB
#include <iostream>
#include <bits/stdc++.h>
#include <cmath>
#include <algorithm>
#include <vector>
#include <deque>
#include <set>
#include <stack>
#include <string>
#include <map>
#include <queue>

using namespace std;

const long long oo = 1000000000000000000;

long long int  sum, ans = 0, mx = 0, mn = 1000000000, num, pos;


/*
    ViHHiPuh

   (( `'-""``""-'` ))
     )-__-_.._-__-(
   / --- (o _ o) --- \
   \ .-* ( .0. ) *-. /
   _'-. ,_ '=' _, .-'_
  / `;#'#'# - #'#'#;` \
 \_)) -----'#'----- ((_/
      # --------- #
  '# ------- ------ #'
  /..-'# ------- #'-.\
  _\...-\'# -- #'/-.../_
  ((____)- '#' -(____))


    cout << fixed << setprecision(6) << x;

    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    freopen ( "sum.in", "r", stdin )
*/

int x, y, l, r, a[ 200001 ], type, par[ 200001 ][ 21 ], lg = 20;
vector <int> v[ 200001 ], vv;
int tin[ 200001 ], tout[ 200001 ], tmr, d[ 200001 ], t[ 800001 ]; 

void dfs( int u, int pr = 1 ) {
	par[ u ][ 0 ] = pr;
	vv.push_back( u );
	tin[ u ] = ++tmr;
	for ( int i = 1; i <= lg; i ++ )
		par[ u ][ i ] = par[ par[ u ][ i - 1 ] ][ i - 1 ];
	for ( auto to : v[ u ] ) {
		if ( to != pr ) {
			d[ to ] = d[ u ] + 1;
			dfs( to, u );
		}
	}
	tout[ u ] = tmr;
}

bool ok( int a, int b ) {
	return ( tin[ a ] <= tin[ b ] && tout[ a ] >= tout[ b ] );
}

int lca( int a, int b ) {
	if ( ok( a, b ) )
		return a;
	if ( ok( b, a ) )
		return b;
	for ( int i = lg; i >= 0; i -- ) {
		if ( !ok( par[ a ][ i ], b ) )
			a = par[ a ][ i ];
	}
	return par[ a ][ 0 ];
}

void build( int v, int tl, int tr ) {
	if ( tl == tr ) {
		t[ v ] = a[ tl ];
		return;
	}
	int mid = ( tl + tr ) / 2;
	build( v + v, tl, mid );
	build( v + v + 1, mid + 1, tr );
	t[ v ] = lca( t[ v + v ], t[ v + v + 1 ] );
}

void upd( int v, int tl, int tr, int pos, int x ) {
	if ( tl > pos || tr < pos )
		return;
	if ( tl == tr ) {
		t[ v ] = x;
		return;
	}
	int mid = ( tl + tr ) / 2;
	upd( v + v, tl, mid, pos, x );
	upd( v + v + 1, mid + 1, tr, pos, x );
	t[ v ] = lca( t[ v + v ], t[ v + v + 1 ] );
}

void gett( int v, int tl, int tr, int l, int r ) {
	if ( tl > r || tr < l )
		return;
	if ( tl >= l && tr <= r ) {
		if ( !num )
			num = t[ v ];
		else
			num = lca( num, t[ v ] );
		return;
	}
	int mid = ( tl + tr ) / 2;
	gett( v + v, tl, mid ,l ,r );
	gett( v + v + 1, mid + 1, tr, l, r );
}

set <pair<int, int> > st[ 200001 ];

int main () {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
	int n, m, q;
	cin >> n >> m >> q;
	for ( int i = 1; i < n; i ++ ) {
		cin >> x >> y;
		v[ x ].push_back( y );
		v[ y ].push_back( x );
	}
	for ( int i = 1; i <= m; i ++ ) {
		cin >> a[ i ];
	}
	dfs( 1 );
	int L, R;
	for ( int i = 1; i < m; i ++ ) {
		st[ lca( a[ i ], a[ i + 1 ] ) ].insert( { i, i + 1 } );
		st[ a[ i ] ].insert( { i, i } );
	}
	st[ a[ m ] ].insert( { m, m } );
	while ( q -- ) {
		cin >> type >> l >> r;
		if ( type == 1 ) {
			if ( l < m ) {
				st[ lca( a[ l ], a[ l + 1 ] ) ].erase( { l, l + 1 } );
				st[ lca( r, a[ l + 1 ] ) ].insert( { l, l + 1 } );
			}
			if ( l > 1 ) {
				st[ lca( a[ l ], a[ l - 1 ] ) ].erase( { l - 1, l } );
				st[ lca( a[ l - 1 ], r ) ].insert( { l - 1, l } );
			}
			st[ a[ l ] ].erase( { l, l } );
			a[ l ] = r;
			st[ r ].insert( { l, l } );
		}
		else {
			cin >> x;
			auto it = st[ x ].lower_bound( { l, l } );
			if ( it != st[ x ].end() && it->second <= r )
				cout << it->first << " " << it->second << "\n";
			else
				cout << "-1 -1\n";
		}
	}
}

/*

CX

1 1 3

5 4 4
1 2
3 1
3 4
5 3
4 5 2 3
2 1 3 1
1 3 5
2 3 4 5 
2 1 3 1
*/

Compilation message

treearray.cpp: In function 'int main()':
treearray.cpp:135:6: warning: unused variable 'L' [-Wunused-variable]
  135 |  int L, R;
      |      ^
treearray.cpp:135:9: warning: unused variable 'R' [-Wunused-variable]
  135 |  int L, R;
      |         ^
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14412 KB n=5
2 Correct 9 ms 14412 KB n=100
3 Correct 8 ms 14428 KB n=100
4 Correct 8 ms 14412 KB n=100
5 Correct 8 ms 14412 KB n=100
6 Correct 10 ms 14412 KB n=100
7 Correct 8 ms 14412 KB n=100
8 Correct 7 ms 14412 KB n=100
9 Correct 8 ms 14376 KB n=100
10 Correct 11 ms 14412 KB n=100
11 Correct 8 ms 14416 KB n=100
12 Correct 8 ms 14408 KB n=100
13 Correct 10 ms 14412 KB n=100
14 Correct 8 ms 14412 KB n=100
15 Correct 7 ms 14412 KB n=100
16 Correct 8 ms 14412 KB n=100
17 Correct 7 ms 14412 KB n=100
18 Correct 8 ms 14412 KB n=100
19 Correct 7 ms 14412 KB n=100
20 Correct 9 ms 14412 KB n=100
21 Correct 9 ms 14412 KB n=100
22 Correct 8 ms 14412 KB n=100
23 Correct 8 ms 14412 KB n=100
24 Correct 7 ms 14412 KB n=100
25 Correct 8 ms 14412 KB n=100
26 Correct 9 ms 14412 KB n=12
27 Correct 8 ms 14412 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14412 KB n=5
2 Correct 9 ms 14412 KB n=100
3 Correct 8 ms 14428 KB n=100
4 Correct 8 ms 14412 KB n=100
5 Correct 8 ms 14412 KB n=100
6 Correct 10 ms 14412 KB n=100
7 Correct 8 ms 14412 KB n=100
8 Correct 7 ms 14412 KB n=100
9 Correct 8 ms 14376 KB n=100
10 Correct 11 ms 14412 KB n=100
11 Correct 8 ms 14416 KB n=100
12 Correct 8 ms 14408 KB n=100
13 Correct 10 ms 14412 KB n=100
14 Correct 8 ms 14412 KB n=100
15 Correct 7 ms 14412 KB n=100
16 Correct 8 ms 14412 KB n=100
17 Correct 7 ms 14412 KB n=100
18 Correct 8 ms 14412 KB n=100
19 Correct 7 ms 14412 KB n=100
20 Correct 9 ms 14412 KB n=100
21 Correct 9 ms 14412 KB n=100
22 Correct 8 ms 14412 KB n=100
23 Correct 8 ms 14412 KB n=100
24 Correct 7 ms 14412 KB n=100
25 Correct 8 ms 14412 KB n=100
26 Correct 9 ms 14412 KB n=12
27 Correct 8 ms 14412 KB n=100
28 Correct 9 ms 14540 KB n=500
29 Correct 9 ms 14540 KB n=500
30 Correct 10 ms 14552 KB n=500
31 Correct 9 ms 14536 KB n=500
32 Correct 10 ms 14504 KB n=500
33 Correct 8 ms 14540 KB n=500
34 Correct 8 ms 14476 KB n=500
35 Correct 8 ms 14540 KB n=500
36 Correct 8 ms 14540 KB n=500
37 Correct 11 ms 14516 KB n=500
38 Correct 11 ms 14468 KB n=500
39 Correct 11 ms 14516 KB n=500
40 Correct 8 ms 14540 KB n=500
41 Correct 8 ms 14540 KB n=500
42 Correct 8 ms 14540 KB n=500
43 Correct 9 ms 14540 KB n=500
44 Correct 8 ms 14540 KB n=500
45 Correct 8 ms 14540 KB n=500
46 Correct 9 ms 14476 KB n=500
47 Correct 9 ms 14540 KB n=500
48 Correct 11 ms 14556 KB n=500
49 Correct 8 ms 14540 KB n=500
50 Correct 10 ms 14540 KB n=500
51 Correct 8 ms 14540 KB n=500
52 Correct 8 ms 14580 KB n=500
53 Correct 9 ms 14540 KB n=500
54 Correct 8 ms 14548 KB n=500
55 Correct 10 ms 14412 KB n=278
56 Correct 8 ms 14540 KB n=500
57 Correct 9 ms 14560 KB n=500
58 Correct 9 ms 14560 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14412 KB n=5
2 Correct 9 ms 14412 KB n=100
3 Correct 8 ms 14428 KB n=100
4 Correct 8 ms 14412 KB n=100
5 Correct 8 ms 14412 KB n=100
6 Correct 10 ms 14412 KB n=100
7 Correct 8 ms 14412 KB n=100
8 Correct 7 ms 14412 KB n=100
9 Correct 8 ms 14376 KB n=100
10 Correct 11 ms 14412 KB n=100
11 Correct 8 ms 14416 KB n=100
12 Correct 8 ms 14408 KB n=100
13 Correct 10 ms 14412 KB n=100
14 Correct 8 ms 14412 KB n=100
15 Correct 7 ms 14412 KB n=100
16 Correct 8 ms 14412 KB n=100
17 Correct 7 ms 14412 KB n=100
18 Correct 8 ms 14412 KB n=100
19 Correct 7 ms 14412 KB n=100
20 Correct 9 ms 14412 KB n=100
21 Correct 9 ms 14412 KB n=100
22 Correct 8 ms 14412 KB n=100
23 Correct 8 ms 14412 KB n=100
24 Correct 7 ms 14412 KB n=100
25 Correct 8 ms 14412 KB n=100
26 Correct 9 ms 14412 KB n=12
27 Correct 8 ms 14412 KB n=100
28 Correct 9 ms 14540 KB n=500
29 Correct 9 ms 14540 KB n=500
30 Correct 10 ms 14552 KB n=500
31 Correct 9 ms 14536 KB n=500
32 Correct 10 ms 14504 KB n=500
33 Correct 8 ms 14540 KB n=500
34 Correct 8 ms 14476 KB n=500
35 Correct 8 ms 14540 KB n=500
36 Correct 8 ms 14540 KB n=500
37 Correct 11 ms 14516 KB n=500
38 Correct 11 ms 14468 KB n=500
39 Correct 11 ms 14516 KB n=500
40 Correct 8 ms 14540 KB n=500
41 Correct 8 ms 14540 KB n=500
42 Correct 8 ms 14540 KB n=500
43 Correct 9 ms 14540 KB n=500
44 Correct 8 ms 14540 KB n=500
45 Correct 8 ms 14540 KB n=500
46 Correct 9 ms 14476 KB n=500
47 Correct 9 ms 14540 KB n=500
48 Correct 11 ms 14556 KB n=500
49 Correct 8 ms 14540 KB n=500
50 Correct 10 ms 14540 KB n=500
51 Correct 8 ms 14540 KB n=500
52 Correct 8 ms 14580 KB n=500
53 Correct 9 ms 14540 KB n=500
54 Correct 8 ms 14548 KB n=500
55 Correct 10 ms 14412 KB n=278
56 Correct 8 ms 14540 KB n=500
57 Correct 9 ms 14560 KB n=500
58 Correct 9 ms 14560 KB n=500
59 Correct 12 ms 14844 KB n=2000
60 Correct 11 ms 14924 KB n=2000
61 Correct 10 ms 14924 KB n=2000
62 Correct 11 ms 14928 KB n=2000
63 Correct 12 ms 14900 KB n=2000
64 Correct 11 ms 14924 KB n=2000
65 Correct 11 ms 14900 KB n=2000
66 Correct 11 ms 14924 KB n=2000
67 Correct 12 ms 14856 KB n=2000
68 Correct 11 ms 14880 KB n=2000
69 Correct 11 ms 14836 KB n=2000
70 Correct 11 ms 14796 KB n=2000
71 Correct 10 ms 14924 KB n=2000
72 Correct 10 ms 14796 KB n=2000
73 Correct 10 ms 14872 KB n=2000
74 Correct 12 ms 14876 KB n=1844
75 Correct 13 ms 14896 KB n=2000
76 Correct 12 ms 14892 KB n=2000
77 Correct 13 ms 14816 KB n=2000
78 Correct 11 ms 14796 KB n=2000
79 Correct 12 ms 14864 KB n=2000
80 Correct 11 ms 14924 KB n=2000
81 Correct 10 ms 14924 KB n=2000
82 Correct 11 ms 14812 KB n=2000
83 Correct 11 ms 14932 KB n=2000
84 Correct 12 ms 14912 KB n=2000
85 Correct 14 ms 14936 KB n=2000
86 Correct 14 ms 14924 KB n=2000
87 Correct 12 ms 14816 KB n=2000
88 Correct 10 ms 14924 KB n=2000
89 Correct 10 ms 14924 KB n=2000
90 Correct 10 ms 14924 KB n=2000
91 Correct 13 ms 14796 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14412 KB n=5
2 Correct 9 ms 14412 KB n=100
3 Correct 8 ms 14428 KB n=100
4 Correct 8 ms 14412 KB n=100
5 Correct 8 ms 14412 KB n=100
6 Correct 10 ms 14412 KB n=100
7 Correct 8 ms 14412 KB n=100
8 Correct 7 ms 14412 KB n=100
9 Correct 8 ms 14376 KB n=100
10 Correct 11 ms 14412 KB n=100
11 Correct 8 ms 14416 KB n=100
12 Correct 8 ms 14408 KB n=100
13 Correct 10 ms 14412 KB n=100
14 Correct 8 ms 14412 KB n=100
15 Correct 7 ms 14412 KB n=100
16 Correct 8 ms 14412 KB n=100
17 Correct 7 ms 14412 KB n=100
18 Correct 8 ms 14412 KB n=100
19 Correct 7 ms 14412 KB n=100
20 Correct 9 ms 14412 KB n=100
21 Correct 9 ms 14412 KB n=100
22 Correct 8 ms 14412 KB n=100
23 Correct 8 ms 14412 KB n=100
24 Correct 7 ms 14412 KB n=100
25 Correct 8 ms 14412 KB n=100
26 Correct 9 ms 14412 KB n=12
27 Correct 8 ms 14412 KB n=100
28 Correct 9 ms 14540 KB n=500
29 Correct 9 ms 14540 KB n=500
30 Correct 10 ms 14552 KB n=500
31 Correct 9 ms 14536 KB n=500
32 Correct 10 ms 14504 KB n=500
33 Correct 8 ms 14540 KB n=500
34 Correct 8 ms 14476 KB n=500
35 Correct 8 ms 14540 KB n=500
36 Correct 8 ms 14540 KB n=500
37 Correct 11 ms 14516 KB n=500
38 Correct 11 ms 14468 KB n=500
39 Correct 11 ms 14516 KB n=500
40 Correct 8 ms 14540 KB n=500
41 Correct 8 ms 14540 KB n=500
42 Correct 8 ms 14540 KB n=500
43 Correct 9 ms 14540 KB n=500
44 Correct 8 ms 14540 KB n=500
45 Correct 8 ms 14540 KB n=500
46 Correct 9 ms 14476 KB n=500
47 Correct 9 ms 14540 KB n=500
48 Correct 11 ms 14556 KB n=500
49 Correct 8 ms 14540 KB n=500
50 Correct 10 ms 14540 KB n=500
51 Correct 8 ms 14540 KB n=500
52 Correct 8 ms 14580 KB n=500
53 Correct 9 ms 14540 KB n=500
54 Correct 8 ms 14548 KB n=500
55 Correct 10 ms 14412 KB n=278
56 Correct 8 ms 14540 KB n=500
57 Correct 9 ms 14560 KB n=500
58 Correct 9 ms 14560 KB n=500
59 Correct 12 ms 14844 KB n=2000
60 Correct 11 ms 14924 KB n=2000
61 Correct 10 ms 14924 KB n=2000
62 Correct 11 ms 14928 KB n=2000
63 Correct 12 ms 14900 KB n=2000
64 Correct 11 ms 14924 KB n=2000
65 Correct 11 ms 14900 KB n=2000
66 Correct 11 ms 14924 KB n=2000
67 Correct 12 ms 14856 KB n=2000
68 Correct 11 ms 14880 KB n=2000
69 Correct 11 ms 14836 KB n=2000
70 Correct 11 ms 14796 KB n=2000
71 Correct 10 ms 14924 KB n=2000
72 Correct 10 ms 14796 KB n=2000
73 Correct 10 ms 14872 KB n=2000
74 Correct 12 ms 14876 KB n=1844
75 Correct 13 ms 14896 KB n=2000
76 Correct 12 ms 14892 KB n=2000
77 Correct 13 ms 14816 KB n=2000
78 Correct 11 ms 14796 KB n=2000
79 Correct 12 ms 14864 KB n=2000
80 Correct 11 ms 14924 KB n=2000
81 Correct 10 ms 14924 KB n=2000
82 Correct 11 ms 14812 KB n=2000
83 Correct 11 ms 14932 KB n=2000
84 Correct 12 ms 14912 KB n=2000
85 Correct 14 ms 14936 KB n=2000
86 Correct 14 ms 14924 KB n=2000
87 Correct 12 ms 14816 KB n=2000
88 Correct 10 ms 14924 KB n=2000
89 Correct 10 ms 14924 KB n=2000
90 Correct 10 ms 14924 KB n=2000
91 Correct 13 ms 14796 KB n=2000
92 Correct 825 ms 61744 KB n=200000
93 Correct 761 ms 67032 KB n=200000
94 Correct 521 ms 71584 KB n=200000
95 Correct 719 ms 61648 KB n=200000
96 Correct 716 ms 61608 KB n=200000
97 Correct 831 ms 65784 KB n=200000
98 Correct 790 ms 61792 KB n=200000
99 Correct 904 ms 61008 KB n=200000
100 Correct 828 ms 61584 KB n=200000
101 Correct 499 ms 73188 KB n=200000
102 Correct 461 ms 62576 KB n=200000
103 Correct 445 ms 62672 KB n=200000
104 Correct 465 ms 62556 KB n=200000
105 Correct 439 ms 62356 KB n=200000
106 Correct 467 ms 62284 KB n=200000
107 Correct 461 ms 62376 KB n=200000
108 Correct 870 ms 61248 KB n=200000
109 Correct 826 ms 61284 KB n=200000
110 Correct 809 ms 61340 KB n=200000
111 Correct 796 ms 61724 KB n=200000
112 Correct 565 ms 71804 KB n=200000
113 Correct 921 ms 65908 KB n=200000
114 Correct 786 ms 61612 KB n=200000
115 Correct 1154 ms 62976 KB n=200000
116 Correct 695 ms 61420 KB n=200000
117 Correct 482 ms 72360 KB n=200000
118 Correct 933 ms 64364 KB n=200000
119 Correct 697 ms 61476 KB n=200000
120 Correct 411 ms 72888 KB n=200000
121 Correct 438 ms 72928 KB n=200000
122 Correct 433 ms 73408 KB n=200000
123 Correct 425 ms 61968 KB n=200000
124 Correct 193 ms 28124 KB n=25264