#1236 : Scores
-
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
-
-
题意:
-
先给定n个人的5科成绩,Q次询问,每次询问给出一个人的成绩,查询所以科目的成绩都不严格大于(小于等于)这个人的成绩的数目,注意每次查询输入的成绩需要xor上次输出的答案。
-
思路:
-
常见的分块暴力题,5个科目分别排序,二分找到对应最后符合要求的科目位置,然后求共同存在这个5科目中的人的数目,考虑到只有两种状态,就用bitset进行操作,先分块处理出5个科目的前缀bitset状态,最后&一下就是答案。
-
代码:
-
#include<bits/stdc++.h> #define mp(a,b) make_pair(a,b) using namespace std; bitset<50010>A[6][300]; pair<int,int>P[6][50010]; int L[300],R[300],in[50010]; int main() { int T; scanf("%d",&T); while(T--){ int n,m; scanf("%d%d",&n,&m); for(int i=1;i<=n;i++){ for(int j=1;j<=5;j++){ scanf("%d",&P[j][i].first); P[j][i].second=i; } }for(int i=1;i<=5;i++){ A[i][0]=0; sort(P[i]+1,P[i]+n+1); }int len = sqrt(n); int num = n/len+(n%len?1:0); L[0]=R[0]=-1; for(int i=1;i<=num;i++){ L[i]=(i-1)*len+1; R[i]=min(n,i*len); for(int j=L[i];j<=R[i];j++){ in[j]=i; } }for(int i=1;i<=num;i++){ for(int j=1;j<=5;j++){ A[j][i]=A[j][i-1]; for(int k=L[i];k<=R[i];k++){ A[j][i].set(P[j][k].second); } } } int q,last=0;scanf("%d",&q); while(q--){ int tmp[6]; for(int i=1;i<6;i++){ scanf("%d",&tmp[i]); tmp[i]^=last; //printf("%d%c",tmp[i],i==5?'\n':' '); }bitset<50010>ans; for(int i=1;i<=5;i++){ int t = lower_bound(P[i]+1,P[i]+1+n,mp(tmp[i],n+1))-(P[i]+1); //printf("%d--->%d,now is %d\n",i,t,P[i][t]); bitset<50010>temp = A[i][in[t]-1]; //printf("t is in %d , L - >%d , R - >%d\n",in[t],L[in[t]],R[in[t]]); for(int j=L[in[t]];j<=t;j++){ temp.set(P[i][j].second); //printf("now is set %d\n",P[i][j].second); } if(i==1){ ans = temp; }else { ans &= temp; } }printf("%d\n",last = ans.count()); } }return 0; } /* 5 5 5 5 5 5 5 5 4 4 4 4 4 3 3 3 3 3 2 2 2 2 2 1 1 1 1 1 5 1 2 3 4 5 */
Description
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.
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.