4571: [Scoi2016]美味
Time Limit: 30 Sec Memory Limit: 256 MBSubmit: 677 Solved: 364
[Submit][Status][Discuss]
Description
一家餐厅有 n 道菜,编号 1...n ,大家对第 i 道菜的评价值为 ai(1≤i≤n)。有 m 位顾客,第 i 位顾客的期
望值为 bi,而他的偏好值为 xi 。因此,第 i 位顾客认为第 j 道菜的美味度为 bi XOR (aj+xi),XOR 表示异或
运算。第 i 位顾客希望从这些菜中挑出他认为最美味的菜,即美味值最大的菜,但由于价格等因素,他只能从第
li 道到第 ri 道中选择。请你帮助他们找出最美味的菜。
Input
第1行,两个整数,n,m,表示菜品数和顾客数。
第2行,n个整数,a1,a2,...,an,表示每道菜的评价值。
第3至m+2行,每行4个整数,b,x,l,r,表示该位顾客的期望值,偏好值,和可以选择菜品区间。
1≤n≤2×10^5,0≤ai,bi,xi<10^5,1≤li≤ri≤n(1≤i≤m);1≤m≤10^5
Output
输出 m 行,每行 1 个整数,ymax ,表示该位顾客选择的最美味的菜的美味值。
Sample Input
4 4
1 2 3 4
1 4 1 4
2 3 2 3
3 2 3 3
4 1 2 4
1 2 3 4
1 4 1 4
2 3 2 3
3 2 3 3
4 1 2 4
Sample Output
9
7
6
7
7
6
7
没有x就随便水
有了x怎么做呐
主席树维护权值线段树
因为x的存在满足条件的数就构成了一段权值区间
所以权值线段树搞一搞就好了
#include<cmath>
#include<ctime>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<string>
#include<bitset>
#include<queue>
#include<map>
#include<set>
using namespace std;
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
void print(int x)
{if(x<0)putchar('-'),x=-x;if(x>=10)print(x/10);putchar(x%10+'0');}
const int N=200100,B=1<<18;
struct president_tree{int ls,rs,w;}tr[N*20];
int root[N];
int sz;
void insert(int &k,int x,int l,int r,int val)
{
k=++sz;
tr[k].w=tr[x].w+1;
if(l==r)return ;
int mid=(l+r)>>1;
tr[k].ls=tr[x].ls;tr[k].rs=tr[x].rs;
val<=mid?insert(tr[k].ls,tr[x].ls,l,mid,val):insert(tr[k].rs,tr[x].rs,mid+1,r,val);
}
int query(int l,int r,int x,int y,int a,int b)
{
if(l>=x&&r<=y)return tr[b].w-tr[a].w;
int mid=(l+r)>>1;
if(x>mid)return query(mid+1,r,x,y,tr[a].rs,tr[b].rs);
if(y<=mid)return query(l,mid,x,y,tr[a].ls,tr[b].ls);
return query(l,mid,x,y,tr[a].ls,tr[b].ls)+query(mid+1,r,x,y,tr[a].rs,tr[b].rs);
}
int main()
{
int n=read(),m=read();
register int i,b,x,l,r,res;
for(i=1;i<=n;++i)x=read(),insert(root[i],root[i-1],0,B,x);//cout<<B/2<<endl;
while(m--)
{
b=read();x=read();l=read();r=read();
res=0;
for(i=17;~i;i--)
{
if((b&(1<<i))==0)res|=(1<<i);
if((res|((1<<i)-1))-x<0)res^=(1<<i);
else if(!query(0,B,max(res-x,0),(res|((1<<i)-1))-x,root[l-1],root[r]))
res^=(1<<i);
}
print(res^b);puts("");
}
return 0;
}