Kyle is a student of Programming Monkey Elementary School. Just as others, he is deeply concerned with his grades.

Last month, the school held an examination including five subjects, without any doubt, Kyle got a perfect score in every single subject.

There are n students took part in this examination(not including Kyle), and everyone got an integer between 1 to m as the score of one subject.

Now, looking at the grade table of these n students, Kyle wants to know how many students still did no better than him even if his scores are something else – Here, “no better” means there is no subject in which the student got strictly greater score than Kyle.

Input

There are multiple test cases.

The first line of the input contains an integer T (T <= 3) which means the number of test cases.

The first line of each test case contains two integers, n, m(n, m≤ 50,000), which are the number of students and the perfect score of each subject.

In the next n lines, each line consists of five integers, indicating a student’s scores.

Then one line follows. This line contains an integer q(q≤ 50,000) indicating the number of queries.

In the next q lines, each line contains five integers as well, representing a query. Each query indicates a set of scores, and for each query, you should figure out that if Kyle's grade is this set of scores, how many students still did no better than him. But for the sake of security, only the first query is in its original form, and other queries are encrypted. To decrypt a query, you must let each integer in the query do xor operation with the answer of last query. It's guaranteed that all the decrypted queries contain integers between 1 and 50000.

Output

For each test case, you should output q lines as the answer for all queries.

Sample Input

2
2 3
1 1 1 1 1
2 2 2 2 2
2
1 1 1 1 1
3 3 3 3 3
3 5
1 1 1 1 1
1 1 1 1 1
1 2 3 4 5
2
1 1 1 1 1
1 1 1 1 1

Sample Output

1
2
2
2

Hint

In case 1, there are two students with different scores and the scores of the first student (1, 1, 1, 1, 1) are not larger than the first query (1 1 1 1 1) in every subject, so the answer for this query is 1.

After having xor operation with the last answer 1, the second query (3,3,3,3,3) will be decrypted into (2, 2, 2, 2, 2). Because both students’ scores are no better than  (2, 2, 2, 2, 2), so the answer for query 2 is 2.


  题目大意 给定n个五元组,在给定q个五元组,询问每个5元组在偏序意义下,有多少个小于等于它。(强制在线,多组数据)

  五维偏序,根据常用套路n - 1维套树,总时间复杂度O(nlog4n)。请问这和暴力区别有多大?

  既然无法区分套树做法和暴力。那就暴力 + 黑科技卡过去吧。

  如果你认为这个暴力是每个询问直接去for套for(第二个for是比较),总时间复杂度为O(nqk)的暴力,那你没救了,赶快换道题吧。

  考虑如果每一维单独比较,然后最后把所有可行的集合取交集,这样似乎可以继续进行优化。

  每一维比较的时候可以用二分查找。既然又要取交集,那就bitset顶上吧。然而你需要对每一维排序后进行,bitset求前缀并(下标),这样你就可以顺利O(1)求出在这一维可行的集合。

  看起来查询很顺利,但是预处理不是怎么想的,首先5 * 4 * 50000 * 50000 / 32空间直接炸掉了。另外bitset赋值也不是O(1),所以时间也炸掉了。

  因此得均衡一下查询和预处理的耗时。所以想到了根号分块。

  然后随便搞就行了。总时间复杂度为(其中k为维度,这里为5)

  然后表示开始手抽每次询问给ans赋值时,一位一位地设成1,然后成功三发TLE,并且十分懵逼。

Code

 /**
* HihoCoder
* Problem#1236
* Accepted
* Time: 1696ms
* Memory: 22528k
*/
#include <bits/stdc++.h>
using namespace std;
typedef bool boolean; int opt;
typedef class Data {
public:
int w[]; friend boolean operator < (const Data& a, const Data& b) {
return a.w[opt] < b.w[opt];
}
}Data; int n, m, q;
int cs, cc;
Data ds[];
boolean endflag[];
int order[][];
bitset<> bs[][]; inline void init() {
scanf("%d%d", &n, &m);
cs = sqrt(n + 0.5);
cc = (n + cs - ) / cs;
// printf("Chunks datas: %d %d\n", cs, cc);
for(int i = ; i < n; i++) {
ds[i].w[] = i;
endflag[i] = !((i + ) % cs);
// if(endflag[i]) printf("endflag:%d\n", i);
for(int j = ; j <= ; j++)
scanf("%d", ds[i].w + j);
}
endflag[n - ] = true;
} inline void init_chunks(int k) {
opt = k;
sort(ds, ds + n);
for(int i = ; i < n; i++)
order[k][i] = ds[i].w[];
bitset<> b;
for(int j = ; j < n; j++) {
b[order[k][j]] = ;
if(endflag[j]) bs[k][j / cs] = bitset<>(b);
}
} inline void init_chunks() {
for(int i = ; i <= ; i++)
init_chunks(i);
opt = ;
sort(ds, ds + n);
} //int bitsetc = 0, cnt = 0; inline bitset<> solve(int k, int val) {
int l = , r = n - , ri;
bitset<> rt;
while(l <= r) {
// cnt++;
int mid = (l + r) >> ;
if(ds[order[k][mid]].w[k] > val) r = mid - ;
else l = mid + ;
}
if(r >= cs)
rt = bitset<>(bs[k][r / cs - ]);//, bitsetc++;
ri = r / cs * cs;
for(; ri <= r; ri++)
rt[order[k][ri]] = ;//, cnt++;
return rt;
} inline void solve() {
int lastans = ;
scanf("%d", &q);
bitset<> buf;
for(int i = ; i < n; i++)
buf[i] = ;
while(q--) {
bitset<> ans(buf);
for(int i = , x; i <= ; i++) {
scanf("%d", &x);
x ^= lastans;
// bitsetc++;
ans &= solve(i, x);
}
lastans = ans.count();
// bitsetc++;
printf("%d\n", lastans);
// fprintf(stderr, "Used:%d for uses:%d bitset uses:%d \n", bitsetc * 50001 / 32 + cnt, cnt, bitsetc);
}
} int T;
int main() {
scanf("%d", &T);
while(T--) {
init();
init_chunks();
solve();
}
// fprintf(stderr, "Time:%dms\n", clock());
return ;
}

HihoCoder 1236 Scores - bitset - 分块的更多相关文章

  1. hihocoder #1236 Scores (15北京赛区网络赛J) (五维偏序,强制在线,bitset+分块)

    链接:http://hihocoder.com/problemset/problem/1236 思路; 有n个五维的向量,给出q个询问,每个询问是一个五维向量,问有多少个向量没有一维比这个向量大.并且 ...

  2. HihoCoder - 1236 Scores (五维偏序,分块+bitset)

    题目链接 题意:给定n个五维空间上的点,以及m组询问,每组询问给出一个点,求五个维度都不大于它的点有多少个,强制在线. 神仙题 单独考虑每个维度,把所有点按这个维度上的大小排序,然后分成T块,每块用一 ...

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

    题意:给你50000个五维点(a1,a2,a3,a4,a5),50000个询问(q1,q2,q3,q4,q5),问已知点里有多少个点(x1,x2,x3,x4,x5)满足(xi<=qi,i=1,2 ...

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

    2015北京网络赛 J Scores 题意:50000组5维数据,50000个询问,问有多少组每一维都不大于询问的数据 思路:赛时没有思路,后来看解题报告也因为智商太低看了半天看不懂.bitset之前 ...

  5. hihocoder 1236(2015北京网络赛 J题) 分块bitset乱搞题

    题目大意: 每个人有五门课成绩,初始给定一部分学生的成绩,然后每次询问给出一个学生的成绩,希望知道在给定的一堆学生的成绩比这个学生每门都低或者相等的人数 因为强行要求在线查询,所以题目要求,每次当前给 ...

  6. HihoCoder#1513 : 小Hi的烦恼(五维数点 bitset 分块)

    题意 题目链接 Sol 五位数点问题,写个cdq分治套cdq分治套cdq分治套cdq分析就完了 可以用bitset搞 对于每一科开\(n\)个bitset,其中\(b[i]\)表示的排名为\(1 - ...

  7. 【卡常 bitset 分块】loj#6499. 「雅礼集训 2018 Day2」颜色

    好不容易算着块大小,裸的分块才能过随机极限数据:然而这题在线的数据都竟然是构造的…… 题目描述 有 $n$ 个数字,第 $i$ 个数字为 $a_i$. 有 $m$ 次询问,每次给出 $k_i$ 个区间 ...

  8. LOJ bitset+分块 大内存毒瘤题

    题面 $ solution: $ 真的没有想到可以用分块. 但是可以发现一个性质,每个询问只关心这个点最后一次赋值操作,和这个赋值操作后的所有取 $ min $ 操作.这个感觉很有用,但是真的很难让人 ...

  9. 洛谷 P5332 - [JSOI2019]精准预测(2-SAT+bitset+分块处理)

    洛谷题面传送门 七月份(7.31)做的题了,题解到现在才补,不愧是 tzc 首先不难发现题目中涉及的变量都是布尔型变量,因此可以考虑 2-SAT,具体来说,我们将每个人在每个时刻的可能的状态表示出来. ...

随机推荐

  1. unity3d射线控制移动

    看看效果图 代码: using UnityEngine; using System.Collections; public class T2 : MonoBehaviour { // Use this ...

  2. vue中解决跨域问题

    方法1.后台更改header header('Access-Control-Allow-Origin:*');//允许所有来源访问 header('Access-Control-Allow-Metho ...

  3. 21Oracle数据库和实例

    Oracle数据库:相关的操作系统文件(即储存在计算机硬盘上的文件)的集合,这些文件组织在一起,成为一个逻辑整体,即为Oracle数据库.物理存在 Oracle实例:位于物理内存里的数据结构,它由操作 ...

  4. armv8 memory system

    在armv8中,由于processor的预取,流水线, 以及多线程并行的执行方式,而且armv8-a中,使用的是一种weakly-ordered memory model, 不保证program or ...

  5. report源码分析——report_handle和report_server和report_catcher

    report_handle主要实现对message的action,severity,file的设置,然后将message传递给server: 主要的function有两个:initial和proces ...

  6. Unknown Treasure (卢卡斯 + 孙子定理, 模板题)

    Unknown Treasure 参考链接 : https://www.cnblogs.com/linyujun/p/5199684.html 卢卡斯定理 : C(n, m) % p  =  C(n ...

  7. Java基础(basis)-----抽象类和接口详解

    1.抽象类 1.1 abstract修饰类:抽象类 不可被实例化 抽象类有构造器 (凡是类都有构造器) 抽象方法所在的类,一定是抽象类 抽象类中可以没有抽象方法 1.2 abstract修饰方法:抽象 ...

  8. sql privot 实现行转列

    表结构如下: RefID    HRMS    Name    InsuranceMoney    InsuranceNamefb2bdee8-e4c9-4470-8e7f-14550d3212f7  ...

  9. locust 的使用

    Contents Locust这一款开源性能测试工具.然而,当前在网络上针对Locust的教程极少,不管是中文还是英文,基本都是介绍安装方法和简单的测试案例演示,但对于较复杂测试场景的案例演示却基本没 ...

  10. 转:【专题六】UDP编程

    引用: 前一个专题简单介绍了TCP编程的一些知识,UDP与TCP地位相当的另一个传输层协议,它也是当下流行的很多主流网络应用(例如QQ.MSN和Skype等一些即时通信软件传输层都是应用UDP协议的) ...