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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
| #include<bits/stdc++.h> #define int long long #define ull unsigned long long #define maxn 200005 #define put() putchar('\n') #define Tp template<typename Ty> #define Ts template<typename Ty,typename... Ar> using namespace std; inline void read(int &x){ int f=1;x=0;char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();} while (c>='0'&&c<='9') {x=x*10+c-'0';c=getchar();} x*=f; } namespace Debug{ Tp void _debug(char* f,Ty t){cerr<<f<<'='<<t<<endl;} Ts void _debug(char* f,Ty x,Ar... y){while(*f!=',') cerr<<*f++;cerr<<'='<<x<<",";_debug(f+1,y...);} Tp ostream& operator<<(ostream& os,vector<Ty>& V){os<<"[";for(auto& vv:V) os<<vv<<",";os<<"]";return os;} #define gdb(...) _debug((char*)#__VA_ARGS__,__VA_ARGS__) }using namespace Debug; int n,q; struct ed{ int x,y,v,c,s; }e[maxn],w[maxn]; vector<ed>O[maxn]; vector<int>to[maxn]; int dfn[maxn],top[maxn],fa[maxn],id[maxn],idnum,deep[maxn],siz[maxn],son[maxn]; inline void dfs1(int x,int pre) { int i;siz[x]=1,deep[x]=deep[pre]+1,fa[x]=pre; for (auto y:to[x]) if (y^pre) { dfs1(y,x); siz[x]+=siz[y]; if (!son[x]||siz[son[x]]<siz[y]) son[x]=y; } } const int inf=1e9; inline void dfs2(int x,int pre,int u) { top[x]=u;id[x]+=++idnum; if (!son[x]) return ; dfs2(son[x],x,u); for (auto y:to[x]) if (y!=pre&&y!=son[x]) dfs2(y,x,y); } inline bool cmp(ed x,ed y) {return x.s<y.s;} int g[maxn],tot,cnt; int root[maxn]; struct node{ int ls,rs,sum; }f[maxn*40]; inline int Update(int l,int r,int pre,int head,int c) { int rt=++cnt;f[rt]=f[pre];f[rt].sum+=c; if (l==r) return rt; int mid=l+r>>1; if (head<=mid) f[rt].ls=Update(l,mid,f[rt].ls,head,c); else f[rt].rs=Update(mid+1,r,f[rt].rs,head,c); return rt; } inline int Query(int l,int r,int rt,int head,int tail) { if (!rt) return 0; if (head<=l&&r<=tail) return f[rt].sum; int mid=l+r>>1,tmp1=0,tmp2=0; if (head<=mid) tmp1=Query(l,mid,f[rt].ls,head,tail); if (tail>mid) tmp2=Query(mid+1,r,f[rt].rs,head,tail); return tmp1+tmp2; } int lca; namespace seg{ int f[maxn<<2]; inline void init(void) { memset(f,0x3f,sizeof(f)); } inline void Update(int l,int r,int rt,int head,int w) { f[rt]=min(f[rt],w); if (l==r) return f[rt]=w,void(); int mid=l+r>>1; if (head<=mid) Update(l,mid,rt<<1,head,w); else Update(mid+1,r,rt<<1|1,head,w); } inline int Query(int l,int r,int rt,int head,int tail) { if (head<=l&&r<=tail) return f[rt]; int mid=l+r>>1,tmp1=inf,tmp2=inf; if (head<=mid) tmp1=Query(l,mid,rt<<1,head,tail); if (tail>mid) tmp2=Query(mid+1,r,rt<<1|1,head,tail); return min(tmp1,tmp2); } inline int query(int x,int y) { int ans=inf; while (top[x]^top[y]) { if (deep[top[x]]<deep[top[y]]) swap(x,y); int tmp=Query(1ll,n,1ll,id[top[x]],id[x]);ans=min(ans,tmp); x=fa[top[x]]; } if (deep[x]<deep[y]) swap(x,y); if (x^y) { ans=min(ans,Query(1ll,n,1ll,id[y]+1,id[x])); } lca=y; return ans; } } inline bool check(int val,int x,int y,int z) { val--; int ans=0; ans=Query(1,n,root[val],1,id[x])+Query(1,n,root[val],1,id[y])-Query(1,n,root[val],1,id[lca])*2; return ans<=z; } signed main(void){ freopen("arozustan.in","r",stdin); freopen("arozustan.out","w",stdout); int i,x,y,z; read(n); for (i=1;i<n;i++) { read(e[i].x),read(e[i].y),read(e[i].v),read(e[i].c),read(e[i].s); g[++tot]=e[i].v,g[++tot]=e[i].s; to[e[i].x].push_back(e[i].y); to[e[i].y].push_back(e[i].x); } sort(g+1,g+1+tot); tot=unique(g+1,g+1+tot)-g-1; seg::init(); for (i=1;i<n;i++) e[i].v=lower_bound(g+1,g+1+tot,e[i].v)-g,e[i].s=lower_bound(g+1,g+1+tot,e[i].s)-g; dfs1(1,0); dfs2(1,0,1); for (i=1;i<n;i++) if (deep[e[i].x]<deep[e[i].y]) w[e[i].y]=e[i];else w[e[i].x]=e[i]; for (i=1;i<=n;i++) if (w[i].x) { w[i].x=i,O[w[i].v].push_back(w[i]); seg::Update(1,n,1,id[w[i].x],w[i].s); } for (i=1;i<=tot;i++) { root[i]=root[i-1]; for (auto tmp:O[i]) { root[i]=Update(1,n,root[i],id[tmp.x],tmp.c); if (id[tmp.x]+siz[tmp.x]<=n) root[i]=Update(1,n,root[i],id[tmp.x]+siz[tmp.x],-tmp.c); } } read(q); while (q--) { read(x),read(y),read(z); int l=0,r=seg::query(x,y)+1,mid; while (l+1<r) { mid=l+r>>1; if (check(mid,x,y,z)) l=mid; else r=mid; } printf("%lld\n",g[l]); } return 0; }
|