求助cf题!!
查看原帖
求助cf题!!
253342
HYX1124楼主2022/7/20 11:55

在cf上遇到的题:1702F - Equate Multisets

我用的是trie树,官方题解用的是multiset的暴力,卡在test2的#881点上了,真心调不出来

求助各位谷民!!

#include <bits/stdc++.h>
#ifndef ONLINE_JUDGE
#include "lib.h"
#endif
#define rep(i, min, max) for(int i = min; i <= max; ++i)
using namespace std;

typedef long long ll;
inline int read()
{
    int now=0; bool nev=false; char c=getchar();
    while(c<'0' || c>'9') { if(c=='-') nev=true; c=getchar();}
    while(c>='0' && c<='9') { now=(now<<1)+(now<<3)+(c&15); c=getchar(); }
    return nev?-now:now;
}

const int N = 2e5 + 10;
int a[N], b[N], alloc = 1;

struct node{
    int s[2], v;
} e[N << 5];

inline void pushdown(int x){
    if(!e[x].s[0]) e[x].s[0] = ++alloc, e[x].s[1] = ++alloc;
}

inline int rev(int x){
    int res = 0;
    while(x) res = (res << 1) | (x & 1), x >>= 1;
    return res;
}

void insert(int now, int x){
    if(!x) return;
    ++e[now].v;
    pushdown(now);
    insert(e[now].s[x & 1], x >> 1);
}

bool find(int now, int x){
    if(!x) return 1;
    if(e[now].v <= 0) return 0;
    --e[now].v;
    pushdown(now);
    return find(e[now].s[x & 1], x >> 1);
}

void clear(int now){
    if(!now) return;
    e[now].v = 0;
    clear(e[now].s[0]);
    clear(e[now].s[1]);
}

int main(){
    int T = read();
    while(T--){
        int n = read();
        rep(i, 1, n) a[i] = rev(read());
        rep(i, 1, n) b[i] = rev(read());
        rep(i, 1, n) insert(1, b[i]);
        rep(i, 1, n){
            bool k = find(1, a[i]);
            if(!k){
                printf("NO\n");
                goto C;
            }
        }
        printf("YES\n");
        C:
        clear(1);
    }
}
2022/7/20 11:55
加载中...