結果
| 問題 |
No.1030 だんしんぐぱーりない
|
| コンテスト | |
| ユーザー |
ysystem7
|
| 提出日時 | 2020-04-17 22:16:44 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 654 ms / 2,000 ms |
| コード長 | 3,911 bytes |
| コンパイル時間 | 4,462 ms |
| コンパイル使用メモリ | 221,820 KB |
| 最終ジャッジ日時 | 2025-01-09 20:12:54 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 40 |
ソースコード
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define cinf(n,x) for(int i=0;i<(n);i++)cin>>x[i];
#define ft first
#define sc second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define all(v) (v).begin(),(v).end()
#define mod 1000000007
#define FS fixed<<setprecision(15)
using namespace std;
typedef long long ll;
template<class T> using V=vector<T>;
using Graph = vector<vector<int>>;
using P=pair<ll,ll>;
typedef unsigned long long ull;
typedef long double ldouble;
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
const ll INF=1e18;
const int max_E=100005,max_log_v=30;//調整
//入力
vector<int> G[max_E];
int root;
int N;//頂点数
int par[max_log_v][max_E];//親を2^k回辿って到達する頂点(根を通り過ぎる場合は-1)
int depth[max_E];//根からの深さ
void dfs(int v,int p,int d){
par[0][v]=p;
depth[v]=d;
for(int i=0;i<G[v].size();i++){
if(G[v][i]!=p) dfs(G[v][i],v,d+1);
}
}
//初期化 絶対に忘れるな!!!
void init_(int v_){
//par[0]とdepthを初期化する
dfs(root,-1,0);
//parを初期化
for(int k=0;k+1<max_log_v;k++){
for(int v=0;v<v_;v++){
if(par[k][v]<0) par[k+1][v]=-1;
else par[k+1][v]=par[k][par[k][v]];
}
}
}
//uとvのLCAを求める
int lca(int u,int v){
//uとvの深さが同じになるまで親をたどる
if(depth[u]>depth[v]) swap(u,v);
for(int k=0;k<max_log_v;k++){
if((depth[v]-depth[u])>>k&1){
v=par[k][v];
}
}
if(u==v) return u;
//二分探索でLCAを求める
for(int k=max_log_v-1;k>=0;k--){
if(par[k][u]!=par[k][v]){
u=par[k][u];
v=par[k][v];
}
}
return par[0][u];
}
const ll MAX_N=(1<<19);
ll n,K,dat[2*MAX_N-1];//n...segmenttreeの葉の大きさ,N...与えられた数列等の大きさ
ll a[1000000];
ll unit(){
return -1;//単位元
}
ll calc(ll a,ll b){
if(a==-1||b==-1) return max(a,b);
return lca(a,b);//演算
}
//初期化
void init(){
n=1;
while(n<K) n*=2;//座標とかであれば n<N ではなく n<(座標の最大値) 下も同様
for(int i=0;i<K;i++) dat[i+n-1]=a[i];
for(int i=K;i<n;i++) dat[i]=unit();//単位元で埋める
}
void update(ll k,ll s){
k+=n-1;
dat[k]=s;//変更する式
while(k>0){
k=(k-1)/2;
dat[k]=calc(dat[2*k+1],dat[2*k+2]);//条件
}
}
ll query(ll a,ll b,ll k,ll l,ll r){
if(r<=a||b<=l) return unit();//単位元
if(a<=l&&r<=b) return dat[k];
ll m=(l+r)/2;
ll u=query(a,b,2*k+1,l,m);
ll v=query(a,b,2*k+2,m,r);
return calc(u,v);//条件
}
int main(){
cin.tie(0);ios::sync_with_stdio(false);
int Q;
cin>>N>>K>>Q;
V<ll> C(N);
cinf(N,C);
//V<int> a(K);
rep(i,K){
cin>>a[i];
a[i]--;
}
rep(i,N-1){
int u,v;
cin>>u>>v;
u--;v--;
G[u].push_back(v);
G[v].push_back(u);
}
V<ll> ma(N);
ma[0]=C[0];
V<int> dist(N,-1);
queue<int> que;
que.push(0);
dist[0]=0;
while(!que.empty()){
int v=que.front();
que.pop();
for(int nv:G[v]){
if(dist[nv]!=-1) continue;
dist[nv]=dist[v]+1;
que.push(nv);
ma[nv]=max(C[nv],ma[v]);
}
}
init_(N);
init();
rep(i,K) update(i,a[i]);
while(Q--){
int t;
cin>>t;
if(t==1){
int x,y;
cin>>x>>y;
x--;y--;
update(x,y);
}else{
ll l,r;
cin>>l>>r;
l--;r--;
ll x=query(l,r+1,0,0,n);
cout<<ma[x]<<'\n';
}
}
}
ysystem7