1. 程式人生 > >首屆湖北省大學程式設計競賽(網路同步賽)

首屆湖北省大學程式設計競賽(網路同步賽)

時間限制:C/C++ 1秒,其他語言2秒
空間限制:C/C++ 262144K,其他語言524288K
64bit IO Format: %lld

題目描述

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.
#include <iostream>
#include <stdio.h>
using namespace std;
const int maxn = 1e5+55;
int root[maxn],R[maxn*34],L[maxn*34],num[maxn*34];
int tot;

void init()
{
	root[0]=R[0]=L[0]=num[0]=0;
	tot=0;
}


void updata(int now,int pre,int x)
{
	R[now]=R[pre];
	L[now]=L[pre];
	for(int i=31;i>=0;i--){
		int to = x&(1<<i);
		if(to){
			R[now]=++tot;
			L[tot]=L[R[pre]];
			R[tot]=R[R[pre]];
			num[tot]=num[R[pre]]+1;
			now=R[now];
			pre=R[pre];
		}else{
			L[now]=++tot;
			L[tot]=L[L[pre]];
			R[tot]=R[L[pre]];
			num[tot]=num[L[pre]]+1;
			now=L[now];
			pre=L[pre];
		}

	}

}

int query(int now ,int pre,int x)
{
	int ans = 0;
	for(int i=31;i>=0;i--){
		int to  = x&(1<<i);
		if(!to){
			if(num[R[now]]-num[R[pre]]){
				ans|=(1<<i);
				now=R[now];
				pre=R[pre];
			}else{
				now=L[now];
				pre=L[pre];
			}
		}else{
			if(num[L[now]]-num[L[pre]]){
				ans|=(1<<i);
				now=L[now];
				pre=L[pre];
			}else{
				now=R[now];
				pre=R[pre];
			}

		}
	}
	return ans ;
}
int main()
{
	int n;
	scanf("%d",&n);
	init();
	for(int i=1;i<=n;i++){
		int x;scanf("%d",&x);
		root[i]=++tot;
		updata(root[i],root[i-1],x);
	}
	int q;scanf("%d",&q);
	for(int i=1;i<=q;i++){
		int l,r,x;
		scanf("%d %d %d",&x,&l,&r);
		printf("%d\n",query(root[l-1],root[r],x));

	}
}