51nod1295 XOR key

链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1295

题意:中文题。

分析:裸的可持久化trie树,练习题。

代码:

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int N=50010;
const int MAX=1000000100;
const int mod=100000000;
const int MOD1=1000000007;
const int MOD2=1000000009;
const double EPS=0.00000001;
typedef long long ll;
const ll MOD=1000000007;
const int INF=1000000010;
const double pi=acos(-1.0);
typedef double db;
typedef unsigned long long ull;
int tot=0,ls[35*N],rs[35*N],root[N],sum[35*N];
int add(int x,int y) {
    int i,ret=++tot,now=ret;
    sum[now]=sum[x]+1;
    ls[now]=ls[x];rs[now]=rs[x];
    for (int i=30;i>=0;i--)
    if (y&(1<<i)) {
        rs[now]=++tot;
        now=tot;x=rs[x];
        sum[now]=sum[x]+1;
        ls[now]=ls[x];rs[now]=rs[x];
    } else {
        ls[now]=++tot;
        now=tot;x=ls[x];
        sum[now]=sum[x]+1;
        ls[now]=ls[x];rs[now]=rs[x];
    }
    return ret;
}
int getans(int x,int y,int z) {
    int i,ret=0;
    for (i=30;i>=0;i--)
    if ((1<<i)&z) {
        if (sum[ls[x]]>sum[ls[y]]) {
            ret+=1<<i;x=ls[x];y=ls[y];
        } else { x=rs[x];y=rs[y]; }
    } else {
        if (sum[rs[x]]>sum[rs[y]]) {
            ret+=1<<i;x=rs[x];y=rs[y];
        } else { x=ls[x];y=ls[y]; }
    }
    return ret;
}
int main()
{
    int i,n,q,l,r,x;
    scanf("%d%d", &n, &q);
    for (i=1;i<=n;i++) {
        scanf("%d", &x);
        root[i]=add(root[i-1],x);
    }
    while (q--) {
        scanf("%d%d%d", &x, &l, &r);l++;r++;
        printf("%d\n", getans(root[r],root[l-1],x));
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值