【2019年ICPC南昌网络赛】Distance on the tree【DFS+线段树合并(可持久化线段树)】

题目链接


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.

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.

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-1n−1 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 11 to nn. Edge between each two adjacent vertexes uuand vv has a value w, you're asked to answer the number of edge whose value is no more than kk during the path between uu and vv."" If you can't solve the problem during the break, we will call you DaShaMao(Foolish Idiot) later on."

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

In the first line there are two integers n,mn,m, represent the number of vertexes on the tree and queries(2 \le n \le 10^5,1 \le m \le 10^52≤n≤105,1≤m≤105)

The next n-1n−1 lines, each line contains three integers u,v,wu,v,w, indicates there is an undirected edge between nodes uu and vv with value ww. (1 \le u,v \le n,1 \le w \le 10^91≤u,v≤n,1≤w≤109)

The next mm lines, each line contains three integers u,v,ku,v,k , be consistent with the problem given by the teacher above. (1 \le u,v \le n,0 \le k \le 10^9)(1≤u,v≤n,0≤k≤109)

Output

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


  题意:有N-1条边,构成u - v边权为w的这样子的组成的一棵树,然后有Q次询问,问的是u - v这条链上小于等于W的值的数量是多少。

  思路:没必要再去离散化了,直接去将右区间开到1e9即可了,然后就是子节点的树合并父亲节点的树,并且还要再加上一个自己的到父节点的边权的值进去,普通的可持久化线段树思维。


#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define efs 1e-8
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN = 1e5 + 7, _UP = 1e9;
int N, Q, head[maxN], cnt, deep[maxN], fa[maxN][25], root[maxN], tot;
struct Eddge
{
    int nex, to, val;
    Eddge(int a=-1, int b=0, int c=0):nex(a), to(b), val(c) {}
}edge[maxN<<1];
inline void addEddge(int u, int v, int val)
{
    edge[cnt] = Eddge(head[u], v, val);
    head[u] = cnt++;
}
inline void _add(int u, int v, int val) { addEddge(u, v, val); addEddge(v, u, val); }
int tree[40 * maxN], lc[40 * maxN], rc[40 * maxN];
inline void pushup(int rt) { tree[rt] = tree[lc[rt]] + tree[rc[rt]]; }
inline void insert(int &rt, int l, int r, int qx)
{
    if(!rt) rt = ++tot;
    tree[rt]++;
    if(l == r) return;
    int mid = HalF;
    if(qx <= mid) insert(lc[rt], l, mid, qx);
    else insert(rc[rt], mid + 1, r, qx);
    pushup(rt);
}
inline void Merge(int rt, int old, int l, int r)
{
    if(l == r) { tree[rt] = tree[rt] + tree[old]; return; }
    int mid = HalF;
    if(lc[rt] && lc[old]) Merge(lc[rt], lc[old], l, mid);
    else if(lc[old]) lc[rt] = lc[old];
    if(rc[rt] && rc[old]) Merge(rc[rt], rc[old], mid + 1, r);
    else if(rc[old]) rc[rt] = rc[old];
    pushup(rt);
}
inline int Query(int rt, int l, int r, int ql, int qr)
{
    if(!rt) return 0;
    if(ql <= l && qr >= r) return tree[rt];
    int mid = HalF;
    if(qr <= mid) return Query(lc[rt], l, mid, ql, qr);
    else if(ql > mid) return Query(rc[rt], mid + 1, r, ql, qr);
    else return Query(lc[rt], l, mid, ql, qr) + Query(rc[rt], mid + 1, r, ql, qr);
}
inline void dfs(int u, int father, int depth)
{
    deep[u] = depth;
    fa[u][0] = father;
    Merge(root[u], root[father], 0, _UP);
    for(int i=head[u], v, c; ~i; i=edge[i].nex)
    {
        v = edge[i].to; c = edge[i].val;
        if(v == father) continue;
        insert(root[v], 0, _UP, c);
        dfs(v, u, depth + 1);
    }
}
inline void pre_LCA()
{
    for(int j=0; (1<<(j + 1)) < N; j++)
    {
        for(int i=1; i<=N; i++)
        {
            if(fa[i][j] < 0) fa[i][j + 1] = -1;
            else fa[i][j + 1] = fa[fa[i][j]][j];
        }
    }
}
int LCA(int u, int v)
{
    if(deep[u] > deep[v]) swap(u, v);
    int tmp = deep[v] - deep[u];
    for(int i=0; (1<<i) <= tmp; i++) if( (tmp >> i) & 1) v = fa[v][i];
    if(u == v) return u;
    for(int i=log2(1. * N); i>=0; i--)
    {
        if(fa[u][i] != fa[v][i])
        {
            u = fa[u][i];
            v = fa[v][i];
        }
    }
    return fa[u][0];
}
inline void init()
{
    cnt = tot = 0;
    memset(head, -1, sizeof(head));
    memset(fa, -1, sizeof(fa));
    memset(tree, 0, sizeof(tree));
    memset(lc, 0, sizeof(lc));
    memset(rc, 0, sizeof(rc));
    for(int i=1; i<=N; i++) root[i] = 0;
}
int main()
{
    scanf("%d%d", &N, &Q);
    init();
    for(int i=1, u, v, w; i<N; i++)
    {
        scanf("%d%d%d", &u, &v, &w);
        _add(u, v, w);
    }
    dfs(1, 1, 0);
    pre_LCA();
    int u, v, w, ff;
    while(Q--)
    {
        scanf("%d%d%d", &u, &v, &w);
        ff = LCA(u, v);
        printf("%d\n", Query(root[u], 0, _UP, 0, w) + Query(root[v], 0, _UP, 0, w) - 2 * Query(root[ff], 0, _UP, 0, w));
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wuliwuliii

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值