#pragma GCC optimize ("Ofast") #include using namespace std; void *wmem; char memarr[96000000]; template inline S max_L(S a,T b){ return a>=b?a:b; } template inline void walloc1d(T **arr, int x, void **mem = &wmem){ static int skip[16] = {0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; (*mem) = (void*)( ((char*)(*mem)) + skip[((unsigned long long)(*mem)) & 15] ); (*arr)=(T*)(*mem); (*mem)=((*arr)+x); } inline int my_getchar_unlocked(){ static char buf[1048576]; static int s = 1048576; static int e = 1048576; if(s == e && e == 1048576){ e = fread_unlocked(buf, 1, 1048576, stdin); s = 0; } if(s == e){ return EOF; } return buf[s++]; } inline void rd(int &x){ int k; int m=0; x=0; for(;;){ k = my_getchar_unlocked(); if(k=='-'){ m=1; break; } if('0'<=k&&k<='9'){ x=k-'0'; break; } } for(;;){ k = my_getchar_unlocked(); if(k<'0'||k>'9'){ break; } x=x*10+k-'0'; } if(m){ x=-x; } } struct MY_WRITER{ char buf[1048576]; int s; int e; MY_WRITER(){ s = 0; e = 1048576; } ~MY_WRITER(){ if(s){ fwrite_unlocked(buf, 1, s, stdout); } } } ; MY_WRITER MY_WRITER_VAR; void my_putchar_unlocked(int a){ if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){ fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout); MY_WRITER_VAR.s = 0; } MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a; } inline void wt_L(char a){ my_putchar_unlocked(a); } inline void wt_L(int x){ int s=0; int m=0; char f[10]; if(x<0){ m=1; x=-x; } while(x){ f[s++]=x%10; x/=10; } if(!s){ f[s++]=0; } if(m){ my_putchar_unlocked('-'); } while(s--){ my_putchar_unlocked(f[s]+'0'); } } template struct segtree_pg{ int N; int logN; T *val; void malloc(int maxN, int once = 0); void walloc(int maxN, int once = 0, void **mem = &wmem); void free(void); T& operator[](int i); void setN(int n, int zerofill = 1, int dobuild = 1); void build(void); inline void build(int a); inline void change(int a, T v); inline T get(int a, int b); } ; struct graph{ int N; int *es; int **edge; void setEdge(int N__, int M, int A[], int B[], void **mem = &wmem){ int i; N = N__; walloc1d(&es, N, mem); walloc1d(&edge, N, mem); for(i=(0);i<(N);i++){ es[i] = 0; } for(i=(0);i<(M);i++){ es[A[i]]++; es[B[i]]++; } for(i=(0);i<(N);i++){ walloc1d(&edge[i], es[i], mem); } for(i=(0);i<(N);i++){ es[i] = 0; } for(i=(0);i<(M);i++){ edge[A[i]][es[A[i]]++] = B[i]; edge[B[i]][es[B[i]]++] = A[i]; } } template void getDistTree_WeightedNode_max(int root, S1 w[], S2 res[], void *mem = wmem){ int i; int j; int k; int m; int *q; int qs = 0; int qe = 1; char *vis; walloc1d(&q,N,&mem); walloc1d(&vis,N,&mem); for(i=(0);i<(N);i++){ vis[i] = 0; } vis[root] = 1; res[root] = w[root]; q[0] = root; while(qs < qe){ i = q[qs++]; for(j=(0);j<(es[i]);j++){ k = edge[i][j]; if(vis[k]){ continue; } res[k] =max_L(w[k], res[i]); q[qe++] = k; vis[k] = 1; } } } } ; struct HLD{ int N; int *es; int **edge; int *group; int *groupind; int groupNum; int *groupSize; int **groupNode; int *groupUpNode; int *groupDepth; void init(graph g, void **mem = &wmem){ init(g.N, g.es, g.edge, mem); } void init(int N__, int *es__, int **edge__, void **mem = &wmem){ int i; int j; int k; int x; int y; int mx; int *q; int q_st; int q_ed; int *sz; char *vis; void *tmpmem; N = N__; es = es__; edge = edge__; walloc1d(&group, N, mem); walloc1d(&groupind, N, mem); tmpmem = *mem; walloc1d(&q, N, &tmpmem); walloc1d(&sz, N, &tmpmem); walloc1d(&vis, N, &tmpmem); for(i=(0);i<(N);i++){ vis[i] = 0; } q_st = 0; q_ed = 1; q[0] = 0; vis[0] = 1; while(q_st < q_ed){ i = q[q_st++]; for(j=(0);j<(es[i]);j++){ k = edge[i][j]; if(!vis[k]){ vis[k] = 1; q[q_ed++] = k; } } } for(i=(0);i<(N);i++){ sz[i] = 0; } for(j=N-1;j>=0;j--){ i = q[j]; sz[i] = 1; for(k=(0);k<(es[i]);k++){ sz[i] += sz[edge[i][k]]; } } for(i=(0);i<(N);i++){ group[i] = -1; } groupNum = 0; for(j=(0);j<(N);j++){ i = q[j]; if(group[i]>=0){ continue; } group[i] = groupNum++; groupind[i] = 0; for(;;){ mx = -1; for(k=(0);k<(es[i]);k++){ if(group[edge[i][k]] != -1){ continue; } if(mx==-1){ mx = k; } else if(sz[edge[i][k]] > sz[edge[i][mx]]){ mx = k; } } if(mx==-1){ break; } group[edge[i][mx]] = group[i]; groupind[edge[i][mx]] = groupind[i]+1; i = edge[i][mx]; } } walloc1d(&groupSize, groupNum, mem); walloc1d(&groupUpNode, groupNum, mem); walloc1d(&groupDepth, groupNum, mem); for(i=(0);i<(groupNum);i++){ groupSize[i] = 0; } for(i=(0);i<(N);i++){ groupSize[group[i]]++; } walloc1d(&groupNode, groupNum, mem); for(i=(0);i<(groupNum);i++){ walloc1d(&groupNode[i], groupSize[i], mem); } for(i=(0);i<(N);i++){ groupNode[group[i]][groupind[i]] = i; } for(i=(0);i<(groupNum);i++){ groupDepth[i] = -1; } groupUpNode[0] = -1; groupDepth[0] = 0; for(x=(0);x<(groupNum);x++){ for(y=(0);y<(groupSize[x]);y++){ i = groupNode[x][y]; for(j=(0);j<(es[i]);j++){ k = edge[i][j]; if(x != group[k] && groupDepth[group[k]]==-1){ groupUpNode[group[k]] = i; groupDepth[group[k]] = groupDepth[x] + 1; } } } } } int lca(int x, int y){ int x1; int y1; int x2; int y2; x1 = group[x]; x2 = groupind[x]; y1 = group[y]; y2 = groupind[y]; while(groupDepth[x1] > groupDepth[y1]){ x = groupUpNode[x1]; x1 = group[x]; x2 = groupind[x]; } while(groupDepth[x1] < groupDepth[y1]){ y = groupUpNode[y1]; y1 = group[y]; y2 = groupind[y]; } while(x1 != y1){ x = groupUpNode[x1]; x1 = group[x]; x2 = groupind[x]; y = groupUpNode[y1]; y1 = group[y]; y2 = groupind[y]; } if(x2 <= y2){ return x; } return y; } int depth(int x){ int x1; int x2; int res = 0; x1 = group[x]; x2 = groupind[x]; while(groupUpNode[x1] != -1){ res += x2 + 1; x = groupUpNode[x1]; x1 = group[x]; x2 = groupind[x]; } return res + x2; } int dist(int x, int y){ int x1; int y1; int x2; int y2; int res = 0; x1 = group[x]; x2 = groupind[x]; y1 = group[y]; y2 = groupind[y]; while(groupDepth[x1] > groupDepth[y1]){ res += x2 + 1; x = groupUpNode[x1]; x1 = group[x]; x2 = groupind[x]; } while(groupDepth[x1] < groupDepth[y1]){ res += y2 + 1; y = groupUpNode[y1]; y1 = group[y]; y2 = groupind[y]; } while(x1 != y1){ res += x2 + y2 + 2; x = groupUpNode[x1]; x1 = group[x]; x2 = groupind[x]; y = groupUpNode[y1]; y1 = group[y]; y2 = groupind[y]; } if(x2 <= y2){ return res + y2 - x2; } return res + x2 - y2; } int up(int x){ int x1 = group[x]; int x2 = groupind[x]; if(x2==0){ return groupUpNode[x1]; } return groupNode[x1][x2-1]; } int up(int x, int d){ int x1 = group[x]; int x2 = groupind[x]; while(d > x2){ if(groupUpNode[x1]==-1){ return -1; } d -= x2 + 1; x = groupUpNode[x1]; x1 = group[x]; x2 = groupind[x]; } return groupNode[x1][x2-d]; } } ; int N; int K; int Q; int C[100000]; int A[100000]; int E[100000]; int F[100000]; int T[3]; int cmx[100000]; graph g; HLD hld; segtree_pg t; void segtree_pg_func(int &res, int &a, int &b){ res = hld.lca(a,b); } int main(){ int ao_dF3pO, i; wmem = memarr; rd(N); rd(K); rd(Q); { int Lj4PdHRW; for(Lj4PdHRW=(0);Lj4PdHRW<(N);Lj4PdHRW++){ rd(C[Lj4PdHRW]); } } { int e98WHCEY; for(e98WHCEY=(0);e98WHCEY<(K);e98WHCEY++){ rd(A[e98WHCEY]);A[e98WHCEY] += (-1); } } { int FmcKpFmN; for(FmcKpFmN=(0);FmcKpFmN<(N-1);FmcKpFmN++){ rd(E[FmcKpFmN]);E[FmcKpFmN] += (-1); rd(F[FmcKpFmN]);F[FmcKpFmN] += (-1); } } g.setEdge(N,N-1,E,F); g.getDistTree_WeightedNode_max(0, C, cmx); hld.init(g); t.malloc(K); t.setN(K); for(i=(0);i<(K);i++){ t[i] = A[i]; } t.build(); for(ao_dF3pO=(0);ao_dF3pO<(Q);ao_dF3pO++){ rd(T[0]); rd(T[1]);T[1] += (-1); rd(T[2]);T[2] += (-1); if(T[0]==1){ t.change(T[1], T[2]); } else{ wt_L(cmx[t.get(T[1], T[2]+1)]); wt_L('\n'); } } return 0; } template void segtree_pg::malloc(int maxN, int once /*= 0*/){ int i; for(i=1;i void segtree_pg::walloc(int maxN, int once /*= 0*/, void **mem /*= &wmem*/){ int i; for(i=1;i void segtree_pg::free(void){ delete [] val; } template T& segtree_pg::operator[](int i){ return val[N+i]; } template void segtree_pg::setN(int n, int zerofill /*= 1*/, int dobuild /*= 1*/){ int i; for(i=1,logN=0;i void segtree_pg::build(void){ for(int i=N-1;i;i--){ segtree_pg_func(val[i], val[2*i], val[2*i+1]); } } template inline void segtree_pg::build(int a){ while(a > 1){ a /= 2; segtree_pg_func(val[a], val[2*a], val[2*a+1]); } } template inline void segtree_pg::change(int a, T v){ val[a+N] = v; build(a+N); } template inline T segtree_pg::get(int a, int b){ T res; T tmp; int fga = 0; int fgb = 0; a += N; b += N; while(a < b){ if(a%2){ if(fga){ segtree_pg_func(res, res, val[a]); } else{ res = val[a]; fga = 1; } a++; } if(b%2){ b--; if(fgb){ segtree_pg_func(tmp, val[b], tmp); } else{ tmp = val[b]; fgb = 1; } } a /= 2; b /= 2; } if(fga==1 && fgb==0){ return res; } if(fga==0 && fgb==1){ return tmp; } if(fga==1 && fgb==1){ segtree_pg_func(res, res, tmp); return res; } return res; } // cLay varsion 20200419-1 [beta] // --- original code --- // int N, K, Q, C[1d5], A[1d5], E[1d5], F[1d5], T[3]; // // int cmx[1d5]; // graph g; // HLD hld; // segtree_pg t; // // void segtree_pg_func(int &res, int &a, int &b){ // res = hld.lca(a,b); // } // // { // rd(N,K,Q,C(N),(A--)(K),(E--,F--)(N-1)); // // g.setEdge(N,N-1,E,F); // g.getDistTree_WeightedNode_max(0, C, cmx); // hld.init(g); // // t.malloc(K); // t.setN(K); // rep(i,K) t[i] = A[i]; // t.build(); // // rep(Q){ // rd(T[0],T[1]--,T[2]--); // if(T[0]==1){ // t.change(T[1], T[2]); // } else { // wt(cmx[t.get(T[1], T[2]+1)]); // } // } // }