Five Day Couple(字典树+区间异或某个数后的最大值)


题目描述

Mingming, a cute girl of ACM/ICPC team of Wuhan University, is alone since graduate from high school. Last year, she used a program to match boys and girls who took part in an active called Boy or Girl friend in five days.


She numbered n () boys from 1 to \(n\), by their date of birth, and given i-th boy a number () in almost random. (We do not mean that in your input is generated in random.). Then she numbered m () girls from 1 to m, and given i-th girl a number () in the same way.


Also, i-th girl said that she only wanted to be matched to a boy whose age is between , which means that she should only be matched to a boy numbered from  , ().


Mingming defined a rate R(i,j) to measure the score when the i-th boy and j-th girl matched. Where  where means bitwise exclusive or. The higher, the better.


Now, for every girl, Mingming wants to know the best matched boy, or her "Mr. Right" can be found while her . As this is the first stage of matching process and Mingming will change the result manually, two girls can have the same "Mr. Right".

输入描述:

 
 

The first line contains one number n.

The second line contains n integers, the i-th one is .

The third line contains an integer m.

Then followed by m lines, the j-th line contains three integers .

输出描述:

Output m lines, the i-th line contains one integer, which is the matching rate of i-th girl and her Mr. Right.
示例1

输入

4
19 19 8 10
2
1 1 4
5 1 4

输出

18
22


题意

先给一个长度为n的数组a[ ],接下来输入一个m,代表下面有m次输入,每次输入b,l,r,b表示一个数的大小,问b与a[l]、a[l+1]、...、a[r]中任意一个异或的最大值。


思路

考虑面向全局的异或,就是根据序列建一棵字典树,然后在Trie上查找最大值。

如果我们不进行节点的回收工作,那么节点的序号代表这个节点被创建的时间戳。

在我们查询的额时候,看一下某个节点的时间戳,就可以知道这个子树在某个时间之后是否进行过修改。

所以,对于每次查询,我们从区间的右端点所表示的根节点开始,每次只访问时间戳不小于区间左端点被创建的时间戳,实际所访问的就是这个区间的数所组成的Trie。

时间复杂度:O(32m);


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <set>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;

const int MAX = 1e5+100;
const int N = 5e6+100;

int n,m;
int a[MAX];
int root[MAX],nxt[N][3];
int tot;//表示时间戳

int newnode()
{
    memset(nxt[tot],-1,sizeof*nxt[tot]);
    return tot++;
}

void add(int u,int fa,int x)
{
    int now1=root[u];
    int now2=root[fa];
    for(int i=31;i>=0;i--)
    {
        //每次取二进制最后一位(倒着来)
        int d=((x>>i)&1);
        nxt[now1][d]=newnode();
        nxt[now1][!d]=nxt[now2][!d];
        now1=nxt[now1][d];
        now2=nxt[now2][d];
    }
}

int query(int l,int r,int x)
{
    int ans=0;
    int now1=root[r];
    int now2=root[l];
    for(int i=31;i>=0;i--)
    {
        int d=((x>>i)&1);
        //访问节点的时间戳要不小于区间左端点被创建的时间戳
        if(nxt[now1][!d]>nxt[now2][!d])
        {
            //这一位异或后值大小为1<<i
            ans+=(1<<i);
            d=!d;
        }
        now1=nxt[now1][d];
        now2=nxt[now2][d];
    }
    ans=max(ans,x^a[l]);
    return ans;
}

int main()
{
    tot=1;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
        root[i]=newnode();
        add(i,i-1,a[i]);
    }
    scanf("%d",&m);
    for(int i=0;i<m;i++)
    {
        int b,l,r;
        scanf("%d%d%d",&b,&l,&r);
        int ans=query(l,r,b);
        printf("%d\n",ans);
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值