HDU5589 Tree

题意:

给出一棵\(N\)个点的树,每条边有边权,每次询问下标为\([L,R]\)区间内的点能选出多少点对,点对之间的路径上的边权异或和大于\(M\)

题解:

对于两点\(u,v\)之间的路径上的边权的异或和,可以转化为根到点\(u\)的路径上异或和与根到点\(v\)的路径上异或和的异或和,所以可以与处理出根到各个点的路径边权异或和

接下来就变成了一个序列上的问题了,每次询问区间\([L,R]\)之间的点有多少对的异或和大于\(M\)

然后就可以用莫队来处理,为了能快速加值和删值还有查询,而且是异或的操作,这里选择用\(01\)字典树来做

//#pragma GCC optimize("O3")
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<bits/stdc++.h>
using namespace std;
function<void(void)> ____ = [](){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);};
const int MAXN = 5e4+7;
typedef long long int LL;
int n,m,q,w[MAXN];
LL ret[MAXN],ans;
struct Trie{
int tot,ch[MAXN<<4][2],cnt[MAXN<<4];
void clear(){
for(int i = 1; i <= tot; i++) ch[i][0] = ch[i][1] = cnt[i] = 0;
tot = 1;
}
void insert(int x, int now = 1){
for(int i = 19; i >= 0; i--){
if(!ch[now][(x>>i)&1]) ch[now][(x>>i)&1] = ++tot;
cnt[now = ch[now][(x>>i)&1]]++;
}
}
void erase(int x, int now = 1){
for(int i = 19; i >= 0; i--) cnt[now = ch[now][(x>>i)&1]]--;
}
int query(int x){
int ret = 0, now = 1;
for(int i = 19; i >= 0 and now; i--){
int bm = ((m>>i)&1);
int bx = ((x>>i)&1);
if(!bm) ret += cnt[ch[now][bx^1]], now = ch[now][bx];
else now = ch[now][bx^1];
}
return ret;
}
}trie;
struct Query{ int l, r, id; } Q[MAXN];
vector<pair<int,int>> G[MAXN];
void dfs(int u, int par){
for(auto e : G[u]) if(e.first!=par){
w[e.first] = w[u] ^ e.second;
dfs(e.first,u);
}
}
void inc(int x){
ans += trie.query(x);
trie.insert(x);
}
void dec(int x){
trie.erase(x);
ans -= trie.query(x);
}
void solve(){
for(int i = 1; i <= n; i++) G[i].clear();
for(int i = 1; i < n; i++){
int u, v, wt;
scanf("%d %d %d",&u,&v,&wt);
G[u].push_back(make_pair(v,wt)); G[v].push_back(make_pair(u,wt));
}
dfs(1,0); trie.clear();
for(int i = 1; i <= q; i++) scanf("%d %d",&Q[i].l,&Q[i].r), Q[i].id = i;
int sqt = sqrt(n);
sort(Q+1,Q+1+q,[&sqt](const Query &lhs, const Query &rhs){
return lhs.l / sqt == rhs.l / sqt ? lhs.r < rhs.r : lhs.l / sqt < rhs.l / sqt;
});
int L = 1, R = 0;
ans = 0;
for(int i = 1; i <= q; i++){
while(L>Q[i].l) inc(w[--L]);
while(R<Q[i].r) inc(w[++R]);
while(L<Q[i].l) dec(w[L++]);
while(R>Q[i].r) dec(w[R--]);
ret[Q[i].id] = ans;
}
for(int i = 1; i <= q; i++) printf("%I64d\n",ret[i]);
}
int main(){
while(scanf("%d %d %d",&n,&m,&q)!=EOF) solve();
return 0;
}

HDU5589 Tree【分块 01字典树】的更多相关文章

  1. HDU6191 Query on A Tre【dsu on tree + 01字典树】

    Query on A Tree Problem Description Monkey A lives on a tree, he always plays on this tree. One day, ...

  2. HDU 6191 2017ACM/ICPC广西邀请赛 J Query on A Tree 可持久化01字典树+dfs序

    题意 给一颗\(n\)个节点的带点权的树,以\(1\)为根节点,\(q\)次询问,每次询问给出2个数\(u\),\(x\),求\(u\)的子树中的点上的值与\(x\)异或的值最大为多少 分析 先dfs ...

  3. HDU6191 Query on A Tree (01字典树+启发式合并)

    题意: 给你一棵1e5的有根树,每个节点有点权,1e5个询问(u,x),问你子树u中与x异或最大的值是多少 思路: 自下而上启发式合并01字典树,注意合并时清空trie 线段树.字典树这种结构确定的数 ...

  4. HDU6191(01字典树启发式合并)

    Query on A Tree Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Othe ...

  5. Chip Factory(01字典树)

    Chip Factory http://acm.hdu.edu.cn/showproblem.php?pid=5536 Time Limit: 18000/9000 MS (Java/Others)  ...

  6. 2014百度之星资格赛—— Xor Sum(01字典树)

    Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) Total ...

  7. AcWing:144. 最长异或值路径(dfs + 01字典树)

    给定一个树,树上的边都具有权值. 树中一条路径的异或长度被定义为路径上所有边的权值的异或和: ⊕ 为异或符号. 给定上述的具有n个节点的树,你能找到异或长度最大的路径吗? 输入格式 第一行包含整数n, ...

  8. AcWing:143. 最大异或对(01字典树 + 位运算 + 异或性质)

    在给定的N个整数A1,A2……ANA1,A2……AN中选出两个进行xor(异或)运算,得到的结果最大是多少? 输入格式 第一行输入一个整数N. 第二行输入N个整数A1A1-ANAN. 输出格式 输出一 ...

  9. Codeforces 979 D. Kuro and GCD and XOR and SUM(异或和,01字典树)

    Codeforces 979 D. Kuro and GCD and XOR and SUM 题目大意:有两种操作:①给一个数v,加入数组a中②给出三个数x,k,s:从当前数组a中找出一个数u满足 u ...

随机推荐

  1. thinkphp redis实现文章点赞功能并同步入mysql

    <?php namespace app\common\controller; use think\App; use think\facade\Cache; use think\facade\Db ...

  2. 【Linux】ABRT has detected 1 problem(s). For more info run: abrt-cli list --since 1548988705

    ------------------------------------------------------------------------------------------------- | ...

  3. Ubuntu下修改缺省dash shell为bash shell

    Debian和Ubuntu下缺省使用的是shell是dash,而不是bash.从/bin/sh软连接的指向可以看出这点. 这是一个不同于bash的shell,它主要是为了执行脚本而出现,而不是交互,它 ...

  4. logging模块简单用法

    logging模块功能比较多,但一般情况下使用其简单功能就已经足够了. 最简单的用法如下: import logging logging.baiscConfig(level=logging.DEBUG ...

  5. linux下的命令自动补齐增强

    linux 7 下 安装 bash-completion 可以实现命令的参数的自动补齐

  6. centos7下 开启/关闭/查看firewall运行状态命令

    1.开启防火墙:systemctl start firewalld.service [root@localhost bin]# systemctl start firewalld.service [r ...

  7. Core3.1 微信v3 JSAPI支付

    1.前言 "小魏呀,这个微信支付还要多久?","快了快了老板,就等着最后一步了...","搞快点哈,就等着上线呢","...... ...

  8. RocketMQ在linx安装及其有关问题解决

    Linx安装和使用: rocketmq官网:http://rocketmq.apache.org/ 首先安装JDK(推荐使用JDK1.8),并配置环境变量 下载rocketmq压碎包并解压到指定目录 ...

  9. EasyExcel基本使用

    EasyExcel基本使用 一.应用场景 1.数据导入:减轻录入工作量 2.数据导出:统计信息归档 3.数据传输:异构系统之间数据传输 二.简介 Java领域解析.生成Excel比较有名的框架有Apa ...

  10. gRPC Motivation and Design Principles | gRPC https://grpc.io/blog/principles/

    gRPC Motivation and Design Principles | gRPC https://grpc.io/blog/principles/