Submission #341213

#TimeUsernameProblemLanguageResultExecution timeMemory
341213IZhO_2021_I_want_SilverBirthday gift (IZhO18_treearray)C++14
56 / 100
4061 ms37612 KiB
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <iomanip>
#include <cassert>
#include <stack>
#include <queue>
#include <deque>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>-

using namespace std;
//using namespace __gnu_pbds;

typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;

// template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
//  order_of_key (k) : Number of items strictly smaller than k .
//  find_by_order(k) : K-th element in a set (counting from zero).
#define sz(a) (int)a.size()
#define all(a) a.begin(), a.end()
#define pb push_back
#define ppb pop_back
#define mkp make_pair
#define F first
#define S second
#define lb lower_bound
#define ub upper_bound
#define show(a) cerr << #a <<" -> "<< a <<" "
#define nl cerr <<"\n"
//#define int ll

const int N = 2e5 + 5;
const int oo = 1e9 + 5;
const int LG = 18;

int n, m, q, arr[N];
vector <int> g[N];
int par[N][LG+1], tin[N], tout[N], tiktak;

void dfs(int v, int pr) {
    par[v][0] = pr;
    for (int i = 1; i <= LG; ++i) { par[v][i] = par[par[v][i-1]][i-1]; }

    tin[v] = ++tiktak;
    for (int to : g[v]) {
        if (to == pr) { continue; }
        dfs(to, v);
    }
    tout[v] = ++tiktak;
}

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

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


void solve() {
    cin >> n >> m >> q;
    for (int i = 1; i < n; ++i) {
        int a, b;
        cin >> a >> b;
        g[a].pb(b);
        g[b].pb(a);
    }
    for (int i = 1; i <= m; ++i) {
        cin >> arr[i];
    }
    dfs(1, 1);
    for (int i = 1; i <= q; ++i) {
        int type;
        cin >> type;
        if (type == 1) {
            int pos, v;
            cin >> pos >> v;
            arr[pos] = v;
        }
        else {
            int l, r, v;
            cin >> l >> r >> v;
            int cur_lca = -1, ans_l = -1;
            bool found = 0;
            for (int j = l; j <= r; ++j) {
                if (is_parent(v, arr[j])) {
                    if (cur_lca == -1) { cur_lca = arr[j]; ans_l = j; }
                    else { cur_lca = lca(cur_lca, arr[j]); }
                }
                else {
                    cur_lca = -1;
                    ans_l = -1;
                }

                if (cur_lca == v) {
                    cout << ans_l <<" "<< j <<"\n";
                    found = 1;
                    break;
                }
            }
            if (!found) { cout << -1 <<" "<< -1 <<"\n"; }
        }
    }
}

 main () {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int tests = 1;
	//cin >> tests;
	while (tests --) {
		solve();
	}
	return 0;
}
/*
    Just Chalish!
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 (stderr)

treearray.cpp:120:8: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  120 |  main () {
      |        ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...