1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
| #include<algorithm> #include<iostream> #include<iomanip> #include<cstring> #include<cstdlib> #include<vector> #include<cstdio> #include<cmath> #include<queue> using namespace std;
inline const int Get_Int() { int num=0,bj=1; char x=getchar(); while(x<'0'||x>'9') { if(x=='-')bj=-1; x=getchar(); } while(x>='0'&&x<='9') { num=num*10+x-'0'; x=getchar(); } return num*bj; }
const int maxn=200005; int n,q,a[maxn],p[maxn][25],Depth[maxn],Target[maxn]; vector<int>edges[maxn];
void AddEdge(int x,int y) { edges[x].push_back(y); }
void Dfs(int Now,int fa) { int pos=fa; for(int i=log2(2*n); i>=0; i--) if(~p[pos][i]&&a[p[pos][i]]<=a[Now])pos=p[pos][i]; if(a[pos]>a[Now])p[Now][0]=fa; else p[Now][0]=p[pos][0]; for(int j=1; j<=log2(2*n); j++) if(~p[Now][j-1])p[Now][j]=p[p[Now][j-1]][j-1]; Depth[Now]=Depth[fa]+1; for(int Next:edges[Now]) { if(Next==fa)continue; Dfs(Next,Now); } }
int main() { memset(p,-1,sizeof(p)); n=Get_Int(); q=Get_Int(); for(int i=1; i<=n; i++)a[i]=Get_Int(); for(int i=1; i<n; i++) { int x=Get_Int(),y=Get_Int(); AddEdge(x,y); AddEdge(y,x); } for(int i=1; i<=q; i++) { int x=Get_Int(),y=Get_Int(),v=Get_Int(); a[n+i]=v; AddEdge(n+i,x); AddEdge(x,n+i); Target[i]=y; } Dfs(1,-1); for(int i=1; i<=q; i++) { int ans=0,pos=n+i; for(int j=log2(2*n); j>=0; j--) if(~p[pos][j]&&Depth[p[pos][j]]>=Depth[Target[i]]) { ans^=1<<j; pos=p[pos][j]; } printf("%d\n",ans); } return 0; }
|