2015 ACM北京网络赛J题 hiho1236

1236 : Scores

时间限制:4000ms
单点时限:4000ms
内存限制:256MB
描述
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.

输入
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.

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

提示
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.

样例输入
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
样例输出
1
2
2
2

网络赛最后一题。 。当时记得快200提交只有几个人AC。 。坑点就在于要合适的查询。 。开始用了二分查找还是WA。 。后来看了题解才知道要用bitset同时还要开根号预处理,简单说就是将要查询的总情况排序好后分成几段,然后查询的时候就不必每次都从一开始查询,直接从相应的段开始查询 。。又用bitset用二进制的方法存储状态,加快速度。

#include<cstdio>
#include<iostream>
#include<map>
#include<cstring>
#include<algorithm>
#include<string>
#include<bitset>
#include<cmath>
//#define LOCAL

using namespace std;

struct Point{
    int v;
    int sign;
};

Point s1[50005];
Point s2[50005];
Point s3[50005];
Point s4[50005];
Point s5[50005];
bitset<50005> G[6][250];
bitset<50005> t;

int qq[7];

bool cmp(Point a,Point b){
    if(a.v>=b.v)
        return false;
    return true;
}

int bin(Point* array,int last,int key){
    int first = 1;
    int middle,pos = 1;

    while(first<last){
        middle = (first+last)/2;
        if(array[middle].v<key){
            first = middle + 1;
            pos = first;
        }else{
            last = middle;
            pos = last;
        }
    }
    if(key<array[pos].v) pos--;
    return pos;
}

int main()
{
#ifdef LOCAL
    freopen("data.in","r",stdin);
#endif
    int T=0;
    scanf("%d",&T);
    bitset<50005> b;
    while(T--)
    {
        int n=0,m=0;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
        {
            int x=0;
            for(int j=1;j<=5;j++)
            {
                scanf("%d",&x);
                switch (j){
                    case 1:
                        s1[i].v = x;
                        s1[i].sign = i;
                        break;
                    case 2:
                        s2[i].v = x;
                        s2[i].sign = i;
                        break;
                    case 3:
                        s3[i].v = x;
                        s3[i].sign = i;
                        break;
                    case 4:
                        s4[i].v = x;
                        s4[i].sign = i;
                        break;
                    case 5:
                        s5[i].v = x;
                        s5[i].sign = i;
                        break;
                }
            }
        }

        sort(s1+1,s1+n+1,cmp);
        sort(s2+1,s2+n+1,cmp);
        sort(s3+1,s3+n+1,cmp);
        sort(s4+1,s4+n+1,cmp);
        sort(s5+1,s5+n+1,cmp);

        int SQ = sqrt(n);

        for(int i = 1;i<=5;i++){
            G[i][0].reset();
            for(int j = 1;(j-1)*SQ<n;j++){
                G[i][j].reset();
                G[i][j] = G[i][j-1];
                int st = (j-1)*SQ;
                for(int k = 1;k<=SQ&&st+k<=n;k++){
                    switch (i){
                        case 1:
                            G[i][j].set(s1[st+k].sign);
                            break;
                        case 2:
                            G[i][j].set(s2[st+k].sign);
                            break;
                        case 3:
                            G[i][j].set(s3[st+k].sign);
                            break;
                        case 4:
                            G[i][j].set(s4[st+k].sign);
                            break;
                        case 5:
                            G[i][j].set(s5[st+k].sign);
                            break;
                    }
                }
            }

        }

        int q=0;
        int sum=0;
        int tmp;
        scanf("%d",&q);
        for(int k=1;k<=q;k++)
        {
            b.reset();
            for(int j=1;j<=5;j++)
            {
                scanf("%d",&qq[j]);
                qq[j] ^= sum;
            }
            for(int j=1;j<=5;j++)
                switch (j){
                    case 1:
                        sum =  bin(s1,n,qq[j]);
                        tmp = sum/SQ;
                        b = G[1][tmp];
                        for(int i = tmp*SQ+1;s1[i].v<=qq[j]&&i<=n;i++)
                            b.set(s1[i].sign);
                        break;
                    case 2:
                        sum =  bin(s2,n,qq[j]);
                        tmp = sum/SQ;
                        t.reset();
                        t = G[j][tmp];
                        for(int i = tmp*SQ+1;s2[i].v<=qq[j]&&i<=n;i++)
                            t.set(s2[i].sign);
                        b &= t;
                        break;
                    case 3:
                        sum =  bin(s3,n,qq[j]);
                        tmp = sum/SQ;
                        t.reset();
                        t = G[j][tmp];
                        for(int i = tmp*SQ+1;s3[i].v<=qq[j]&&i<=n;i++)
                            t.set(s3[i].sign);
                        b &= t;
                        break;
                    case 4:
                        sum =  bin(s4,n,qq[j]);
                        tmp = sum/SQ;
                        t.reset();
                        t = G[j][tmp];
                        for(int i = tmp*SQ+1;s4[i].v<=qq[j]&&i<=n;i++)
                            t.set(s4[i].sign);
                        b &= t;
                        break;
                    case 5:
                        sum =  bin(s5,n,qq[j]);
                        tmp = sum/SQ;
                        t.reset();
                        t = G[j][tmp];
                        for(int i = tmp*SQ+1;s5[i].v<=qq[j]&&i<=n;i++)
                            t.set(s5[i].sign);
                        b &= t;
                        break;
                }
            sum = b.count();
            printf("%d\n",sum);
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值