【BZOJ5285】【HNOI2018】Treasure Hunt

Description

click me


Solution

for each bit of the result i Consider if the bit is in the j operation is 1 ,So b i First j bit is 1 , otherwise 0 .
For an operation, if the j The second operation is an AND operation, then x equal 1 , otherwise 0 .
If the final result of the i bit is 1 ,So x < b i ,otherwise x b i .
Then process each query directly after sorting to find x range is sufficient.


Code

/************************************************
 * Au: Hany01
 * Date: Apr 20th, 2018
 * Prob: [BZOJ5285][HNOI2018] 寻宝游戏
 * Email: [email protected]
************************************************/

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
#define File(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout)
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define x first
#define y second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) ((int)(a).size())
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define Mod (1000000007)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia

template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }

inline int read()
{
    register int _, __; register char c_;
    for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
    for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
    return _ * __;
}

const int maxn = 1005, maxm = 5005;

int n, m, q, pos[maxm];


//////////////////////For bitset
bitset<maxm> s[maxn], S;

bool operator < (bitset<maxn> A, bitset<maxn> B) {
    For(i, 1, n) if (A[i] ^ B[i])
        if (A[i]) return 0; else return 1;
    return 0;
}

struct BB
{
    bitset<maxn> b;
    int id, val;

    inline bool operator < (const BB A) const { return b < A.b; }

    inline void getval() {
        val = 0;
        For(i, 1, n) val = (val * 2 % Mod + b[i]) % Mod;
    }

}b[maxm];

inline bool getbit()
{
    register char c;
    for (c = getchar(); !isdigit(c); c = getchar()) ;
    return c ^ 48;
}

bitset<maxm> getbits()
{
    static bitset<maxm> bt;
    Fordown(i, m, 1) bt[i] = getbit();
    return bt;
}
////////////////////////////////




int main()
{
#ifdef hany01
    File("bzoj5285");
#endif

    //Init
    ////Input
    n = read(), m = read(), q = read();
    For(i, 1, n) s[i] = getbits();

    ////Get array b, which stands for if the i_th bit is 0 or 1, the value of x should < or > b[i], and sort them.
    For(i, 1, m) {
        For(j, 1, n) b[i].b[n - j + 1] = s[j][i];
        b[i].id = i, b[i].getval();
    }
    sort(b + 1, b + 1 + m);
    For(i, 1, m) pos[b[i].id] = i;


    //Answer questions
    pos[m + 1] = m + 1;
    For(i, 1, n) b[m + 1].b[i] = 1;
    b[m + 1].getval(), ++ b[m + 1].val;
    while (q --)
    {
        S = getbits();
        register int Min = m + 1, Max = 0;
        For(i, 1, m) S[i] ? chkmin(Min, pos[i]) : chkmax(Max, pos[i]);
        if (Min <= Max) puts("0");
        else printf("%d\n", (b[Min].val - b[Max].val + Mod) % Mod);
    }

    return 0;
}
//红颜未老恩先断,斜倚薰笼坐到明。
//    -- 白居易《后宫词》

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324663272&siteId=291194637