2019ICPC南昌邀请赛J题 Distance on the tree(主席树)

Descriptions:

\quad DSM(Data Structure Master) once learned about tree when he was preparing for NOIP(National Olympiad in Informatics in Provinces) in Senior High School. So when in Data Structure Class in College, he is always absent-minded about what the teacher says.

\quad The experienced and knowledgeable teacher had known about him even before the first class. However, she didn’t wish an informatics genius would destroy himself with idleness. After she knew that he was so interested in ACM(ACM International Collegiate Programming Contest), she finally made a plan to teach him to work hard in class, for knowledge is infinite.

\quad This day, the teacher teaches about trees." A tree with nn nodes, can be defined as a graph with only one connected component and no cycle. So it has exactly n − 1 n-1 n1 edges…" DSM is nearly asleep until he is questioned by teacher. " I have known you are called Data Structure Master in Graph Theory, so here is a problem. " " A tree with nn nodes, which is numbered from 1 1 1 to nn. Edge between each two adjacent vertexes u u u and v v v has a value w w w, you’re asked to answer the number of edge whose value is no more than k k k during the path between u u u and v v v."" If you can’t solve the problem during the break, we will call you DaShaMao(Foolish Idiot) later on."

\quad The problem seems quite easy for DSM. However, it can hardly be solved in a break. It’s such a disgrace if DSM can’t solve the problem. So during the break, he telephones you just for help. Can you save him for his dignity?

Input

\quad In the first line there are two integers n , m n,m n,m, represent the number of vertexes on the tree and queries ( 2 ≤ n ≤ 1 0 5 , 2 ≤ m ≤ 1 0 5 ) \left(2≤n≤10^{5},2≤m≤10^{5} \right) (2n105,2m105)
\quad The next n − 1 n-1 n1 lines, each line contains three integers u u u, v v v, w w w indicates there is an undirected edge between nodes u u u and v v v with value w w w. ( 2 ≤ u , v ≤ n , 1 ≤ w ≤ 1 0 9 ) \left(2≤u,v≤n,1≤w≤10^{9} \right) (2u,vn,1w109)

\quad The next mm lines, each line contains three integers u , v , k u,v,k u,v,k, be consistent with the problem given by the teacher above. ( 1 ≤ u , v ≤ n , 0 ≤ k ≤ 1 0 9 ) \left(1≤u,v≤n,0≤k≤10^{9} \right) (1u,vn,0k109)

Output

\quad For each query, just print a single line contains the number of edges which meet the condition.

样例输入1

3 3
1 3 2
2 3 7
1 3 0
1 2 4
1 2 7

样例输出1

0
1
2

样例输入2

5 2
1 2 1000000000
1 3 1000000000
2 4 1000000000
3 5 1000000000
2 3 1000000000
4 5 1000000000

样例输出2

2
4
\quad 题意就是 n n n个点, n − 1 n-1 n1条边,然后每条边都有自己的权值, m m m个询问,每个询问就是从一个点到另外一个点的路径上有多少满足权值不大于 k k k的边有多少条。
这个就是HDU4417的进阶版,加了一个LCA函数。

AC代码:

#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
using namespace std;
#define sd(n) scanf("%d",&n)
#define sdd(n,m) scanf("%d%d",&n,&m)
#define sddd(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define pd(n) printf("%d\n", n)
#define pc(n) printf("%c", n)
#define pdd(n,m) printf("%d %d", n, m)
#define pld(n) printf("%lld\n", n)
#define pldd(n,m) printf("%lld %lld\n", n, m)
#define sld(n) scanf("%lld",&n)
#define sldd(n,m) scanf("%lld%lld",&n,&m)
#define slddd(n,m,k) scanf("%lld%lld%lld",&n,&m,&k)
#define sf(n) scanf("%lf",&n)
#define sc(n) scanf("%c",&n)
#define sff(n,m) scanf("%lf%lf",&n,&m)
#define sfff(n,m,k) scanf("%lf%lf%lf",&n,&m,&k)
#define ss(str) scanf("%s",str)
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,a,n) for(int i=n;i>=a;i--)
#define mem(a,n) memset(a, n, sizeof(a))
#define debug(x) cout << #x << ": " << x << endl
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mod(x) ((x)%MOD)
#define gcd(a,b) __gcd(a,b)
#define lowbit(x) (x&-x)
typedef pair<int,int> PII;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int MOD = 1e9 + 7;
const double eps = 1e-9;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const int inf = 0x3f3f3f3f;
inline int read()
{
    int ret = 0, sgn = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9')
    {
        if(ch == '-')
            sgn = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9')
    {
        ret = ret*10 + ch - '0';
        ch = getchar();
    }
    return ret*sgn;
}
inline void Out(int a)    //Êä³öÍâ¹Ò
{
    if(a>9)
        Out(a/10);
    putchar(a%10+'0');
}

int qpow(int m, int k, int mod)
{
    int res = 1, t = m;
    while (k)
    {
        if (k&1)
            res = res * t % mod;
        t = t * t % mod;
        k >>= 1;
    }
    return res;
}

ll gcd(ll a,ll b)
{
    return b==0?a : gcd(b,a%b);
}

ll lcm(ll a,ll b)
{
    return a*b/gcd(a,b);
}

const int maxn = 1e5+7;
struct node
{
    int l;
    int r;
    int sum;
} T[maxn*40];
struct edge
{
    int to;
    int w;
};
int n,m,cnt,root[maxn],x,y,w,k,lg[maxn],fa[maxn][21],dep[maxn],vis[maxn];
vector<int> v;
vector<edge> G[maxn];
int getid(int x)
{
    return lower_bound(v.begin(),v.end(),x)-v.begin()+1;
}
void update(int l,int r,int &now,int pre,int pos)
{
    T[++cnt]=T[pre];
    T[cnt].sum++;
    now=cnt;
    if(l==r)
        return;
    int mid=(l+r)/2;
    if(pos<=mid)
        update(l,mid,T[now].l,T[pre].l,pos);
    else
        update(mid+1,r,T[now].r,T[pre].r,pos);
}
int query(int l,int r,int x,int y,int k)
{
    if(r<=k)
        return T[y].sum-T[x].sum;
    int mid=(l+r)/2;
    if(k<=mid)
        return query(l,mid,T[x].l,T[y].l,k);
    else
        return T[T[y].l].sum-T[T[x].l].sum+query(mid+1,r,T[x].r,T[y].r,k);
}
void dfs(int now,int last)
{
    dep[now]=dep[last]+1;
    fa[now][0]=last;
    for(int i=1; (1<<i)<=dep[now]; i++)
    {
        fa[now][i]=fa[fa[now][i-1]][i-1];
    }
    for(int i=0; i<G[now].size(); i++)
    {
        int t=G[now][i].to;
        if(t==last)
            continue;
        update(1,n,root[t],root[now],getid(G[now][i].w));
        dfs(t,now);
    }
}
int lca(int x,int y)
{
    if(dep[x]>dep[y])
        swap(x,y);
    while(dep[x]!=dep[y])
    {
        if(lg[dep[y]-dep[x]]-1>=0)
            y=fa[y][lg[dep[y]-dep[x]]-1];
        else
            y=fa[y][0];
    }
    if(x==y)
        return x;
    for(int i=lg[dep[y]]; i>=0; i--)
    {
        if(fa[x][i]!=fa[y][i])
        {
            x=fa[x][i];
            y=fa[y][i];
        }
    }
    return fa[x][0];
}
int main()
{
    scanf("%d %d",&n,&m);
    for(int i=1; i<=n; i++)
        lg[i]=lg[i-1]+(1<<lg[i-1]==i);
    for(int i=0; i<n-1; i++)
    {
        scanf("%d%d%d",&x,&y,&w);
        G[x].push_back({y,w});
        G[y].push_back({x,w});
        v.push_back(w);
    }
    sort(v.begin(),v.end());
    v.erase(unique(v.begin(),v.end()),v.end());
    dfs(1,0);
    for(int i=1; i<=m; i++)
    {
        scanf("%d %d %d",&x,&y,&k);
        int pos=upper_bound(v.begin(),v.end(),k)-v.begin();
        if(pos)
        {
            int ans=query(1,n,root[1],root[x],pos)+query(1,n,root[1],root[y],pos)-2*query(1,n,root[1],root[lca(x,y)],pos);
            printf("%d\n",ans);
        }
        else
            printf("0\n");
    }
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值