hihoCoder 1236 Scores 解题报告(bitset + 分段暴力)

#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
解题报告: 求五维皆小于的数量。使用bitset记录当前某个维度,分数不大于给定查询的序号,最终对5个维度的bitset进行与操作。因为查询数量较多,故可以分段预处理。复杂度为O(n * sqrt(n) * M),M为bitset与操作的常数复杂度。样例很少,所以AC还是可以的。代码如下:

#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <vector>
#include <functional>
#include <cassert>
#include <bitset>
#include <list>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;

#define ff(i, n) for(int i=0,END=(n);i<END;i++)
#define fff(i, n, m) for(int i=(n),END=(m);i<=END;i++)
#define dff(i, n, m) for(int i=(n),END=(m);i>=END;i--)
#define travel(e, u) for(int e=first[u], v=vv[first[u]]; ~e; e=nxt[e])
#define mid ((l+r)/2)
#define bit(n) (1ll<<(n))
#define clr(a, b) memset(a, b, sizeof(a))
#define debug(x) cout << #x << " = " << x << endl;

#define ls (rt << 1)
#define rs (ls | 1)
#define lson l, m, ls
#define rson m + 1, r, rs

void work();

template<typename T>
void read(T & x) {
    char ch = getchar();
    while((ch < '0' || ch > '9') && ch != '-') ch = getchar();

    bool flag = (ch == '-');
    if (flag) ch = getchar();

    x = 0;
    while(ch >= '0' && ch <= '9') {
        x = x * 10 + (ch - '0');
        ch = getchar();
    }
    if (flag) x = -x;
}

template<typename T>
void print(T v) {
    if (v < 0) {
        putchar('-');
        print(-v);
    } else {
        if (v < 10) {
            putchar('0' + v);
        } else {
            print(v / 10);
            putchar(v % 10 + '0');
        }
    }
}

int main() {
    work();
    return 0;
}

/**************************Beautiful GEGE**********************************/

const int maxn = 50005;
pair<int, int> node[5][maxn];
bitset<maxn> stu[5][250];

void work() {
    int T; scanf("%d", &T);

    fff(cas, 1, T) {
        int n, m; read(n), read(m);
        ff(j, n) {
            ff(i, 5) {
                int s; read(s);
                node[i][j] = make_pair(s, j);
            }
        }

        ff(i, 5) {
            sort(node[i], node[i] + n);
            int top = 0;
            ff(j, n) {
                if ((j & 255) == 0) stu[i][top + 1] = stu[i][top], top ++;
                stu[i][top].set(node[i][j].second);
            }
        }

        int q; read(q);
        int ans = 0;
        while(q --) {
            bitset<maxn> u;
            u.flip();

            ff(i, 5) {
                int v; scanf("%d", &v);
                v ^= ans;
                int pos = upper_bound(node[i], node[i] + n, make_pair(v, maxn)) - node[i] - 1;
                if (pos == -1) {
                    u.reset();
                    continue;
                }

                bitset<maxn> now = stu[i][pos >> 8];
                fff(j, pos & ~255, pos) {
                    now.set(node[i][j].second);
                }
                u = u & now;
            }
            ans = u.count();
            print(ans);
            puts("");
        }
    }
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值