Submission #948090

#TimeUsernameProblemLanguageResultExecution timeMemory
948090nguyennhBirthday gift (IZhO18_treearray)C++14
100 / 100
743 ms87124 KiB
#include<bits/stdc++.h>
#define el '\n'
using namespace std ;

mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());

const int MN = 2e5 + 5;

vector<int> adj[MN];
int a[MN] , depth[MN] , up[MN][20];
set<int> pos[MN] , satis[MN];

void dfs(int u){
  for ( auto x : adj[u] ){
    if (x == up[u][0]) continue;
    depth[x] = depth[u] + 1;
    up[x][0] = u;
    for ( int i = 1 ; i < 19 ; i++ ) up[x][i] = up[up[x][i - 1]][i - 1];
    dfs(x);
  }
}

int lca(int u , int v){
  if (depth[u] != depth[v]){
    if (depth[u] < depth[v]) swap(u , v);
    int k = depth[u] - depth[v];
    for ( int i = 0 ; (1 << i) <= k ; i++ ){
      if (k >> i & 1) u = up[u][i];
    }
  }
  if (u == v) return u;
  int k = __lg(depth[u]);
  for ( int i = k ; i >= 0 ; i-- ){
    if (up[u][i] ^ up[v][i]){
      u = up[u][i];
      v = up[v][i];
    }
  }
  return up[u][0];
}

int32_t main (){
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n , m , q;
  cin >> n >> m >> q;
  for ( int i = 1 ; 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 <= m ; i++ ) cin >> a[i];
  for ( int i = 1 ; i <= m ; i++ ){
    pos[a[i]].insert(i);
    if (i < m) satis[lca(a[i] , a[i + 1])].insert(i);
  }
  while(q--){
    int type;
    cin >> type;
    if (type == 2){
      int l , r , v;
      cin >> l >> r >> v;
      auto it = pos[v].lower_bound(l);
      if (it != pos[v].end() && *it <= r){
        cout << *it << " " << *it << el;
        continue;
      }
      auto it2 = satis[v].lower_bound(l);
      if (it2 != satis[v].end() && *it2 < r){
        cout << *it2 << " " << *it2 + 1 << el;
        continue;
      }
      cout << -1 << " " << -1 << el;
    }
    else {
      int p , v;
      cin >> p >> v;
      auto it = pos[a[p]].lower_bound(p);
      if (it != pos[a[p]].end() && *it == p) pos[a[p]].erase(it);
      pos[v].insert(p);
      if (p > 1){
        int x = lca(v , a[p - 1]) , y = lca(a[p] , a[p - 1]);
        if (x != y){
          auto it1 = satis[y].lower_bound(p - 1);
          if (it1 != satis[y].end() && *it1 == p - 1) satis[y].erase(it1);
          satis[x].insert(p - 1);
        }
      }
      if (p < m){
        int x = lca(v , a[p + 1]) , y = lca(a[p] , a[p + 1]);
        if (x != y){
          auto it1 = satis[y].lower_bound(p);
          if (it1 != satis[y].end() && *it1 == p) satis[y].erase(it1);
          satis[x].insert(p);
        }
      }
      a[p] = v;
    }
  }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...