zoukankan      html  css  js  c++  java
  • 树上主席树(边权查询)

    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-1n1 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 uu and 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^52n105,1m105)

    The next n-1n1 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^91u,vn,1w109)

    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)(1u,vn,0k109)

    Output

    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

    题意 : 给你一棵树,并告诉你树上的边权是多少,m 次查询,每次查询俩个结点间小于等于 k 的边权的个数

    思路分析:

      对要查询的数据的权值排个序,然后套个树剖的板子就可以解决

      主席树应该也是可以解决,网上看有人主席树过的

      将边权全部加到儿子节点上面,然后就是正常的树上主席树查询俩个点间小于K的边权的数量

      以前写过一道类似的题目

    代码示例:

    #define ll long long
    const int maxn = 2e5+5;
    const int mod = 1e9+7;
    const double eps = 1e-9;
    const double pi = acos(-1.0);
    const int inf = 0x3f3f3f3f;
    
    int n, m;
    int pre[maxn*3][3], ran[maxn*3];
    struct pp
    {
        int to, cost;
    };
    vector<pp>ve[maxn];
    int ss;
    int root[maxn], cnt;
    struct node
    {
        int l, r;
        int sum;
    }t[maxn*40];
    void init(){
        cnt = 2;
        root[1] = 1;
        t[1].l = t[1].r = t[1].sum = 0;
    }
    void update(int num, int &rt, int l, int r) {
        t[cnt++] = t[rt];
        rt = cnt-1;
        t[rt].sum++;
        
        if (l == r) return;
        int mid = (l+r)>>1;
        if (num <= mid) update(num, t[rt].l, l, mid);
        else update(num, t[rt].r, mid+1, r);
    }
    
    int grand[maxn][22], dep[maxn];
    int getlca(int a, int b){
        if (dep[a] < dep[b]) swap(a, b); //a zai xia
        
        for(int i = 20; i >= 0; i--){
            if (dep[a] > dep[b] && dep[grand[a][i]] >= dep[b]) {
                a = grand[a][i];
            }
        }
        if (a == b) return a;
        for(int i = 20; i >= 0; i--){
            if (grand[a][i] != grand[b][i]){
                a = grand[a][i];
                b = grand[b][i];
            }
        }
        return grand[a][0];
    }
    
    void dfs(int x, int fa){
        for(int i = 1; i <= 20; i++){
            grand[x][i] = grand[grand[x][i-1]][i-1];
        }
        
        for(int i = 0; i < ve[x].size(); i++){
            int to = ve[x][i].to;
            int cost = ve[x][i].cost;
            if (to == fa) continue;
            
            int num = lower_bound(ran+1, ran+ss, cost)-ran;
            root[to] = root[x];
            update(num, root[to], 1, n);
            grand[to][0] = x;
            dep[to] = dep[x]+1;
            dfs(to, x);
        }
            
    }
    
    int ans;
    void query(int a, int b, int c, int d, int k, int l, int r){
        if (r <= k) {
            ans += t[a].sum+t[b].sum-t[c].sum-t[d].sum;
            return;
        } 
        
        int mid = (l+r)>>1;
        query(t[a].l, t[b].l, t[c].l, t[d].l, k, l, mid);
        if (k > mid) query(t[a].r, t[b].r, t[c].r, t[d].r, k, mid+1, r);
    }
    
    int main() {
        //freopen("in.txt", "r", stdin);
        //freopen("out.txt", "w", stdout);
        
        cin >> n >> m;
        int u, v, w;
        for(int i = 1; i < n; i++){
            scanf("%d%d%d", &u, &v, &w);
            ve[u].push_back({v, w});
            ve[v].push_back({u, w});
            ran[i] = w;
        }    
        int c = n;
        for(int i = 1; i <= m; i++){
            scanf("%d%d%d", &pre[i][0], &pre[i][1], &pre[i][2]);
            if (pre[i][2] > 0) ran[c++] = pre[i][2];
        }
        sort(ran+1, ran+c);
        ss = unique(ran+1, ran+c)-ran;
        
        init();
        dep[1] = 1;
        dfs(1, 0);
        
        for(int i = 1; i <= m; i++){
            u = pre[i][0], v = pre[i][1], w = pre[i][2];
            int lca = getlca(u, v);
            
            ans = 0;
            if (w > 0) w = lower_bound(ran+1, ran+ss, w)-ran;
            if (w > 0) query(root[u], root[v], root[lca], root[lca], w, 1, n);
            printf("%d
    ",ans);
        }
        return 0;
    }
    

      

  • 相关阅读:
    英语:真正有效的英语学习心得,把英语当母语学习!(转载)
    《2010年年度总结》
    SQL游标使用
    千万数量级分页存储过程
    关于动态创建DOM元素的问题
    MVC3 “从客户端中检测到有潜在危险的 Request.QueryString或者Request.Form 值”问题解决
    记录Ally项目的点点滴滴(一)总结
    解决session丢失问题
    转载:我的外语学习历程(如何学会十门外语)
    C#经典问题总结一
  • 原文地址:https://www.cnblogs.com/ccut-ry/p/10806644.html
Copyright © 2011-2022 走看看