2015北京网络赛 J Scores bitset+分块

时间:2021-11-20 11:27:46

2015北京网络赛 J Scores

题意:50000组5维数据,50000个询问,问有多少组每一维都不大于询问的数据

思路:赛时没有思路,后来看解题报告也因为智商太低看了半天看不懂。bitset之前没用过,查了下发现其实就是一个二进制表示,这里的每一位就表示原序中的状态。

    建一个bitset<50000> bs[6][sqrt(50000)], bs[i][j] 就表示在第i维上前j个块中的数据在原序中是哪些位置。

 #include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <bitset>
#define LL long long
#define eps 1e-8
#define INF 0x3f3f3f3f
#define MAXN 50005
using namespace std;
struct Node{
int x, pos;
};
bitset<MAXN> bs[][];
int t[];
bitset<MAXN> res[], w;
bool compare(Node a, Node b){
return a.x < b.x;
}
Node s[][MAXN];
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif // OPEN_FILE
int T;
int n, m;
scanf("%d", &T);
while (T--){
scanf("%d%d", &n, &m);
for (int i = ; i <= n; i++){
for (int j = ; j <= ; j++){
scanf("%d", &s[j][i].x);
s[j][i].pos = i;
}
}
for (int i = ; i <= ; i++){
sort(s[i] + , s[i] + + n, compare);
}
int u = sqrt(n);
int num = n / u;
if (n % u != ){
num++;
}
memset(bs, , sizeof(bs));
for (int i = ; i <= ; i++){
for (int j = ;j <= num; j++){
bs[i][j] |= bs[i][j - ];
int left = (j - ) * u + , right = j * u;
if (right > n){
right = n;
}
for (int k = left; k <= right; k++){
bs[i][j][s[i][k].pos] = ;
}
}
}
int ans = ;
int q;
scanf("%d", &q);
while (q--){
for (int i = ; i <= ; i++){
scanf("%d", &t[i]);
t[i] ^= ans;
}
for (int i = ; i <= ; i++){
int left = , right = n;
while (left < right){
int mid = (left + right) >> ;
if (s[i][mid].x > t[i]){
right = mid;
}
else{
left = mid + ;
}
}
int pos;
if (s[i][left].x > t[i]){
pos = left - ;
}
else{
pos = left;
}
int u_pos = (pos - ) / u;
res[i] = bs[i][u_pos];
for (int j = u_pos * u + ; j <= pos; j++){
res[i][s[i][j].pos] = ;
}
}
w = res[];
for (int i = ; i <= ; i++){
w &= res[i];
}
ans = w.count();
printf("%d\n", ans);
}
}
}