zoukankan      html  css  js  c++  java
  • 2017ACM/ICPC广西邀请赛 K- Query on A Tree trie树合并

    Query on A Tree

    Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)


    Problem Description
    Monkey A lives on a tree, he always plays on this tree.

    One day, monkey A learned about one of the bit-operations, xor. He was keen of this interesting operation and wanted to practise it at once.

    Monkey A gave a value to each node on the tree. And he was curious about a problem.

    The problem is how large the xor result of number x and one node value of label y can be, when giving you a non-negative integer x and a node label u indicates that node y is in the subtree whose root is u(y can be equal to u).

    Can you help him?
     
    Input
    There are no more than 6 test cases.

    For each test case there are two positive integers n and q, indicate that the tree has n nodes and you need to answer q queries.

    Then two lines follow.

    The first line contains n non-negative integers V1,V2,,Vn, indicating the value of node i.

    The second line contains n-1 non-negative integers F1,F2,Fn1Fi means the father of node i+1.

    And then q lines follow.

    In the i-th line, there are two integers u and x, indicating that the node you pick should be in the subtree of u, and x has been described in the problem.

    2n,q105

    0Vi109

    1Fin, the root of the tree is node 1.

    1un,0x109
     
    Output
    For each query, just print an integer in a line indicating the largest result.
     
    Sample Input
    2 2 1 2 1 1 3 2 1
     
    Sample Output
    2 3

    题解:

      每个数存在各自trie树里边,n个点这是棵树,再从底向上tri树合并起来

      查询就是查询一颗合并后的trie树,利用从高位到低位,贪心取

    #include <bits/stdc++.h>
    inline int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;}
    
    using namespace std;
    
    #define LL long long
    const int N = 2e5;
    
    vector<int > G[N];
    int n, q, x, u, a[N];
    int ch[N*45][2], root[N],sz;
    
    void inserts(int u,int x) {
        root[u] = ++sz;
        int tmp = sz;
        int y = sz;
        for(int i = 30; i >= 0; --i) {
            int tmps = (x>>i)&1;
            if(!ch[y][tmps]) ch[y][tmps] = ++sz;
            y = ch[y][tmps];
        }
    }
    int merges(int u,int to) {
        if(u == 0) return to;
        if(to == 0) return u;
        int t = ++sz;
        ch[t][0] = merges(ch[u][0],ch[to][0]);
        ch[t][1] = merges(ch[u][1],ch[to][1]);
        return t;
    }
    void dfs(int u) {
        inserts(u,a[u]);
        for(auto to:G[u]) {
            dfs(to);
           root[u] =  merges(root[u],root[to]);
        }
    }
    LL query(int u,int x) {
        int y = root[u];
        LL ret = 0;
        for(int i = 30; i >= 0; --i) {
            int tmps = (x>>i)&1;
            if(ch[y][tmps^1]) ret += (1<<i),y = ch[y][tmps^1];
            else y = ch[y][tmps];
        }
        return ret;
    }
    void init() {
        for(int i = 0; i <= n; ++i) root[i] = 0,G[i].clear();
        sz = 0;
        memset(ch,0,sizeof(ch));
    }
    int main( int argc , char * argv[] ){
        while(scanf("%d%d",&n,&q)!=EOF) {
            for(int i = 1; i <= n; ++i) scanf("%d",&a[i]);
            init();
            for(int i = 2; i <= n; ++i) {
                scanf("%d",&x);
                G[x].push_back(i);
            }
            dfs(1);
            for(int i = 1; i <= q; ++i) {
                scanf("%d%d",&u,&x);
                printf("%lld
    ",query(u,x));
            }
        }
        return 0;
    }
  • 相关阅读:
    Oracle 循环语句
    IDEA---SpringBoot同一个项目多端口启动
    Maven引入oracle驱动包
    Linux安装 PostgreSQL
    Oracle备份及备份策略
    Oracle优化的几个简单步骤
    Oracle RMAN备份策略
    常见的几种索引扫描类型
    插槽内容
    分布式系统session同步解决方案
  • 原文地址:https://www.cnblogs.com/zxhl/p/7459240.html
Copyright © 2011-2022 走看看