题目
思路
暴力
其实我们要做的很简单。——见到更小的就替换。
只需要注意到,从 a a a到 b b b,中间是否有 c c c作为落脚点,代价分别为
a dist ( a , c ) + a dist ( c , b ) a n d a dist ( a , c ) + c dist ( c , b ) a\operatorname{dist}(a,c)+a\operatorname{dist}(c,b)\\ and\\ a\operatorname{dist}(a,c)+c\operatorname{dist}(c,b) adist(a,c)+adist(c,b)andadist(a,c)+cdist(c,b)
所以遇到小的(也就是 c < a c<a c<a的情况),就将代价替换为小的。
暴力的代码如下。
int son[MaxN];
long long work(int s,int t){
son[s] = 0; if(s == t) return 0;
for(int i=t; fa[i] and i!=s; i=fa[i]) son[fa[i]] = i;
if(son[s] == 0) return -1;
int x = s; long long ans = 0;
for(int i=s; i!=t; i=son[i])
ans += (depth[son[i]]-depth[i])*(x=min(x,i));
return ans;
}
一条链
相当于是一个序列,要找左边第一个比自己小的数字。单调栈 的板题嘛。
然后呢?然后我们 倍增,用 f ( x , j ) f(x,j) f(x,j)表示, x x x往下跳了 2 j 2^j 2j次(每次都是跳到单调栈中求出来的值,也就是说, f ( x , 0 ) = f(x,0)= f(x,0)=左边第一个比自己小的数字); g ( x , j ) g(x,j) g(x,j)则是代价。转移则很简单了,
f ( x , j + 1 ) = f [ f ( x , j ) , j ] f(x,j+1)=f[f(x,j),j] f(x,j+1)=f[f(x,j),j]
g ( x , j + 1 ) = g ( x , j ) + g [ f ( x , j ) , j ] g(x,j+1)=g(x,j)+g[f(x,j),j] g(x,j+1)=g(x,j)+g[f(x,j),j]
然后就很简单了。暴力的倍增跳跃即可。
一棵树
你知道一条链怎么做,就套 链分治 就好了。
然后就没什么知识点了。时间复杂度 O ( m log 2 n ) \mathcal O(m\log^2n) O(mlog2n)。
代码
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
inline int readint(){
int a = 0; char c = getchar(), f = 1;
for(; c<'0' or c>'9'; c=getchar())
if(c == '-') f = -f;
for(; '0'<=c and c<='9'; c=getchar())
a = (a<<3)+(a<<1)+(c^48);
return a*f;
}
inline void writeint(long long x){
if(x < 0) putchar('-'), x = -x;
if(x > 9) writeint(x/10);
putchar((x%10)^48);
}
const int MaxN = 300005, LogN = 21;
int n, m, head[MaxN], cntEdge;
struct Edge{
int to, nxt, val;
Edge(int T=0,int N=0,int V=0){
to = T, nxt = N, val = V;
}
} edge[MaxN<<1];
void addEdge(int a,int b,int c){
edge[cntEdge] = Edge(b,head[a],c);
head[a] = cntEdge ++;
edge[cntEdge] = Edge(a,head[b],c);
head[b] = cntEdge ++;
}
void input(){
n = readint(), m = readint();
for(int i=1; i<=n; ++i) head[i] = -1;
for(int i=1,x,y; i<n; ++i){
x = readint(), y = readint();
addEdge(x,y,readint());
}
}
int fa[MaxN], son[MaxN];
long long depth[MaxN];
void getInfo(int x){
static int size[MaxN];
size[x] = 1, size[son[x] = 0] = 0;
for(int i=head[x]; ~i; i=edge[i].nxt)
if(edge[i].to != fa[x]){
fa[edge[i].to] = x;
depth[edge[i].to] = depth[x]+edge[i].val;
getInfo(edge[i].to);
size[x] += size[edge[i].to];
if(size[son[x]] < size[edge[i].to])
son[x] = edge[i].to;
}
}
int topChain[MaxN];
void chainSplit(int x,int tc){
topChain[x] = tc;
if(son[x] != 0) chainSplit(son[x],tc);
for(int i=head[x]; ~i; i=edge[i].nxt)
if(edge[i].to != son[x] and edge[i].to != fa[x])
chainSplit(edge[i].to,edge[i].to);
}
int jump[MaxN][LogN];
long long cost[MaxN][LogN];
void getStack(int x){
static vector<int> sta; sta.clear();
while(true){
while(not sta.empty() and sta.back() > x)
sta.pop_back();
if(not sta.empty()){
jump[x][0] = sta.back();
cost[x][0] = (depth[sta.back()]-depth[x])*x;
}
for(int j=0; j+1<LogN; ++j){
jump[x][j+1] = jump[jump[x][j]][j];
cost[x][j+1] = cost[x][j]+cost[jump[x][j]][j];
}
sta.push_back(x);
if(topChain[x] == x) break; // 重链头
x = fa[x];
}
}
long long work(int s,int t){
static vector<pair<int,int>> road; road.clear();
while(topChain[s] != topChain[t]){
if(depth[topChain[s]] > depth[topChain[t]])
return -1; // impossible
road.push_back(make_pair(topChain[t],t));
t = fa[topChain[t]]; // from first to second
}
if(depth[s] > depth[t]) return -1;
if(s != t) road.push_back(make_pair(s,t));
int minV = s; long long ans = 0;
while(not road.empty()){
auto it = road.back(); road.pop_back();
int x = it.first, y = it.second;
if(x != s) // 轻边
ans += minV*(depth[x]-depth[fa[x]]);
minV = min(minV,x); // 可能产生新的min值
for(int j=LogN-1; ~j; --j){
if(jump[x][j] == 0) continue; // jump out
if(depth[jump[x][j]] > depth[y])
continue; // too far (beneath the goal)
if(jump[x][j] >= minV){
ans += (depth[jump[x][j]]-depth[x])*minV;
x = jump[x][j]; // 向下跳跃
} // 此时的花费都是用minV,因为x<=minV
}
if(jump[x][0] != 0 and depth[jump[x][0]] <= depth[y]){
ans += (depth[jump[x][0]]-depth[x])*minV;
minV = x = jump[x][0];
} // 跳到一个比minV大的x,剩下的就可以用cost
for(int j=LogN-1; ~j; --j){
if(jump[x][j] == 0) continue;
if(depth[jump[x][j]] <= depth[y])
ans += cost[x][j], x = jump[x][j];
}
minV = min(minV,x); // 当前的x系最小的?
ans += (depth[y]-depth[x])*minV;
}
return ans;
}
void solve(){
getInfo(1), chainSplit(1,1);
for(int i=1; i<=n; ++i)
if(son[i] == 0)
getStack(i);
while(m --){
int one = readint();
writeint(work(one,readint()));
putchar('\n');
}
}
int main(){
input(), solve();
return 0;
}