HihoCoder 1236 Scores - bitset - 分块
Posted 阿波罗2003
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HihoCoder 1236 Scores - bitset - 分块相关的知识,希望对你有一定的参考价值。
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
1 /** 2 * HihoCoder 3 * Problem#1236 4 * Accepted 5 * Time: 1696ms 6 * Memory: 22528k 7 */ 8 #include <bits/stdc++.h> 9 using namespace std; 10 typedef bool boolean; 11 12 int opt; 13 typedef class Data { 14 public: 15 int w[6]; 16 17 friend boolean operator < (const Data& a, const Data& b) { 18 return a.w[opt] < b.w[opt]; 19 } 20 }Data; 21 22 int n, m, q; 23 int cs, cc; 24 Data ds[50005]; 25 boolean endflag[50005]; 26 int order[6][50005]; 27 bitset<50001> bs[6][400]; 28 29 inline void init() { 30 scanf("%d%d", &n, &m); 31 cs = sqrt(n + 0.5); 32 cc = (n + cs - 1) / cs; 33 // printf("Chunks datas: %d %d\n", cs, cc); 34 for(int i = 0; i < n; i++) { 35 ds[i].w[0] = i; 36 endflag[i] = !((i + 1) % cs); 37 // if(endflag[i]) printf("endflag:%d\n", i); 38 for(int j = 1; j <= 5; j++) 39 scanf("%d", ds[i].w + j); 40 } 41 endflag[n - 1] = true; 42 } 43 44 inline void init_chunks(int k) { 45 opt = k; 46 sort(ds, ds + n); 47 for(int i = 0; i < n; i++) 48 order[k][i] = ds[i].w[0]; 49 bitset<50001> b; 50 for(int j = 0; j < n; j++) { 51 b[order[k][j]] = 1; 52 if(endflag[j]) bs[k][j / cs] = bitset<50001>(b); 53 } 54 } 55 56 57 inline void init_chunks() { 58 for(int i = 1; i <= 5; i++) 59 init_chunks(i); 60 opt = 0; 61 sort(ds, ds + n); 62 } 63 64 //int bitsetc = 0, cnt = 0; 65 66 inline bitset<50001> solve(int k, int val) { 67 int l = 0, r = n - 1, ri; 68 bitset<50001> rt; 69 while(l <= r) { 70 // cnt++; 71 int mid = (l + r) >> 1; 72 if(ds[order[k][mid]].w[k] > val) r = mid - 1; 73 else l = mid + 1; 74 } 75 if(r >= cs) 76 rt = bitset<50001>(bs[k][r / cs - 1]);//, bitsetc++; 77 ri = r / cs * cs; 78 for(; ri <= r; ri++) 79 rt[order[k][ri]] = 1;//, cnt++; 80 return rt; 81 } 82 83 inline void solve() { 84 int lastans = 0; 85 scanf("%d", &q); 86 bitset<50001> buf; 87 for(int i = 0; i < n; i++) 88 buf[i] = 1; 89 while(q--) { 90 bitset<50001> ans(buf); 91 for(int i = 1, x; i <= 5; i++) { 92 scanf("%d", &x); 93 x ^= lastans; 94 // bitsetc++; 95 ans &= solve(i, x); 96 } 97 lastans = ans.count(); 98 // bitsetc++; 99 printf("%d\n", lastans); 100 // fprintf(stderr, "Used:%d for uses:%d bitset uses:%d \n", bitsetc * 50001 / 32 + cnt, cnt, bitsetc); 101 } 102 } 103 104 int T; 105 int main() { 106 scanf("%d", &T); 107 while(T--) { 108 init(); 109 init_chunks(); 110 solve(); 111 } 112 // fprintf(stderr, "Time:%dms\n", clock()); 113 return 0; 114 }
以上是关于HihoCoder 1236 Scores - bitset - 分块的主要内容,如果未能解决你的问题,请参考以下文章
1236 - Pairs Forming LCM -- LightOj1236 (LCM) 给你一个数n,让你求1到n之间的数(a,b && a<=b)两个数的最小公倍数等
LightOJ 1236 - Pairs Forming LCM(素因子分解)