結果

問題 No.1030 だんしんぐぱーりない
ユーザー HaarHaar
提出日時 2020-07-11 01:55:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 160 ms / 2,000 ms
コード長 8,621 bytes
コンパイル時間 2,698 ms
コンパイル使用メモリ 220,464 KB
実行使用メモリ 18,560 KB
最終ジャッジ日時 2024-10-11 23:04:37
合計ジャッジ時間 10,690 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,248 KB
testcase_03 AC 2 ms
5,248 KB
testcase_04 AC 2 ms
5,248 KB
testcase_05 AC 118 ms
16,000 KB
testcase_06 AC 96 ms
13,440 KB
testcase_07 AC 59 ms
7,680 KB
testcase_08 AC 62 ms
9,856 KB
testcase_09 AC 107 ms
17,024 KB
testcase_10 AC 40 ms
5,504 KB
testcase_11 AC 97 ms
11,008 KB
testcase_12 AC 104 ms
14,080 KB
testcase_13 AC 87 ms
13,824 KB
testcase_14 AC 113 ms
13,952 KB
testcase_15 AC 49 ms
5,760 KB
testcase_16 AC 99 ms
11,392 KB
testcase_17 AC 104 ms
16,512 KB
testcase_18 AC 123 ms
14,848 KB
testcase_19 AC 64 ms
6,912 KB
testcase_20 AC 79 ms
9,728 KB
testcase_21 AC 80 ms
12,416 KB
testcase_22 AC 79 ms
10,240 KB
testcase_23 AC 92 ms
9,984 KB
testcase_24 AC 67 ms
7,296 KB
testcase_25 AC 83 ms
10,624 KB
testcase_26 AC 50 ms
5,248 KB
testcase_27 AC 57 ms
5,248 KB
testcase_28 AC 104 ms
11,904 KB
testcase_29 AC 85 ms
14,208 KB
testcase_30 AC 75 ms
10,368 KB
testcase_31 AC 76 ms
9,728 KB
testcase_32 AC 96 ms
13,440 KB
testcase_33 AC 105 ms
15,104 KB
testcase_34 AC 41 ms
6,272 KB
testcase_35 AC 155 ms
18,432 KB
testcase_36 AC 151 ms
18,560 KB
testcase_37 AC 157 ms
18,432 KB
testcase_38 AC 157 ms
18,432 KB
testcase_39 AC 160 ms
18,304 KB
testcase_40 AC 2 ms
5,248 KB
testcase_41 AC 2 ms
5,248 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using i32 = int_fast32_t;
using i64 = int_fast64_t;
using u32 = uint_fast32_t;
using u64 = uint_fast64_t;
using f64 = double;
using f80 = long double;

#define FOR(v, a, b) for(i64 v = (a); v < (b); ++v)
#define FORE(v, a, b) for(i64 v = (a); v <= (b); ++v)
#define REP(v, n) FOR(v, 0, n)
#define REPE(v, n) FORE(v, 0, n)
#define REV(v, a, b) for(i64 v = (a); v >= (b); --v)
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define ITR(it, c) for(auto it = (c).begin(); it != (c).end(); ++it)
#define RITR(it, c) for(auto it = (c).rbegin(); it != (c).rend(); ++it)
#define EXIST(c,x) ((c).find(x) != (c).end())
#define fst first
#define snd second
#define UNIQ(v) (v).erase(unique(ALL(v)), (v).end())
#define bit(i) (1LL<<(i))

#ifdef DEBUG
#include <Mylib/Debug/debug.cpp>
#else
#define dump(...) ((void)0)
#endif

using namespace std;

template <typename I> void join(ostream &ost, I s, I t, string d=" "){for(auto i=s; i!=t; ++i){if(i!=s)ost<<d; ost<<*i;}ost<<endl;}
template <typename T> istream& operator>>(istream &is, vector<T> &v){for(auto &a : v) is >> a; return is;}
template <typename T> void pout(const T &value){std::cout << value << "\n";}
template <typename T, typename ...Args> void pout(const T &value, const Args&... args){std::cout << value << " ";pout(args...);}

template <typename T, typename U> bool chmin(T &a, const U &b){return (a>b ? a=b, true : false);}
template <typename T, typename U> bool chmax(T &a, const U &b){return (a<b ? a=b, true : false);}
template <typename T, size_t N, typename U> void fill_array(T (&a)[N], const U &v){fill((U*)a, (U*)(a+N), v);}
template <typename T> auto make_vector(int n, int m, const T &value){return vector<vector<T>>(n, vector<T>(m, value));}


struct Init{
  Init(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(12);
    cerr << fixed << setprecision(12);
  }
}init;


template <typename Cost = int> class Edge{
public:
  int from,to;
  Cost cost;
  Edge() {}
  Edge(int to, Cost cost): to(to), cost(cost){}
  Edge(int from, int to, Cost cost): from(from), to(to), cost(cost){}

  Edge rev() const {return Edge(to,from,cost);}
  
  friend std::ostream& operator<<(std::ostream &os, const Edge &e){
    os << "(FROM: " << e.from << "," << "TO: " << e.to << "," << "COST: " << e.cost << ")";
    return os;
  }
};

template <typename T> using Graph = std::vector<std::vector<Edge<T>>>;
template <typename T> using Tree = std::vector<std::vector<Edge<T>>>;

template <typename C, typename T> void add_edge(C &g, int from, int to, T w){
  g[from].emplace_back(from, to, w);
}

template <typename C, typename T> void add_undirected(C &g, int a, int b, T w){
  add_edge(g, a, b, w);
  add_edge(g, b, a, w);
}


template <typename T> class HLDecomposition{
  Tree<T> tree;
  int n;
  
  std::vector<int> sub, // subtree size
    par, // parent id
    head, // chain head id 
    id, // id[original id] = hld id
    rid, // rid[hld id] = original id
    next, // next node in a chain
    end; // 

  int dfs_sub(int cur, int p){
    par[cur] = p;
    int t = 0;
    for(auto &e : tree[cur]){
      if(e.to == p) continue;
      sub[cur] += dfs_sub(e.to, cur);
      if(sub[e.to] > t){
        t = sub[e.to];
        next[cur] = e.to;
        std::swap(e, tree[cur][0]);
      }
    }
    return sub[cur];
  }

  void dfs_build(int cur, int &i){
    id[cur] = i;
    rid[i] = cur;
    ++i;

    for(auto &e : tree[cur]){
      if(e.to == par[cur]) continue;
      head[e.to] = (e.to == tree[cur][0].to ? head[cur] : e.to);
      dfs_build(e.to, i);
    }

    end[cur] = i;
  }
  

public:
  HLDecomposition(const Tree<T> &tree, int root):
    tree(tree), n(tree.size()), sub(n,1), par(n,-1), head(n), id(n), rid(n), next(n,-1), end(n,-1){
    dfs_sub(root, -1);
    int i = 0;
    dfs_build(root, i);
  }

  template <typename Func> // std::function<void(int,int)>
  void path_query_vertex(int x, int y, const Func &f) const {
    while(1){
      if(id[x] > id[y]) std::swap(x, y);
      f(std::max(id[head[y]], id[x]), id[y]+1);
      if(head[x] == head[y]) return;
      y = par[head[y]];
    }
  }

  template <typename LeftFunc, typename RightFunc>
  void path_query_vertex(int x, int y, const LeftFunc &f, const RightFunc &g) const {
    const int w = lca(x, y);

    path_query_vertex(x, w, f);

    x = y;
    y = w;

    while(1){
      if(id[x] > id[y]){
        std::swap(x, y);
      }
      g(std::max({id[head[y]], id[x], id[w]+1}), id[y]+1);
      if(head[x] == head[y]) return;
      y = par[head[y]];
    }
  }

  template <typename Func> // std::function<void(int,int)>
  void path_query_edge(int x, int y, const Func &f) const {
    while(1){
      if(id[x] > id[y]) std::swap(x, y);
      if(head[x] == head[y]){
        if(x != y) f(id[x]+1, id[y]+1);
        return;
      }
      f(id[head[y]], id[y]+1);
      y = par[head[y]];
    }
  }
  
  template <typename Func> // std::function<void(int,int)>
  void subtree_query_edge(int x, const Func &f) const {
    f(id[x]+1, end[x]);
  }

  template <typename Func> // std::function<void(int,int)>
  void subtree_query_vertex(int x, const Func &f) const {
    f(id[x], end[x]);
  }

  int get_edge_id(int u, int v) const { // 辺に対応するid
    if(par[u] == v){
      return id[u];
    }else if(par[v] == u){
      return id[v];
    }
 
    return -1;
  }

  int parent(int x) const {return par[x];};

  int lca(int u, int v) const {
    while(1){
      if(id[u] > id[v]) std::swap(u, v);
      if(head[u] == head[v]) return u;
      v = par[head[v]];
    }
  }

  int get_id(int x) const {
    return id[x];
  }
};


template <typename Monoid>
class SegmentTree{
  using value_type = typename Monoid::value_type;
  
  int depth, size, hsize;
  std::vector<value_type> data;

public:
  SegmentTree(){}
  SegmentTree(int n):
    depth(n > 1 ? 32-__builtin_clz(n-1) + 1 : 1),
    size(1 << depth), hsize(size / 2),
    data(size, Monoid::id())
  {}

  inline auto operator[](int i) const {return at(i);}
  inline auto at(int i) const {return data[hsize + i];}
  
  inline auto get(int x, int y) const { // [x,y)
    value_type ret_left = Monoid::id();
    value_type ret_right = Monoid::id();
    
    int l = x + hsize, r = y + hsize;
    while(l < r){
      if(r & 1) ret_right = Monoid::op(data[--r], ret_right);
      if(l & 1) ret_left = Monoid::op(ret_left, data[l++]);
      l >>= 1, r >>= 1;
    }
    
    return Monoid::op(ret_left, ret_right);
  }

  inline void update(int i, const value_type &x){
    i += hsize;
    data[i] = x;
    while(i > 1) i >>= 1, data[i] = Monoid::op(data[i << 1 | 0], data[i << 1 | 1]);
  }

  template <typename T>
  inline void init_with_vector(const std::vector<T> &val){
    data.assign(size, Monoid::id());
    for(int i = 0; i < (int)val.size(); ++i) data[hsize + i] = val[i];
    for(int i = hsize-1; i >= 1; --i) data[i] = Monoid::op(data[i << 1 | 0], data[i << 1 | 1]);
  }

  template <typename T>
  inline void init(const T &val){
    init_with_vector(std::vector<value_type>(hsize, val));
  }  
};



template <typename T>
struct MaxMonoid{
  using value_type = T;
  constexpr inline static value_type id(){return std::numeric_limits<T>::lowest();}
  constexpr inline static value_type op(const value_type &a, const value_type &b){return std::max(a, b);}
};


template <typename Semigroup>
struct MaybeMonoid{
  using value_type = optional<typename Semigroup::value_type>;
  
  static value_type id(){return {};}
  static value_type op(const value_type &a, const value_type &b){
    if(not a) return b;
    if(not b) return a;
    return {Semigroup::op(*a, *b)};
  }
};


int main(){
  int N, K, Q; cin >> N >> K >> Q;
  vector<int> C(N); cin >> C;
  vector<int> A(K); cin >> A;
  REP(i,K) A[i] -= 1;

  Tree<int> tree(N);
  REP(i,N-1){
    int e, f; cin >> e >> f;
    --e, --f;
    add_edge(tree, f, e, 1);
  }

  static HLDecomposition<int> hld(tree, 0);


  SegmentTree<MaxMonoid<int>> seg1(N);
  REP(i,N){
    seg1.update(hld.get_id(i), C[i]);
  }

  struct Semigroup{
    using value_type = int;
    static value_type op(const value_type &a, const value_type &b){
      return hld.lca(a, b);
    }
  };

  SegmentTree<MaybeMonoid<Semigroup>> seg2(K);
  seg2.init_with_vector(A);

  while(Q--){
    int T; cin >> T;

    if(T == 1){
      int X, Y; cin >> X >> Y;
      --X; --Y;

      seg2.update(X, Y);
    }else{
      int L, R; cin >> L >> R;
      
      int lca = *seg2.get(L-1, R);
      int ans = 0;
      hld.path_query_vertex(0, lca, [&](int l, int r){chmax(ans, seg1.get(l, r));});
      pout(ans);
    }
  }

  return 0;
}
0