I. Five Day Couple--“今日头条杯”首届湖北省大学程序设计竞赛(网络同步赛)

题目描述:链接点此

这套题的github地址(里面包含了数据,题解,现场排名):点此

链接:https://www.nowcoder.com/acm/contest/104/H
来源:牛客网

题目描述

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个数,然后m次询问,每次询问有三个数b,l,r,求在[l,r]范围内,和b异或值最大的值

异或最大一般用字典树

但是这个涉及区间询问,所以要中可持久化字典树

/*
    data:2018.04.27
    author:gsw
    link:https://www.nowcoder.com/acm/contest/104/H
    account:tonygsw
*/
#define ll long long
#define IO ios::sync_with_stdio(false);

#include<iostream>
#include<math.h>
#include<string.h>
#include<stdio.h>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn=1e5+5;

class Node{
    public:
        int cnt,ls,rs;
};
Node tr[maxn*40];
int root[maxn];int cnt;

int in(int pre,int x,int deep)
{
    int num=++cnt;
    tr[num]=tr[pre];
    tr[num].cnt=tr[pre].cnt+1;
    if(deep<0)return num;
    if(!((x>>deep)&1))tr[num].ls=in(tr[pre].ls,x,deep-1);
    else tr[num].rs=in(tr[pre].rs,x,deep-1);
    return num;
}
int query(int l,int r,int x,int deep)
{
    if(deep<0)return 0;
    if(!((x>>deep)&1))
    {
        if(tr[tr[r].rs].cnt>tr[tr[l].rs].cnt)return (1<<deep)+query(tr[l].rs,tr[r].rs,x,deep-1);
        else return query(tr[l].ls,tr[r].ls,x,deep-1);
    }
    else
    {
        if(tr[tr[r].ls].cnt>tr[tr[l].ls].cnt)return (1<<deep)+query(tr[l].ls,tr[r].ls,x,deep-1);
        else return query(tr[l].rs,tr[r].rs,x,deep-1);
    }
}


int main()
{
    int n,x,m,b,l,r;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&x);
        root[i]=in(root[i-1],x,29);
    }
    scanf("%d",&m);
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d%d",&b,&l,&r);
        printf("%d\n",query(root[l-1],root[r],b,29));
    }
}


猜你喜欢

转载自www.cnblogs.com/fantastic123/p/8964595.html
今日推荐