結果

問題 No.1030 だんしんぐぱーりない
ユーザー ChanyuhChanyuh
提出日時 2020-04-29 00:07:18
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 358 ms / 2,000 ms
コード長 4,575 bytes
コンパイル時間 2,245 ms
コンパイル使用メモリ 118,256 KB
実行使用メモリ 24,440 KB
最終ジャッジ日時 2023-08-17 07:43:36
合計ジャッジ時間 14,823 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 251 ms
22,012 KB
testcase_06 AC 189 ms
16,500 KB
testcase_07 AC 124 ms
8,568 KB
testcase_08 AC 129 ms
10,476 KB
testcase_09 AC 199 ms
21,576 KB
testcase_10 AC 90 ms
5,616 KB
testcase_11 AC 214 ms
12,056 KB
testcase_12 AC 228 ms
20,456 KB
testcase_13 AC 189 ms
17,852 KB
testcase_14 AC 257 ms
17,060 KB
testcase_15 AC 111 ms
4,828 KB
testcase_16 AC 236 ms
15,688 KB
testcase_17 AC 214 ms
22,816 KB
testcase_18 AC 272 ms
18,056 KB
testcase_19 AC 157 ms
8,068 KB
testcase_20 AC 186 ms
13,424 KB
testcase_21 AC 171 ms
16,564 KB
testcase_22 AC 188 ms
13,900 KB
testcase_23 AC 220 ms
12,000 KB
testcase_24 AC 158 ms
6,764 KB
testcase_25 AC 184 ms
11,520 KB
testcase_26 AC 123 ms
4,536 KB
testcase_27 AC 140 ms
4,664 KB
testcase_28 AC 237 ms
15,796 KB
testcase_29 AC 176 ms
19,932 KB
testcase_30 AC 171 ms
14,368 KB
testcase_31 AC 179 ms
12,868 KB
testcase_32 AC 207 ms
16,560 KB
testcase_33 AC 219 ms
20,524 KB
testcase_34 AC 91 ms
6,196 KB
testcase_35 AC 358 ms
24,416 KB
testcase_36 AC 338 ms
24,412 KB
testcase_37 AC 348 ms
24,440 KB
testcase_38 AC 357 ms
24,400 KB
testcase_39 AC 339 ms
24,300 KB
testcase_40 AC 1 ms
4,380 KB
testcase_41 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<tuple>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define Per(i,sta,n) for(int i=n-1;i>=sta;i--)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef long double ld;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};

int n,k,q;
int c[100010],L[100010];
vector<int> v;
vector<vector<int>> G;

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_){//sizeがn_のsegtreeを作る
    n=1;
    while(n<n_) n<<=1;
    dat.assign(n<<1,ti);
  }

  void build(const vector<T> &v){//vによってsegtreeをbuildする
    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){//k番目を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){//区間[a,b)に対しFを適応した値を返す
    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);
  }

  T &operator [] (int i) {return dat[i+n];};
};


struct LCA{
    int n,root,k=0;
    vector<vector<int>> D,G;
    vector<int> depth;

    void dfs(int s,int par){
        D[s].push_back(par);
        if (par!=-1) depth[s]=depth[par]+1;
        rep(i,G[s].size()){
            int t=G[s][i];
            if (t==par) continue;
            dfs(t,s);
        }
    }
    
    LCA(int r,vector<vector<int>> G_){
        n=G_.size();
        G=G_;
        root=r;
        depth.resize(n,0);
        D.resize(n,{});
        dfs(root,-1);
        while(true){
            bool flag=false;
            int m=(1<<k);
            rep(i,n){
                if (D[i][k]==-1) D[i].push_back(-1);
                else{
                    if (D[D[i][k]][k]!=-1) flag=true;
                    D[i].push_back(D[D[i][k]][k]);
                }
            }
            if (!flag) break;
            k+=1;
        }
    }

    int lca(int p,int q){
        if(p==G.size()) return q;
        if(q==G.size()) return p;
        if (depth[p]<depth[q]) swap(p,q);
        per(i,k+1){
            if (((depth[p]-depth[q]) & (1 << i))!=0) p=D[p][i];
        }
        if (p==q) return p;
        per(i,k+1){
            if (D[p][i]!=D[q][i]) {
                p=D[p][i];
                q=D[q][i];
            }
        }
        return D[p][0];
    }

    function<int(int,int)> f=[this](int a,int b){return lca(a,b);};
};

void dfs(int s,int l){
  L[s]=max(l,c[s]);
  for(int t:G[s]){
    dfs(t,L[s]);
  }
}

void solve(){
  cin >> n >> k >> q;
  v.resize(k);
  rep(i,n){
    cin >> c[i];
  }
  rep(i,k){
    cin >> v[i];
    v[i]--;
  }
  G.resize(n);
  rep(i,n-1){
    int e,f;cin >> e >> f;e--;f--;
    G[f].push_back(e);
  }
  dfs(0,0);
  LCA lca(0,G);
  SegmentTree<int> seg(lca.f,n);
  seg.build(v);
  rep(i,q){
    int m,x,y;
    cin >> m >> x >> y;x--;y--;
    if(m==1){
      seg.set_val(x,y);
    }
    else{
      cout << L[seg.query(x,y+1)] << endl;
    }
  }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(50);
    solve();
}
0