제출 #42294

#제출 시각아이디문제언어결과실행 시간메모리
42294minhtung0404Birthday gift (IZhO18_treearray)C++14
100 / 100
1549 ms113576 KiB
//https://oj.uz/problem/view/IZhO18_treearray

#include<bits/stdc++.h>
const int N = 2e5 + 5;
using namespace std;

vector <int> adj[N];
set <int> ans[2][N];

int n, m, q, a[N], p[N][20], d[N];

void dfs(int u){
    for (int i = 0; i < (int)adj[u].size(); i++){
        int v = adj[u][i];
        if (v == p[u][0]) continue;
        d[v] = d[u] + 1; p[v][0] = u;
        dfs(v);
    }
}

int lca(int u, int v){
    if (d[u] < d[v]) swap(u, v);
    for (int i = 19; i >= 0; i--) if (d[u] - (1 << i) >= d[v]) u = p[u][i];
    for (int i = 19; i >= 0; i--) if (p[u][i] != p[v][i]) u = p[u][i], v = p[v][i];
    return ((u != v) ? p[u][0] : u);
}

int main(){
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    cin >> n >> m >> q;
    for (int i = 2; i <= n; i++){
        int u, v; cin >> u >> v;
        adj[u].push_back(v); adj[v].push_back(u);
    }
    dfs(1);
    for (int i = 1; i < 20; i++) for (int j = 1; j <= n; j++) p[j][i] = p[p[j][i-1]][i-1];
    for (int i = 1; i <= m; i++) cin >> a[i];
    for (int i = 1; i <= m; i++) ans[0][a[i]].insert(i);
    for (int i = 1; i <  m; i++) ans[1][lca(a[i], a[i+1])].insert(i);
    while (q--){
        int type;
        cin >> type;
        if (type == 1){
            int pos, v;
            cin >> pos >> v;
            ans[0][a[pos]].erase(pos);
            if (pos > 1) ans[1][lca(a[pos-1], a[pos])].erase(pos-1);
            if (pos < m) ans[1][lca(a[pos], a[pos+1])].erase(pos);
            a[pos] = v;
            ans[0][a[pos]].insert(pos);
            if (pos > 1) ans[1][lca(a[pos-1], a[pos])].insert(pos-1);
            if (pos < m) ans[1][lca(a[pos], a[pos+1])].insert(pos);
        }
        else{
            int l, r, v;
            cin >> l >> r >> v;
            set <int> ::iterator it = ans[0][v].lower_bound(l);
            if (it != ans[0][v].end() && *it <= r) {
                cout << *it << " " << *it << "\n";
                continue;
            }
            it = ans[1][v].lower_bound(l);
            if (it != ans[1][v].end() && *it < r){
                cout << *it << " " << (*it)+1 << "\n";
                continue;
            }
            cout << "-1 -1\n";
        }
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...