結果

問題 No.1030 だんしんぐぱーりない
ユーザー beetbeet
提出日時 2020-04-17 21:50:40
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 312 ms / 2,000 ms
コード長 3,825 bytes
コンパイル時間 2,445 ms
コンパイル使用メモリ 210,724 KB
実行使用メモリ 32,108 KB
最終ジャッジ日時 2024-04-14 13:30:15
合計ジャッジ時間 10,793 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 214 ms
27,768 KB
testcase_06 AC 162 ms
21,700 KB
testcase_07 AC 97 ms
10,860 KB
testcase_08 AC 105 ms
14,168 KB
testcase_09 AC 183 ms
28,504 KB
testcase_10 AC 62 ms
6,944 KB
testcase_11 AC 171 ms
16,168 KB
testcase_12 AC 179 ms
24,920 KB
testcase_13 AC 160 ms
22,688 KB
testcase_14 AC 211 ms
22,244 KB
testcase_15 AC 76 ms
6,940 KB
testcase_16 AC 176 ms
18,884 KB
testcase_17 AC 187 ms
29,008 KB
testcase_18 AC 234 ms
23,632 KB
testcase_19 AC 105 ms
9,816 KB
testcase_20 AC 138 ms
16,064 KB
testcase_21 AC 135 ms
20,572 KB
testcase_22 AC 139 ms
16,548 KB
testcase_23 AC 167 ms
15,296 KB
testcase_24 AC 112 ms
9,088 KB
testcase_25 AC 147 ms
15,648 KB
testcase_26 AC 79 ms
6,940 KB
testcase_27 AC 90 ms
6,944 KB
testcase_28 AC 186 ms
19,744 KB
testcase_29 AC 144 ms
24,576 KB
testcase_30 AC 122 ms
17,100 KB
testcase_31 AC 128 ms
15,388 KB
testcase_32 AC 179 ms
21,832 KB
testcase_33 AC 193 ms
25,868 KB
testcase_34 AC 66 ms
7,808 KB
testcase_35 AC 312 ms
31,856 KB
testcase_36 AC 301 ms
31,984 KB
testcase_37 AC 312 ms
31,984 KB
testcase_38 AC 310 ms
32,108 KB
testcase_39 AC 307 ms
31,988 KB
testcase_40 AC 2 ms
6,944 KB
testcase_41 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
using Int = long long;
const char newl = '\n';


struct LowestCommonAncestor{
  Int n,h;
  vector< vector<Int> > G,par;
  vector<Int> dep;
  LowestCommonAncestor(){}
  LowestCommonAncestor(Int n):n(n),G(n),dep(n){
    h=1;
    while((1<<h)<=n) h++;
    par.assign(h,vector<Int>(n,-1));
  }

  void add_edge(Int u,Int v){
    G[u].emplace_back(v);
    G[v].emplace_back(u);
  }

  void dfs(Int v,Int p,Int d){
    par[0][v]=p;
    dep[v]=d;
    for(Int u:G[v])
      if(u!=p) dfs(u,v,d+1);
  }

  void build(Int r=0){
    dfs(r,-1,0);
    for(Int k=0;k+1<h;k++)
      for(Int v=0;v<n;v++)
        if(~par[k][v])
          par[k+1][v]=par[k][par[k][v]];
  }

  Int lca(Int u,Int v){
    if(dep[u]>dep[v]) swap(u,v);
    for(Int k=0;k<h;k++)
      if((dep[v]-dep[u])>>k&1)
        v=par[k][v];

    if(u==v) return u;

    for(Int k=h-1;k>=0;k--)
      if(par[k][u]!=par[k][v])
        u=par[k][u],v=par[k][v];

    return par[0][u];
  }

  Int distance(Int u,Int v){
    return dep[u]+dep[v]-dep[lca(u,v)]*2;
  }
};


template <typename T>
struct SegmentTree{
  using F = function<T(T,T)>;
  Int n;
  F f;
  T ti;
  vector<T> dat;

  SegmentTree(){}
  SegmentTree(F f,T ti):f(f),ti(ti){}

  void init(Int n_){
    n=1;
    while(n<n_) n<<=1;
    dat.assign(n<<1,ti);
  }

  void build(const vector<T> &v){
    Int n_=v.size();
    init(n_);
    for(Int i=0;i<n_;i++) dat[n+i]=v[i];
    for(Int i=n-1;i;i--)
      dat[i]=f(dat[(i<<1)|0],dat[(i<<1)|1]);
  }

  void set_val(Int k,T x){
    dat[k+=n]=x;
    while(k>>=1)
      dat[k]=f(dat[(k<<1)|0],dat[(k<<1)|1]);
  }

  T query(Int a,Int b){
    if(a>=b) return ti;
    T vl=ti,vr=ti;
    for(Int l=a+n,r=b+n;l<r;l>>=1,r>>=1) {
      if(l&1) vl=f(vl,dat[l++]);
      if(r&1) vr=f(dat[--r],vr);
    }
    return f(vl,vr);
  }

  template<typename C>
  Int find(Int st,C &check,T &acc,Int k,Int l,Int r){
    if(l+1==r){
      acc=f(acc,dat[k]);
      return check(acc)?k-n:-1;
    }
    Int m=(l+r)>>1;
    if(m<=st) return find(st,check,acc,(k<<1)|1,m,r);
    if(st<=l&&!check(f(acc,dat[k]))){
      acc=f(acc,dat[k]);
      return -1;
    }
    Int vl=find(st,check,acc,(k<<1)|0,l,m);
    if(~vl) return vl;
    return find(st,check,acc,(k<<1)|1,m,r);
  }

  template<typename C>
  Int find(Int st,C &check){
    T acc=ti;
    return find(st,check,acc,1,0,n);
  }
};


template<typename F>
struct FixPoint : F{
  FixPoint(F&& f):F(forward<F>(f)){}
  template<typename... Args>
  decltype(auto) operator()(Args&&... args) const{
    return F::operator()(*this,forward<Args>(args)...);
  }
};
template<typename F>
inline decltype(auto) MFP(F&& f){
  return FixPoint<F>{forward<F>(f)};
}

//INSERT ABOVE HERE
signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  Int n,k,q;
  cin>>n>>k>>q;

  vector<Int> cs(n),as(k);
  for(Int i=0;i<n;i++) cin>>cs[i];
  for(Int i=0;i<k;i++) cin>>as[i],as[i]--;

  vector<vector<Int>> G(n);
  LowestCommonAncestor lca(n);
  for(Int i=1;i<n;i++){
    Int e,f;
    cin>>e>>f;
    e--;f--;
    G[f].emplace_back(e);
    lca.add_edge(e,f);
  }
  lca.build(0);

  vector<Int> ans(n);
  MFP([&](auto dfs,Int v,Int x)->void{
    chmax(x,cs[v]);
    ans[v]=x;
    for(Int u:G[v]) dfs(u,x);
  })(0,-1);

  Int ti=-1;
  auto f=[&](Int u,Int v){
    if(u==ti) return v;
    if(v==ti) return u;
    return lca.lca(u,v);
  };
  SegmentTree<Int> seg(f,ti);
  seg.build(as);

  for(Int i=0;i<q;i++){
    Int t;
    cin>>t;
    if(t==1){
      Int x,y;
      cin>>x>>y;
      x--;y--;
      as[x]=y;
      seg.set_val(x,as[x]);
    }
    if(t==2){
      Int l,r;
      cin>>l>>r;
      l--;
      cout<<ans[seg.query(l,r)]<<newl;
    }
  }
  return 0;
}
0