HDU-4757【倍增+可持续化字典树】【树剖+可持续化字典树】

这道题要处理节点与父亲节点的前缀,而不是dfs的区间前缀。
当查询的时候对每一位只需要查询sum[x][!t]+sum[y][!t] > sum[lca][!t]就行了。
倍增

#pragma GCC optimize(2)
#pragma comment(linker, “/ STACK : 1024000000, 1024000000”)
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
int Case = 1;
int n, m, cc[maxn], cnt, head[maxn], dep[maxn];
int p[30][maxn];
struct node {
   
  int val, v, nex;
} CC[maxn << 1];
void add(int u, int v) {
   
  cnt++;
  CC[cnt].v = v;
  CC[cnt].nex = head[u];
  head[u] = cnt;
}
int root[maxn << 6];
void init() {
   
  cnt = 0;
  memset(head, 0, sizeof(head));
}
struct Trie {
   
  int nex[maxn << 6][2], tot, sum[maxn << 6][2];
  int newnode() {
   
    nex[tot][0] = nex[tot][1] = 0;
    return tot++;
  }
  void init() {
   
    tot = 1;
    nex[0][0] = nex[0][1] = 0;
  }
  int insert(int x, int id) {
   
    int t = newnode(), res = t;
    for (int i = 16; i >= 0; i--) {
   
      int tx = (1 << i) & x;
      tx = (tx >> i);
      sum[t][tx] = sum[id][tx] + 1;
      sum[t][!tx] = sum[id][!tx];
      nex[t][tx] = newnode();
      nex[t][!tx] = nex[id][!tx];
      t = nex[t][tx];
      id = nex[id][tx];
    }
    return res;
  }

  int query(int lca, int x, int y, int s) {
   
    int res = 0, ans = cc[lca] ^ s;
    lca = root[lca];
    for (int i = 16; i >= 0; i--) {
   
      int t = (1 << i) & s;
      t = (t >> i);
      if (sum[y][!t]
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值