Submission #528973

#TimeUsernameProblemLanguageResultExecution timeMemory
528973Alex_tz307Birthday gift (IZhO18_treearray)C++17
100 / 100
858 ms82136 KiB
#include <bits/stdc++.h>

using namespace std;

const int kN = 2e5;
const int kLog = 17;
int n, m, q, timer, a[1 + kN], tin[1 + kN], tout[1 + kN], depth[1 + kN], lg2[1 + kN], anc[1 + kN][1 + kLog];
vector<int> g[1 + kN];
set<int> one[1 + kN], two[1 + kN];

void precalc() {
  for (int i = 2; i <= kN; ++i) {
    lg2[i] = lg2[i / 2] + 1;
  }
}

void dfs(int u) {
  tin[u] = ++timer;
  for (int i = 1; i <= kLog; ++i) {
    anc[u][i] = anc[anc[u][i - 1]][i - 1];
    if (anc[u][i] == 0) {
      break;
    }
  }
  for (int v : g[u]) {
    if (v != anc[u][0]) {
      anc[v][0] = u;
      depth[v] = depth[u] + 1;
      dfs(v);
    }
  }
  tout[u] = timer;
}

bool isAncestor(int u, int v) {
  return tin[u] <= tin[v] && tout[v] <= tout[u];
}

int getLca(int u, int v) {
  if (depth[u] < depth[v]) {
    swap(u, v);
  }
  if (isAncestor(v, u)) {
    return v;
  }
  for (int i = lg2[depth[v]]; i >= 0; --i) {
    if (anc[v][i] && !isAncestor(anc[v][i], u)) {
      v = anc[v][i];
    }
  }
  return anc[v][0];
}

void testCase() {
  cin >> n >> m >> q;
  for (int i = 1; i < n; ++i) {
    int u, v;
    cin >> u >> v;
    g[u].emplace_back(v);
    g[v].emplace_back(u);
  }
  dfs(1);
  for (int i = 1; i <= m; ++i) {
    cin >> a[i];
    one[a[i]].emplace(i);
    if (i > 1) {
      two[getLca(a[i - 1], a[i])].emplace(i - 1);
    }
  }
  for (int i = 0; i < q; ++i) {
    char op;
    cin >> op;
    if (op == '1') {
      int pos, v;
      cin >> pos >> v;
      one[a[pos]].erase(pos);
      if (pos > 1) {
        two[getLca(a[pos - 1], a[pos])].erase(pos - 1);
      }
      if (pos < m) {
        two[getLca(a[pos], a[pos + 1])].erase(pos);
      }
      a[pos] = v;
      one[a[pos]].emplace(pos);
      if (pos > 1) {
        two[getLca(a[pos - 1], a[pos])].emplace(pos - 1);
      }
      if (pos < m) {
        two[getLca(a[pos], a[pos + 1])].emplace(pos);
      }
    } else {
      int l, r, v;
      cin >> l >> r >> v;
      auto it = one[v].lower_bound(l);
      if (it != one[v].end() && *it <= r) {
        cout << *it << ' ' << *it << '\n';
      } else {
        it = two[v].lower_bound(l);
        if (it != two[v].end() && *it < r) {
          cout << *it << ' ' << *it + 1 << '\n';
        } else {
          cout << "-1 -1\n";
        }
      }
    }
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  precalc();
  int tests = 1;
  for (int tc = 0; tc < tests; ++tc) {
    testCase();
  }
  return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...