树上的函数式线段树, 查询那步自己写的是logn*logn的办法,可能数据比较弱的原因跑的还挺快的,可以在线段树上逼近降到logn
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
#include <cstring>
#include <stack>
#include <cctype>
#include <utility>
#include <map>
#include <string>
#include <climits>
#include <set>
#include <string>
#include <sstream>
#include <utility>
#include <ctime>
#include <bitset>
//#pragma comment(linker, "/STACK:102400000,102400000")
using std::priority_queue;
using std::vector;
using std::swap;
using std::stack;
using std::sort;
using std::max;
using std::min;
using std::pair;
using std::map;
using std::string;
using std::cin;
using std::cout;
using std::set;
using std::queue;
using std::string;
using std::stringstream;
using std::make_pair;
using std::getline;
using std::greater;
using std::endl;
using std::multimap;
using std::deque;
using std::unique;
using std::lower_bound;
using std::random_shuffle;
using std::bitset;
using std::upper_bound;
using std::multiset;
typedef long long LL;
typedef unsigned long long ULL;
typedef unsigned UN;
typedef pair<int, int> PAIR;
typedef multimap<int, int> MMAP;
typedef LL TY;
typedef long double LF;
const int MAXN(1500010);
const int MAXM(50010);
const int MAXE(150010);
const int MAXK(6);
const int HSIZE(13131);
const int SIGMA_SIZE(4);
const int MAXH(20);
const int INFI((INT_MAX-1) >> 1);
const ULL BASE(31);
const LL LIM(1e13);
const int INV(-10000);
const int MOD(31313);
const double EPS(1e-7);
const LF PI(acos(-1.0));
template<typename T> inline void checkmax(T &a, T b){if(b > a) a = b;}
template<typename T> inline void checkmin(T &a, T b){if(b < a) a = b;}
template<typename T> inline T ABS(const T &a){return a < 0? -a: a;}
struct EDGE
{
int v, w, next;
} edge[200010];
int first[100010], rear;
void init(int n)
{
rear = 0;
memset(first, -1, sizeof(first[0])*(n+1));
}
void insert(int u, int v, int w)
{
edge[rear].w = w;
edge[rear].v = v;
edge[rear].next = first[u];
first[u] = rear++;
}
struct LCA
{
int dep[200010], E[200010], pos[100010], dp[19][200010], back;
void dfs(int u, int d, int f)
{
E[++back] = u;
dep[back] = d;
pos[u] = back;
for(int i = first[u]; ~i; i = edge[i].next)
if(edge[i].v != f)
{
dfs(edge[i].v, d+1, u);
E[++back] = u;
dep[back] = d;
}
}
int cmp(int a, int b){return dep[a] < dep[b]? a: b;}
void init()
{
back = 0;
dfs(1, 0, 0);
for(int i = 1; i <= back; ++i) dp[0][i] = i;
for(int i = 1; (1 << i) <= back; ++i)
for(int j = 1; j+(1 << i)-1 <= back; ++j)
dp[i][j] = cmp(dp[i-1][j], dp[i-1][j+(1 << (i-1))]);
}
int query(int a, int b)
{
a = pos[a];
b = pos[b];
if(a > b) swap(a, b);
int len = b-a+1, temp = 0;
while((1 << temp) <= len) ++temp;
--temp;
return E[cmp(dp[temp][a], dp[temp][b-(1 << temp)+1])];
}
} lca;
int ls[MAXN], rs[MAXN], sz[MAXN];
LL sum[MAXN];
int root[100010];
int back;
void updata(int l, int r, int val, int prt, int &rt)
{
rt = back++;
ls[rt] = ls[prt];
rs[rt] = rs[prt];
sz[rt] = sz[prt]+1;
sum[rt] = sum[prt]+val;
if(l == r) return;
int m = (l+r) >> 1;
if(val <= m) updata(l, m, val, ls[prt], ls[rt]);
else updata(m+1, r, val, rs[prt], rs[rt]);
}
LL query1(int l, int r, LL val, int rt) //找 <= val的所有边的容量和
{
if(r <= val) return sum[rt];
int m = (l+r) >> 1;
LL ret = query1(l, m, val, ls[rt]);
if(val > m) ret += query1(m+1, r, val, rs[rt]);
return ret;
}
int query2(int l, int r, LL val, int rt) //找 <= val的边的条数
{
if(r <= val) return sz[rt];
int m = (l+r) >> 1;
int ret = query2(l, m, val, ls[rt]);
if(val > m) ret += query2(m+1, r, val, rs[rt]);
return ret;
}
int query3(int l, int r, int prt, int lrt, int rrt) //找容量最小边
{
if(l == r) return l;
int ts = sz[ls[lrt]]+sz[ls[rrt]]-sz[ls[prt]]*2;
int m = (l+r) >> 1;
if(ts) return query3(l, m, ls[prt], ls[lrt], ls[rrt]);
else return query3(m+1, r, rs[prt], rs[lrt], rs[rrt]);
}
void dfs(int u, int w, int f)
{
if(f) updata(0, 10000, w, root[f], root[u]);
for(int i = first[u]; ~i; i = edge[i].next)
if(edge[i].v != f)
dfs(edge[i].v, edge[i].w, u);
}
int main()
{
int TC, n_case(0);
scanf("%d", &TC);
while(TC--)
{
int n, m;
scanf("%d%d", &n, &m);
init(n);
int u, v;
LL w;
for(int i = 1; i < n; ++i)
{
scanf("%d%d%I64d", &u, &v, &w);
insert(u, v, w);
insert(v, u, w);
}
lca.init();
root[1] = 0;
ls[0] = rs[0] = 0;
sum[0] = sz[0] = 0;
back = 1;
dfs(1, 0, 0);
LL a, b;
printf("Case #%d:\n", ++n_case);
for(int i = 0; i < m; ++i)
{
scanf("%d%d%I64d%I64d%I64d", &u, &v, &w, &a, &b);
int lc = lca.query(u, v);
if(a <= b)
{
printf("%I64d\n", query3(0, 10000, root[lc], root[u], root[v])+(LL)w/a);
continue;
}
bool flag(true);
LL x = -1;
if(w >= a)
{
x = (w-a)/b+query3(0, 10000, root[lc], root[u], root[v])+1;
LL cost = ((LL)query2(0, 10000, x, root[u])+query2(0, 10000, x, root[v])-query2(0, 10000, x, root[lc])*2)*x-
(query1(0, 10000, x, root[u])+query1(0, 10000, x, root[v])-query1(0, 10000, x, root[lc])*2);
cost *= b;
if(cost > w) flag = false;
}
if(flag)
{
LL l = x, r = query3(0, 10000, root[lc], root[u], root[v])+(LL)w/b+1;
while(l < r)
{
LL m = (l+r) >> 1;
LL cost = ((LL)query2(0, 10000, m, root[u])+query2(0, 10000, m, root[v])-query2(0, 10000, m, root[lc])*2)*m-
(query1(0, 10000, m, root[u])+query1(0, 10000, m, root[v])-query1(0, 10000, m, root[lc])*2);
cost *= b;
if(cost <= w) l = m+1;
else r = m;
}
--l;
printf("%I64d\n", l);
}
else
printf("%I64d\n", x);
}
}
return 0;
}