很久以前就聽說這個大名鼎鼎的東西(暴力)了,現在纔去寫。其實很簡單,對每一維排序,從左到右掃,維護一個bitset表示這位之前的某個點是否出現(即出現則該位爲1否則0),查詢某個數時則在每一維的排序完數組二分找到最右邊<=這個數這一維權值的位置,取出該位的bitset,將每一維的bitset與起來即可得到答案,每次查詢n/w,但這樣空間要維數*n^2/w,因此可以分塊增加查找時的一些時間換空間。
鏈接:五維偏序
http://hihocoder.com/problemset/problem/1236
#include<bits/stdc++.h>
#define Bst bitset<50010>
using namespace std;
const int N=50010,bsz=250;
template<class T>
void rd(T &x)
{
char c=getchar();x=0;bool f=0;
while(!isdigit(c))f|=(c=='-'),c=getchar();
while(isdigit(c))x=x*10+c-48,c=getchar();
if(f)x=-x;
}
struct gg{
int a[5],id;
void in()
{rd(a[0]),rd(a[1]),rd(a[2]),rd(a[3]),rd(a[4]);}
}nd[N];
int n,m,bg[260],blk,cmp_id,mp[5][N],wh[5][N],lm[260],rm[260],bl[N];
Bst pre[5][260],bst;
bool cmp(gg x,gg y)
{return x.a[cmp_id]<y.a[cmp_id];}
Bst ask(int id,int x)
{
static Bst res;res.reset();
int ps=upper_bound(mp[id]+1,mp[id]+n+1,x)-mp[id],tmp;
while(mp[id][ps]>x||ps>n)--ps;
if(!ps)return res;
tmp=bl[ps]-1;
if(tmp!=0)res|=pre[id][tmp];
for(int i=rm[tmp]+1;i<=ps;i++)res.set(wh[id][i]);
return res;
}
int main()
{
int T,ans,Q;
rd(T);
while(T--)
{
rd(n),rd(m),ans=0;
for(int i=1;i<=n;i++)
nd[i].in(),nd[i].id=i;
for(int id=0;id<5;id++)
{
cmp_id=id;
sort(nd+1,nd+n+1,cmp);
for(int i=1;i<=n;i++)
mp[id][i]=nd[i].a[id],wh[id][i]=nd[i].id;
bst.reset();
for(int i=1,j=1;i<=n;i+=bsz,j++)
{
for(int k=0;k<bsz&&i+k<=n;k++)
bst.set(wh[id][i+k]),bl[i+k]=j;
pre[id][j]=bst,lm[j]=i,rm[j]=min(i+bsz-1,n);
}
}
gg tmp;int ps;
rd(Q);
while(Q--)
{
tmp.in();
bst.set();
for(int i=0;i<5;i++)
bst&=ask(i,tmp.a[i]^ans);
printf("%d\n",ans=bst.count());
}
}
}