HDU 6085 Rikka with Candies bitset

HDU 6085

考虑枚举 B[ i ], 分成50000 / B[ i ] 段每段用bitset处理, 手写bitset的话复杂度n ^ 2 / 32。

训练的时候我没有手写bitset, 靠卡常加开o3优化卡过去了。

其实有一种方法能用手写bitset做到n ^ 2 / 32 + n * log(n), 感觉很巧妙呀。

博客在这里https://blog.csdn.net/luricheng/article/details/77006185

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
#include<bits/stdc++.h>
#define LL long long
#define LD long double
#define ull unsigned long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ALL(x) (x).begin(), (x).end()
#define fio ios::sync_with_stdio(false); cin.tie(0);

using namespace std;

const int N = 5e4 + 1;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 998244353;
const double eps = 1e-8;
const double PI = acos(-1);

template<class T, class S> inline void add(T &a, S b) {a += b; if(a >= mod) a -= mod;}
template<class T, class S> inline void sub(T &a, S b) {a -= b; if(a < 0) a += mod;}
template<class T, class S> inline bool chkmax(T &a, S b) {return a < b ? a = b, true : false;}
template<class T, class S> inline bool chkmin(T &a, S b) {return a > b ? a = b, true : false;}

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int n, m, q;
int T, A[N], B[N], C[N];
int ret[N];
bitset<N> bit, tmp;
bitset<N> ans;

int main() {
    scanf("%d", &T);
    while(T--) {
        memset(C, 0, sizeof(C));
        memset(ret, 0, sizeof(ret));
        bit.reset();
        ans.reset();
        int maxVal = 1;
        scanf("%d%d%d", &n, &m, &q);
        for(int i = 1; i <= n; i++) {
            scanf("%d", &A[i]);
            C[A[i]] ^= 1;
            chkmax(maxVal, A[i]);
        }
        for(int i = 1; i <= maxVal; i++) bit[i] = C[i];
        for(int i = 1; i <= m; i++) {
            scanf("%d", &B[i]);
        }
        sort(B + 1, B + 1 + m);
        for(int i = 1; i <= m; i++) {
            if(B[i] <= 1200) {
                for(int j = 0; j < B[i]; j++) {
                    int now = j, c = 0;
                    while(now <= maxVal) {
                        c ^= C[now];
                        now += B[i];
                    }
                    if(c) {
                        if(ans[j]) ans[j] = 0;
                        else ans[j] = 1;
                    }
                }
            } else {
                for(int L = 0, R = B[i] - 1; L <= maxVal; L += B[i], R += B[i]) {
                    R = min(maxVal, R);
                    tmp = (bit << (N - 1 - R)) >> (N - (R - L + 1));
                    ans ^= tmp;
                }
            }
        }
        while(q--) {
            int k; scanf("%d", &k);
            printf("%d\n", (int)ans[k]);
        }
    }
    return 0;
}


/*
*/

猜你喜欢

转载自www.cnblogs.com/CJLHY/p/11107838.html