結果
| 問題 |
No.1030 だんしんぐぱーりない
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-04-17 23:18:25 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 199 ms / 2,000 ms |
| コード長 | 8,744 bytes |
| コンパイル時間 | 2,783 ms |
| コンパイル使用メモリ | 215,088 KB |
| 最終ジャッジ日時 | 2025-01-09 20:44:29 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 40 |
ソースコード
#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);}
};
struct Monoid{
using value_type = optional<int>;
static std::function<value_type(value_type, value_type)> f;
static value_type id(){return {};}
static value_type op(const value_type &a, const value_type &b){return f(a, b);}
};
std::function<optional<int>(optional<int>,optional<int>)> Monoid::f;
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);
}
HLDecomposition<int> hld(tree, 0);
Monoid::f = [&](const auto &a, const auto &b) -> optional<int>{
if(!a) return b;
if(!b) return a;
return {hld.lca(*a, *b)};
};
SegmentTree<MaxMonoid<int>> seg1(N);
REP(i,N){
seg1.update(hld.get_id(i), C[i]);
}
SegmentTree<Monoid> seg2(K);
seg2.init_with_vector(A);
/*
REP(i,K){
cerr << seg2.at(i) << " ";
}
cerr << endl;*/
dump(seg2.get(0, K));
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);
dump(lca);
int ans = 0;
hld.path_query_vertex(0, lca, [&](int l, int r){chmax(ans, seg1.get(l, r));});
pout(ans);
}
}
return 0;
}