hihocoder #1236 : Scores

高维bitset....

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

const int maxn = 50005;

struct node
{
	int val, id;
	node(int val = 0, int id = 0) : val(val), id(id) {}
	bool operator < (const node& b) const {
		if(val == b.val) return id < b.id;
		return val < b.val;
	}
}a[6][maxn];

bitset<maxn> bs[6][305], res, result, T;
int n, m;

void work()
{
	scanf("%d%*d", &n);
	for(int i = 0; i < n; i++) {
		for(int j = 1; j <= 5; j++) {
			scanf("%d", &a[j][i].val);
			a[j][i].id = i;
		}
	}
	
	for(int i = 1; i <= 5; i++) sort(a[i], a[i]+n);
	
	int s = sqrt(n);
	for(int j = 1; j <= 5; j++)
		for(int i = 0; i <= n / s + 1; i++) 
			bs[j][i] = 0;
	
	for(int i = 1; i <= 5; i++) {
		for(int j = 0; j < n; j++) {
			int t = j / s;
			bs[i][t][a[i][j].id] = 1;
		}
	}
	for(int j = 1; j <= 5; j++) 
		for(int i = 1; i <= n / s + 1; i++)
			bs[j][i] |= bs[j][i-1];
	
	T = 0;
	for(int i = 0; i < n; i++) T[i] = 1;
	
	int ans = 0;
	scanf("%d", &m);
	while(m--) {
		result = T;
		for(int i = 1; i <= 5; i++) {
			res = 0;
			int x;
			scanf("%d", &x);
			x ^= ans;
			int t = upper_bound(a[i], a[i]+n, node(x, n+1)) - a[i] - 1;
			if(t < 0) {
				result &= res;
				continue;
			}
			int tt = t / s;
			
			if(tt) res |= bs[i][tt-1];
			for(int j = tt * s; j <= t; j++) res[a[i][j].id] = 1;
			result &= res;
		}
		ans = result.count();
		printf("%d\n", ans);
	}	
}

int main()
{
	int _;
	scanf("%d", &_);
	while(_--) work();
	
	return 0;
}


你可能感兴趣的:(hihocoder #1236 : Scores)