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 .
4 19 19 8 10 2 1 1 4 5 1 4
18 22
可持续化字典树:
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
const int maxn = 100000;
int n,m,a[maxn+5],root[maxn+5],sum[maxn*80+5];
int ch[maxn*40+5][2],sz;
void Insert(int last,int &v,int x)
{
v = ++sz;
int u = v;
sum[u] = sum[last]+1;
for(int i=30; i>=0; i--)
{
int c = (x>>i)&1;
ch[u][c^1] = ch[last][c^1], ch[u][c] = ++sz;
u = ch[u][c];
last = ch[last][c];
sum[u] = sum[last]+1;
}
}
int query(int l,int r,int x)
{
int y = 0;
for(int i=30; i>=0; i--)
{
int c = (x>>i)&1;
if(sum[ch[r][c^1]]-sum[ch[l][c^1]])
{
y|=(1<<i);
l=ch[l][c^1], r = ch[r][c^1];
}
else
{
l = ch[l][c], r = ch[r][c];
}
}
return y;
}
int main()
{
sz = 1;
scanf("%d",&n);
for(int i=1; i<=n; i++)
{
scanf("%d",&a[i]);
Insert(root[i-1],root[i],a[i]);
}
scanf("%d",&m);
for(int i=1; i<=m; i++)
{
int b,l,r,ans;
scanf("%d %d %d",&b,&l,&r);
ans = query(root[l-1],root[r],b);
printf("%d\n",ans);
}
return 0;
}