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("");
        }
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章