Submission #854556

# Submission time Handle Problem Language Result Execution time Memory
854556 2023-09-28T04:06:43 Z NeroZein Birthday gift (IZhO18_treearray) C++17
0 / 100
8 ms 38236 KB
#include "bits/stdc++.h"
using namespace std;
 
#ifdef Nero
#include "Deb.h"
#else
#define deb(...)
#endif
 
const int LOG = 20; 
const int N = 2e5 + 5; 
 
int a[N]; 
vector<int> g[N]; 
int dep[N], up[LOG][N]; 
set<int> lco[N], no[N]; 
 
void dfs(int v, int p) {
  for (int u : g[v]) {
    if (u == p) {
      continue;
    }
    dep[u] = dep[v] + 1; 
    up[0][u] = v;
    for (int j = 1; j < LOG; ++j) {
      up[j][u] = up[j - 1][up[j - 1][u]]; 
    }
    dfs(u, v); 
  }
}
 
int lca(int u, int v) {
  if (dep[u] < dep[v]) swap(u, v);
  for (int j = 0; j < LOG; ++j) {
    if ((dep[u] - dep[v]) >> j & 1) {
      u = up[j][u];
    }
  }
  if (u == v) return v;
  for (int j = LOG - 1; j >= 0; --j) {
    if (up[j][u] != up[j][v]) {
      u = up[j][u], v = up[j][v]; 
    }
  }
  return up[0][v];
}
 
int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, m, q;
  cin >> n >> m >> q;
  for(int i = 0; i < n - 1; ++i) {
    int u, v;
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  for (int i = 0; i < m; ++i) {
    cin >> a[i]; 
    no[a[i]].insert(i); 
  }
  dfs(1, 1); 
  for (int i = 0; i < m - 1; ++i) {
    lco[lca(a[i], a[i + 1])].insert(i); 
  }
  while (q--) {
    int t;
    cin >> t;
    if (t == 1) {
      int pos, v;
      cin >> pos >> v;
      pos--; 
      no[a[pos]].erase(pos);
      if (pos > 0) {
        lco[lca(a[pos], a[pos - 1])].erase(pos - 1);
      }
      if (pos < m) {
        lco[lca(a[pos], a[pos + 1])].erase(pos);
      }
      a[pos] = v; 
      no[a[pos]].insert(pos);
      if (pos > 0) {
        lco[lca(a[pos], a[pos - 1])].insert(pos - 1); 
      }
      if (pos < m) {
        lco[lca(a[pos], a[pos + 1])].insert(pos + 1); 
      }
    } else {
      int l, r, v;
      cin >> l >> r >> v;
      --l, --r;
      auto s1 = lower_bound(no[v].begin(), no[v].end(), l); 
      if (s1 != no[v].end() && *s1 <= r) {
        cout << *s1 + 1 << ' ' << *s1 + 1 << '\n';
        continue;
      }
      s1 = lower_bound(lco[v].begin(), lco[v].end(), l); 
      if (s1 != lco[v].end() && *s1 < r) {
        cout << *s1 + 1 << ' ' << *s1 + 2 << '\n';
      } else {
        cout << -1 << ' ' << -1 << '\n';
      }
    }
  }
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 7 ms 38236 KB n=5
2 Correct 8 ms 38236 KB n=100
3 Incorrect 7 ms 38232 KB Wrong range.
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 38236 KB n=5
2 Correct 8 ms 38236 KB n=100
3 Incorrect 7 ms 38232 KB Wrong range.
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 38236 KB n=5
2 Correct 8 ms 38236 KB n=100
3 Incorrect 7 ms 38232 KB Wrong range.
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 38236 KB n=5
2 Correct 8 ms 38236 KB n=100
3 Incorrect 7 ms 38232 KB Wrong range.
4 Halted 0 ms 0 KB -