Submission #1359826

#TimeUsernameProblemLanguageResultExecution timeMemory
1359826ayazBirthday gift (IZhO18_treearray)C++20
100 / 100
700 ms107968 KiB
#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif

#define all(x) (x).begin(), (x).end()
#define isz(x) (int)x.size()
#define int long long

const int block = 256, MAX = 2e5 + 100, mod = 1e9 + 7, LOG = 30, INF = 1e18;

int tin[MAX], tout[MAX], par[MAX][LOG], a[MAX], timer = 0;
vector<int> g[MAX];
set<array<int, 2>> lca[MAX];

void dfs(int u, int p) {
  tin[u] = ++timer;
  par[u][0] = p;
  for (int d = 1; d < LOG; d++) {
    par[u][d] = par[par[u][d - 1]][d - 1];
  }
  for (auto &v : g[u]) {
    if (v == p) continue;
    dfs(v, u);
  }
  tout[u] = timer;
}
bool is_anc(int u, int v) {
  return tin[u] <= tin[v] && tin[v] <= tout[u];
}
int getlca(int u, int v) {
  if (is_anc(u, v)) return u;
  if (is_anc(v, u)) return v;
  for (int d = LOG - 1; d >= 0; d--) {
    if (!is_anc(par[u][d], v)) {
      u = par[u][d];
    }
  }
  return par[u][0];
}
void run(int tc) {
  int n, m, q; cin >> n >> m >> q;
  for (int i = 1; i < n; i++) {
    int u, v; cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  dfs(1, 1);
  for (int i = 1; i <= m; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= m; i++) {
    lca[a[i]].insert({i, i});
    if (i + 1 <= m) {
      lca[getlca(a[i], a[i + 1])].insert({i, i + 1});
    }
  }
  while(q--) {
    int ty; cin >> ty;
    if (ty == 1) {
      int pos, val; cin >> pos >> val;
      lca[a[pos]].erase(lca[a[pos]].find(array<int, 2>{pos, pos}));
      if (pos != 1) {
        lca[getlca(a[pos], a[pos - 1])].erase(lca[getlca(a[pos], a[pos - 1])].find(array<int, 2>{pos - 1, pos}));
      }
      if (pos != m) {
        lca[getlca(a[pos], a[pos + 1])].erase(lca[getlca(a[pos], a[pos + 1])].find(array<int, 2>{pos, pos + 1}));
      }
      a[pos] = val;
      lca[a[pos]].insert({pos, pos});
      if (pos != 1) {
        lca[getlca(a[pos], a[pos - 1])].insert(array<int, 2>{pos - 1, pos});
      }
      if (pos != m) {
        lca[getlca(a[pos], a[pos + 1])].insert(array<int, 2>{pos, pos + 1});
      }
    } else {
      int l, r, v; cin >> l >> r >> v;
      array<int, 2> ans{-1, -1};
      auto it = lca[v].lower_bound(array<int, 2>{l, -INF});
      auto [x, y] = *it;
      if (it != lca[v].end() && max(x, y) <= r) {
        cout << x << ' ' << y << '\n';
      } else {
        cout << -1 << ' ' << -1 << '\n';
      }
    }
  }
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int t = 1; // cin >> t;
  for (int tc = 1; tc <= t; tc++) run(tc);
}
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...