# hihocoder1236(2015长春网赛J题) Scores(bitset && 分块)

#pragma warning(disable:4996)
#include<algorithm>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<queue>
#include <list>
#include<time.h>
#include<bitset>
using namespace std;

#define maxn 50005

struct Node
{
int val, id;
Node(int _val, int _id) :val(_val), id(_id){}
Node(){}
bool operator < (const Node &b)const{
if (val == b.val) return id < b.id;
return val < b.val;
}
}a[6][maxn];
int n,m,q;

bitset<maxn> bs[5][250];
int block_num;
int per_block;

int main()
{
int T; cin >> T;
while (T--)
{
cin >> n >> m;
for (int i = 0; i < n; ++i){
for (int j = 0; j < 5; ++j){
scanf("%d", &a[j][i].val);
a[j][i].id = i;
}
}
for (int i = 0; i < 5; ++i) sort(a[i], a[i] + n);
block_num = sqrt(n + .0);
per_block = ceil(n / (block_num + .0));

for (int i = 0; i < 5; ++i){
for (int k = 0; k < block_num; ++k){
bs[i][k].reset();
}
}

for (int i = 0; i < 5; ++i){
for (int j = 0; j < n; ++j){
bs[i][j / per_block].set(a[i][j].id);
}
}

for (int i = 0; i < 5; ++i){
for (int j = 0; j < block_num; ++j){
if (j) bs[i][j] |= bs[i][j - 1];
}
}

cin >> q;
int ans = 0;
bitset<maxn> res, tmp;
int d;
while (q--)
{
res.set();
for (int k = 0; k < 5; ++k){
tmp.reset();
scanf("%d", &d);
d ^= ans;
int index = upper_bound(a[k], a[k] + n, Node(d, n + 1)) - a[k] - 1;
if (index < 0){
res.reset();
continue;
}
if (index / per_block) {
tmp = bs[k][index / per_block - 1];
}
int start = (index / per_block)*per_block;
int end = index;
for (int i = start; i <= end; ++i){
tmp.set(a[k][i].id);
}
res &= tmp;
}
ans = res.count();
printf("%d\n", ans);
}
}
return 0;
}


posted @ 2015-09-22 18:13  chanme  阅读(270)  评论(0编辑  收藏  举报