2019南昌邀请赛 E.Another Sequence

链接

点击跳转

题解

第一步就是先 f w t fwt fwt,非常裸

第二步就是离散化+数据结构维护,非常裸

代码

#include 
#include 
#include 
#define iinf 0x3f3f3f3f
#define linf (1ll<<60)
#define eps 1e-8
#define maxn 1000010
#define maxe 1000010
#define cl(x) memset(x,0,sizeof(x))
#define rep(_,__) for(_=1;_<=(__);_++)
#define em(x) emplace(x)
#define emb(x) emplace_back(x)
#define emf(x) emplace_front(x)
#define fi first
#define se second
#define de(x) cerr<<#x<<" = "<
#define mod 998244353
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll read(ll x=0)
{
    ll c, f(1);
    for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f;
    for(;isdigit(c);c=getchar())x=x*10+c-0x30;
    return f*x;
}
struct FWT
{
    ll a[maxn], b[maxn], n;
    void init(ll N)
    {
        for(n=1;n<=N;n<<=1);
        for(ll i=0;i<n;i++)a[i]=b[i]=0;
    }
    void fwt(ll opt)
    {
        for(ll i=1;i<n;i<<=1)
            for(ll p=i<<1,j=0;j<n;j+=p)
                for(ll k=0;k<i;++k)
                    if(opt==1)a[i+j+k]=(a[j+k]+a[i+j+k]);
                    else a[i+j+k]=(a[i+j+k]-a[j+k]);
    }
}a, b;
struct Lisan
{
    ll tmp[maxn], tot;
    void clear(){tot=0;}
    void insert(ll x){tmp[++tot]=x;}
    void run()
    {
        sort(tmp+1,tmp+tot+1);
        tot=unique(tmp+1,tmp+tot+1)-tmp-1;
    }
    void lisan(ll *a, ll len)
    {
        for(ll i=1;i<=len;i++)a[i]=lower_bound(tmp+1,tmp+tot+1,a[i])-tmp;
    }
    ll lisan(ll x)
    {
        return lower_bound(tmp+1,tmp+tot+1,x)-tmp;
    }
}ls;
struct BIT
{
    ll bit[maxn], n;
    void init(ll N){n=N;for(ll i=1;i<=n;i++)bit[i]=0;}
    ll lowbit(ll x){return x&(-x);}
    void add(ll pos, ll v)
    {
        for(;pos<=n;pos+=lowbit(pos))bit[pos]+=v;
    }
    ll sum(ll pos)
    {
        ll ans(0);
        for(;pos;pos-=lowbit(pos))ans+=bit[pos];
        return ans;
    }
}bit;
ll c[maxn], l[maxn], r[maxn];
int main()
{
    ll i, j, n, m;
    n=read();
    a.init(100000), b.init(100000);
    rep(i,n)a.a[read()]++;
    rep(i,n)b.a[read()]++;
    a.fwt(1), b.fwt(1);
    rep(i,200000)a.a[i]*=b.a[i];
    a.fwt(-1);
    rep(i,200000)c[i]=a.a[i]+c[i-1];
    m=read();
    rep(i,m)
    {
        l[i]=read(), r[i]=read();
        ls.insert(l[i]), ls.insert(l[i]+1);
        ls.insert(r[i]), ls.insert(r[i]+1);
    }
    ls.run();
    bit.init(ls.tot);
    rep(i,m)
    {
        if(l[i])
        {
            bit.add(ls.lisan(l[i]),+1);
            bit.add(ls.lisan(r[i]+1),-1);
        }
        else
        {
            ll t=bit.sum(ls.lisan(r[i])), ans=lower_bound(c+1,c+200000,r[i])-c;
            while(ans>1 and t)
            {
                ans=ll(sqrt(ans));
                t--;
            }
            printf("%lld\n",ans);
        }
    }
    return 0;
}

你可能感兴趣的:(FWT,树状数组)