結果

問題 No.1030 だんしんぐぱーりない
ユーザー tnakao0123tnakao0123
提出日時 2020-04-19 18:42:06
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 234 ms / 2,000 ms
コード長 3,458 bytes
コンパイル時間 1,755 ms
コンパイル使用メモリ 112,092 KB
実行使用メモリ 18,944 KB
最終ジャッジ日時 2024-04-15 20:02:50
合計ジャッジ時間 8,971 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 11 ms
16,256 KB
testcase_01 AC 10 ms
16,256 KB
testcase_02 AC 11 ms
16,256 KB
testcase_03 AC 10 ms
16,256 KB
testcase_04 AC 10 ms
16,256 KB
testcase_05 AC 162 ms
18,304 KB
testcase_06 AC 134 ms
17,920 KB
testcase_07 AC 99 ms
17,152 KB
testcase_08 AC 96 ms
17,408 KB
testcase_09 AC 131 ms
18,432 KB
testcase_10 AC 80 ms
16,640 KB
testcase_11 AC 164 ms
17,408 KB
testcase_12 AC 144 ms
18,048 KB
testcase_13 AC 122 ms
17,920 KB
testcase_14 AC 173 ms
17,920 KB
testcase_15 AC 99 ms
16,768 KB
testcase_16 AC 151 ms
17,664 KB
testcase_17 AC 136 ms
18,560 KB
testcase_18 AC 187 ms
18,176 KB
testcase_19 AC 113 ms
16,896 KB
testcase_20 AC 124 ms
17,536 KB
testcase_21 AC 110 ms
17,664 KB
testcase_22 AC 122 ms
17,408 KB
testcase_23 AC 160 ms
17,408 KB
testcase_24 AC 128 ms
16,896 KB
testcase_25 AC 138 ms
17,408 KB
testcase_26 AC 101 ms
16,512 KB
testcase_27 AC 117 ms
16,640 KB
testcase_28 AC 155 ms
17,920 KB
testcase_29 AC 105 ms
18,176 KB
testcase_30 AC 107 ms
17,408 KB
testcase_31 AC 121 ms
17,280 KB
testcase_32 AC 141 ms
17,920 KB
testcase_33 AC 140 ms
18,304 KB
testcase_34 AC 75 ms
16,768 KB
testcase_35 AC 234 ms
18,688 KB
testcase_36 AC 213 ms
18,944 KB
testcase_37 AC 217 ms
18,816 KB
testcase_38 AC 228 ms
18,944 KB
testcase_39 AC 217 ms
18,816 KB
testcase_40 AC 11 ms
16,384 KB
testcase_41 AC 10 ms
16,512 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 1030.cc:  No.1030 だんしんぐぱーりない - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 100000;
const int MAX_K = 100000;
const int MAX_E2 = 1 << 18; // = 262144
const int BN = 17;

/* typedef */

typedef vector<int> vi;
typedef queue<int> qi;

template <typename T, const int MAX_E2, const int BN>
struct SegTreeLCA {
  int n, e2;
  T nodes[MAX_E2];
  int ps[MAX_E2 / 2][BN], ds[MAX_E2 / 2];
  SegTreeLCA() {}

  void init(int _n) {
    n = _n;
    for (e2 = 1; e2 < n; e2 <<= 1);
    memset(nodes, -1, sizeof(nodes));
    memset(ps, -1, sizeof(ps));
    memset(ds, 0, sizeof(ds));
  }

  T &get(int i) { return nodes[e2 - 1 + i]; }

  int __lca(int u, int v) {
    if (u < 0) return v;
    if (v < 0) return u;
    if (ds[u] > ds[v]) swap(u, v);

    for (int i = BN - 1; i >= 0; i--)
      if (((ds[v] - ds[u]) >> i) & 1) v = ps[v][i];
    if (u == v) return u;

    for(int i = BN - 1; i >= 0; i--)
      if (ps[u][i] != ps[v][i])
	u = ps[u][i], v = ps[v][i];

    return ps[u][0];
  }
  
  void setall() {
    for (int j = e2 - 2; j >= 0; j--)
      nodes[j] = __lca(nodes[j * 2 + 1], nodes[j * 2 + 2]);
  }
  
  void set(int i, T v) {
    int j = e2 - 1 + i;
    nodes[j] = v;
    while (j > 0) {
      j = (j - 1) / 2;
      nodes[j] = __lca(nodes[j * 2 + 1], nodes[j * 2 + 2]);
    }
  }

  T lca_range(int r0, int r1, int k, int i0, int i1) {
    if (r1 <= i0 || i1 <= r0) return -1;
    if (r0 <= i0 && i1 <= r1) return nodes[k];

    int im = (i0 + i1) / 2;
    T v0 = lca_range(r0, r1, k * 2 + 1, i0, im);
    T v1 = lca_range(r0, r1, k * 2 + 2, im, i1);
    return __lca(v0, v1);
  }
  T lca_range(int r0, int r1) { return lca_range(r0, r1, 0, 0, e2); }
};

/* global variables */

int cs[MAX_N], as[MAX_K];
vi nbrs[MAX_N];
SegTreeLCA<int,MAX_E2,BN> st;

/* subroutines */

/* main */

int main() {
  int n, k, qn;
  scanf("%d%d%d", &n, &k, &qn);

  st.init(k);
  
  for (int i = 0; i < n; i++) scanf("%d", cs + i);
  for (int i = 0; i < k; i++) scanf("%d", as + i), as[i]--;

  for (int i = 1; i < n; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    a--, b--;
    st.ps[a][0] = b;
    nbrs[b].push_back(a);
  }

  for (int i = 0; i + 1 < BN; i++)
    for (int u = 0; u < n; u++)
      st.ps[u][i + 1] = (st.ps[u][i] >= 0) ? st.ps[st.ps[u][i]][i] : -1;
	    
  st.ds[0] = 0;
  qi q;
  q.push(0);

  while (! q.empty()) {
    int u = q.front(); q.pop();
    int up = st.ps[u][0];
    if (up >= 0 && cs[u] < cs[up]) cs[u] = cs[up];

    int vd = st.ds[u] + 1;
    vi &nbru = nbrs[u];
    for (vi::iterator vit = nbru.begin(); vit != nbru.end(); vit++) {
      int v = *vit;
      st.ds[v] = vd;
      q.push(v);
    }
  }
  //for (int i = 0; i < n; i++) printf("%d: %d,%d\n", i, ls[i], rs[i]);

  for (int i = 0; i < k; i++) st.get(i) = as[i];
  st.setall();

  while (qn--) {
    int t;
    scanf("%d", &t);

    if (t == 1) {
      int x, y;
      scanf("%d%d", &x, &y);
      x--, y--;
      st.set(x, y);
    }
    else {
      int l, r;
      scanf("%d%d", &l, &r);
      l--;
      int v = st.lca_range(l, r);
      printf("%d\n", cs[v]);
    }
  }
  return 0;
}
0