题解:P5607 [Ynoi2013] 无力回天 NOI2017

· · 题解

前置知识

思路

考虑暴力,即所有集合都使用 bitset,但是这样会 TLE 加上 MLE,所以考虑对于一个集合,如果大小小于 B 那么使用 vector 维护,如果大小大于等于 B 那么使用 bitset 维护,每次插入操作如果发现集合大小大于等于 B 那么给这个集合分配一个 bitset,查询时分类讨论:

现在还是 TLE,因为插入的时间复杂度很小,所以考虑把询问的时间复杂度分担到插入上来,因为现在询问时间复杂度的瓶颈在两个都是 bitset 的情况,所以考虑每次查询时如果是第一次查询这两个 bitset,那么直接统计答案,将这两个集合标记,否则输出存在数组里的答案,然后修改的时候如果该集合是 bitset 维护的,那么枚举标记,将所有被标记的集合对统计答案,这样询问的一些时间复杂度就被分担到修改上面了。

哪里需要循环展开呢,就是求两个 bitset 或后 1 的数量的时候:

inline __attribute__((always_inline)) int count_or(const int a,const int b){
    const uint64_t* __restrict pa = bit_dat[a];
    const uint64_t* __restrict pb = bit_dat[b];
    int cnt = 0,i = 0,maxx = max(bit_max[a],bit_max[b]);
    for(;i + 7 <= maxx;i += 8){
        __builtin_prefetch(pa + i + 16);
        __builtin_prefetch(pb + i + 16);
        cnt += __builtin_popcountll(pa[i] | pb[i]);
        cnt += __builtin_popcountll(pa[i + 1] | pb[i + 1]);
        cnt += __builtin_popcountll(pa[i + 2] | pb[i + 2]);
        cnt += __builtin_popcountll(pa[i + 3] | pb[i + 3]);
        cnt += __builtin_popcountll(pa[i + 4] | pb[i + 4]);
        cnt += __builtin_popcountll(pa[i + 5] | pb[i + 5]);
        cnt += __builtin_popcountll(pa[i + 6] | pb[i + 6]);
        cnt += __builtin_popcountll(pa[i + 7] | pb[i + 7]);
    }
    for(;i <= maxx;i ++){
        cnt += __builtin_popcountll(pa[i] | pb[i]);
    }
    return cnt;
}

这里还可以使用 __builtin_prefetch 进行优化。

还有一些小技巧,比如:

接下来就可以 AC 了,我的程序跑了 5.22 秒,成功挤进了最优解第一页。

时间复杂度是 O(n \times B + \frac{n^2}{B}),但是因为 \frac{n^2}{B} 的这部分常数极小,且 B 的值比 \sqrt{n} 小很多,所以卡过了。

Code

#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include<iostream>
#include<vector>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdint>
#define MAXN 15630
using namespace std;
int n,m,id[1000005];
uint64_t bit_dat[2005][MAXN];
int bit_ans[2005],bit_max[2005];
inline __attribute__((always_inline)) int count_or(const int a,const int b){
    const uint64_t* __restrict pa = bit_dat[a];
    const uint64_t* __restrict pb = bit_dat[b];
    int cnt = 0,i = 0,maxx = max(bit_max[a],bit_max[b]);
    for(;i + 7 <= maxx;i += 8){
        __builtin_prefetch(pa + i + 16);
        __builtin_prefetch(pb + i + 16);
        cnt += __builtin_popcountll(pa[i] | pb[i]);
        cnt += __builtin_popcountll(pa[i + 1] | pb[i + 1]);
        cnt += __builtin_popcountll(pa[i + 2] | pb[i + 2]);
        cnt += __builtin_popcountll(pa[i + 3] | pb[i + 3]);
        cnt += __builtin_popcountll(pa[i + 4] | pb[i + 4]);
        cnt += __builtin_popcountll(pa[i + 5] | pb[i + 5]);
        cnt += __builtin_popcountll(pa[i + 6] | pb[i + 6]);
        cnt += __builtin_popcountll(pa[i + 7] | pb[i + 7]);
    }
    for(;i <= maxx;i ++){
        cnt += __builtin_popcountll(pa[i] | pb[i]);
    }
    return cnt;
}
vector<int> v[1000005];
int mp[2005][2005],bit_tot,tt[2005][2005];
namespace IO{
    int tot;
    char buffer[1 << 23],tmp[80];
    inline __attribute__((always_inline)) char get_char(){
        static char buf[1000000],*p1 = buf,*p2 = buf;
        if(p1 == p2){
            p2 = (p1 = buf) + fread(buf,1,1000000,stdin);
            if(p1 == p2){
                return EOF;
            }
        }
        return *p1 ++;
    }
    inline __attribute__((always_inline)) int read(){
        int x = 0,f = 1;
        char ch = get_char();
        while(ch < '0' or ch > '9'){
            if(ch == '-'){
                f = -1;
            }
            ch = get_char();
        }
        while(ch >= '0' and ch <= '9'){
            x = x * 10 + ch - '0';
            ch = get_char();
        }
        return x * f;
    }
    inline __attribute__((always_inline)) void write(int x){
        if(x < 0){
            buffer[tot ++] = '-';
            x = -x;
        }
        if(x == 0){
            buffer[tot ++] = '0';
        }
        int len = 0;
        while(x > 0){
            tmp[len ++] = x % 10 + '0';
            x /= 10;
        }
        for(int i = len - 1;i >= 0;i --){
            buffer[tot ++] = tmp[i];
        }
        buffer[tot ++] = '\n';
    }
}
int main(){
    n = IO::read();
    m = 512;
    memset(id,-1,sizeof(id));
    while(n --){
        int op,x,y;
        op = IO::read();
        x = IO::read();
        y = IO::read();
        int idx = id[x];
        if(op == 1){    
            if(idx != -1){
                int w = y >> 6;
                bit_dat[idx][w] |= (1ull << (y & 63));
                bit_max[idx] = max(bit_max[idx],w);
                bit_ans[idx] ++;
                for(int k = 1;k <= tt[idx][0];k ++){
                    int j = tt[idx][k];
                    mp[idx][j] += !((bit_dat[j][w] >> (y & 63)) & 1ull);
                    mp[j][idx] += !((bit_dat[j][w] >> (y & 63)) & 1ull);
                }
            }else{
                if(v[x].size() + 1 < m){
                    v[x].insert(lower_bound(v[x].begin(),v[x].end(),y),y);
                }else{
                    id[x] = idx = ++ bit_tot;
                    for(int j : v[x]){
                        int w = j >> 6;
                        bit_dat[idx][w] |= (1ull << (j & 63));
                        bit_max[idx] = max(bit_max[idx],w);
                        bit_ans[idx] ++;
                    }
                    int w = y >> 6;
                    bit_dat[idx][w] |= (1ull << (y & 63));
                    bit_max[idx] = max(bit_max[idx],w);
                    bit_ans[idx] ++;
                    vector<int>().swap(v[x]);
                }
            }
        }else{
            int idy = id[y];
            if(idx == -1 and idy == -1){
                int i = 0,j = 0,ans = 0,sz_x = v[x].size(),sz_y = v[y].size();
                while(i < sz_x and j < sz_y){
                    int vx = v[x][i],vy = v[y][j];
                    ans += (vx == vy);
                    i += (vx <= vy);
                    j += (vy <= vx);
                }
                IO::write(sz_x + sz_y - ans);
            }else if(idx != -1 and idy != -1){
                if(x == y){
                    IO::write(bit_ans[idx]);
                    continue;
                }
                if(idx > idy){
                    swap(x,y);
                    swap(idx,idy);
                }
                if(mp[idx][idy] == 0){
                    mp[idx][idy] += count_or(idx,idy);
                    mp[idy][idx] = mp[idx][idy];
                    tt[idx][++ tt[idx][0]] = idy;
                    tt[idy][++ tt[idy][0]] = idx;
                }
                IO::write(mp[idx][idy]);
            }else{
                if(idx != -1){
                    swap(x,y);
                    swap(idx,idy);
                }
                int ans = bit_ans[idy];
                for(int j : v[x]){
                    ans += !((bit_dat[idy][j >> 6] >> (j & 63)) & 1ull);
                }
                IO::write(ans);
            }
        }
    }
    fwrite(IO::buffer,1,IO::tot,stdout);
    return 0;
}