Submission #495786

#TimeUsernameProblemLanguageResultExecution timeMemory
495786vinnipuh01Birthday gift (IZhO18_treearray)C++17
30 / 100
4049 ms5472 KiB
#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 ], t, par[ 200001 ][ 21 ], lg = 20;
vector <int> v[ 200001 ], vv;
int tin[ 200001 ], tout[ 200001 ], tmr, d[ 200001 ]; 

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 ];
}

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 );
	while ( q -- ) {
		cin >> t >> l >> r;
		if ( t == 2 ) {
			cin >> x;
			for ( int i = l; i <= r; i ++ ) {
				num = a[ i ];
				for ( int j = i; j <= r; j ++ ) {
					sum = lca( num, a[ j ] );
					if ( sum == x ) {
						ans = 1;
						cout << i << " " << j << "\n";
						break;
					}
					if ( d[ sum ] < d[ x ] )
						break;
					num = sum;
				}
				if ( ans )
					break;
			}
			if ( !ans )
				cout << "-1 -1\n";
			ans = 0;
		}
		else {
			a[ l ] = r;
		}
	}
}

/*

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
*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...