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. Solon rpc 之 SocketD 协议 - RPC鉴权模式

    Solon rpc 之 SocketD 协议系列 Solon rpc 之 SocketD 协议 - 概述 Solon rpc 之 SocketD 协议 - 消息上报模式 Solon rpc 之 Soc ...

  2. Tomcat配置上遇到的一些问题

    Tomcat启动:在bin目录下双击startup.bat文件就行. 访问:在浏览器输入http://localhost:8080 回车访问的是自己 的界面: http://othersip:8080 ...

  3. AvaloniaUI体验

    公司的原有的PC端WPF产品有跨平台需求,无奈微软自己的xamarin对wpf的支持当前尚未达到能支撑产品的成熟度,于是经过搜索,发现了一个号称用WPF实现跨平台的第三方图形库AvaloniaUI. ...

  4. 一次snapshot迁移引发的Hbase RIT(hbase2.1.0-cdh6.3.0)

    1. 问题起因 通过snapshot做跨集群数据同步时,在执行拷贝脚本里没有指定所有者及所有组,导致clone时没有权限,客户端卡死.master一直报错,经过一系列操作后,导致RIT异常. 2. 异 ...

  5. 【Vue】Vue开发环境搭建

    Vue前置学习环境 文章目录 Vue前置学习环境 IDE Node.js 调试环境 工程环境 小结 IDE WebStorm 官网下载:https://www.jetbrains.com/websto ...

  6. 【老孟Flutter】如何提高Flutter应用程序的性能

    首先 Flutter 是一个非常高性能的框架,因此大多时候不需要开发者做出特殊的处理,只需要避免常见的性能问题即可获得高性能的应用程序. 重建最小化原则 在调用 setState() 方法重建组件时, ...

  7. Flink可靠性的基石-checkpoint机制详细解析

    Checkpoint介绍 checkpoint机制是Flink可靠性的基石,可以保证Flink集群在某个算子因为某些原因(如 异常退出)出现故障时,能够将整个应用流图的状态恢复到故障之前的某一状态,保 ...

  8. 406 UDP协议是面向非连接的协议 Keep-Alive

    HTTP The Definitive Guide   Table 3-1. Common HTTP methods   Method Description Message body?   GET ...

  9. 线上nginx的一次“no live upstreams while connecting to upstream ”分析

    线上nginx的一次"no live upstreams while connecting to upstream "分析 线上nginx的一次"no live upst ...

  10. Git提交代码规范 而且规范的Git提交历史,还可以直接生成项目发版的CHANGELOG(semantic-release)

    Git提交代码规范 - 木之子梦之蝶 - 博客园 https://www.cnblogs.com/liumengdie/p/7885210.html Commit message 的格式 Git 每次 ...