hdu—6059 01字典树

传送门

Kanade's trio

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 534    Accepted Submission(s): 178


Problem Description
Give you an array  A[1..n] ,you need to calculate how many tuples  (i,j,k)  satisfy that  (i<j<k)  and  ((A[i] xor A[j])<(A[j] xor A[k]))

There are T test cases.

1T20

1n5105

0A[i]<230
 

Input
There is only one integer T on first line.

For each test case , the first line consists of one integer  n  ,and the second line consists of  n  integers which means the array  A[1..n]
 

Output
For each test case , output an integer , which means the answer.
 

Sample Input
  
  
1 5 1 2 3 4 5
 

Sample Output
  
  
6
 

Source
 

Recommend
liuyiding   |   We have carefully selected several similar problems for you:   6066  6065  6064  6063  6062 

参考这里

题意:问你A[i]^A[j]<A[j]^A[k]。有多少组。

做法:比赛的时候看了这道题。刚开始想了想字典树,仔细分析的时候其实对于A[j]他的意义不大,其实看的是A[i],A[k]的不同位。假如一个二进制,相同位置都相同,那么跟另外一个数^肯定没区别,所以看的是两个数二进制不同的地方。如果这个不同的地方跟A[j]一样,^之后肯定就为0了,另一个是1。所以对于两个数A[i],A[k]维护最高位即可。具体可以用一个前缀树和后缀树维护(这点比赛的时候没想到,就不敢继续往下想了。。)然后操作也是跟正常的01字典树一样。枚举a[j],考虑1~j-1维护一棵字典树,j+1~n维护一棵字典树,每次计算a[j]的每一位对应的两棵树的每一层满足后缀树^(a[j]^(1<<k))>前缀树^(a[j]^(1<<k))的方案数,也就是当前位置如果是0,就计算后缀树当前层中1的数量和前缀树当前层中0的数量。

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

#define pi acos(-1)
#define endl '\n'
#define srand() srand(time(0));
#define me(x,y) memset(x,y,sizeof(x));
#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)
#define close() ios::sync_with_stdio(0); cin.tie(0);
#define FOR(x,n,i) for(int i=x;i<=n;i++)
#define FOr(x,n,i) for(int i=x;i<n;i++)
#define W while
#define sgn(x) ((x) < 0 ? -1 : (x) > 0)
#define bug printf("***********\n");
typedef long long LL;
const int INF=0x3f3f3f3f;
const LL LINF=0x3f3f3f3f3f3f3f3fLL;
const int dx[]={-1,0,1,0,1,-1,-1,1};
const int dy[]={0,1,0,-1,-1,1,-1,1};
const int maxn=1e5+100;
const int maxx=1e7+100;
const double EPS=1e-7;
const int MOD=10000007;
#define mod(x) ((x)%MOD);
template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);}
template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);}
template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));}
template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));}
inline int Scan()
{
    int Res=0,ch,Flag=0;
    if((ch=getchar())=='-')Flag=1;
    else if(ch>='0' && ch<='9')Res=ch-'0';
    while((ch=getchar())>='0'&&ch<='9')Res=Res*10+ch-'0';
    return Flag ? -Res : Res;
}
//freopen( "in.txt" , "r" , stdin );
//freopen( "data.out" , "w" , stdout );
//cerr << "run time is " << clock() << endl;
int next1[maxn*32][2],next2[maxn*32][2],ans[maxn*32][2],n,st=0,a[maxn*5];
LL trans[maxn*32];
void init()
{
    for(int i=0;i<=st;i++)
        next1[i][0]=next1[i][1]=next2[i][0]=next2[i][1]=trans[i]=0;
    st=0;
    for(int i=0;i<=32;i++)
        ans[i][0]=ans[i][1]=0;
}
void update(int pos,LL x,int modi,int t,int dep)
//当前节点pos,当前位01状态x,add/delete状态modi
//前缀树和后缀树区分t(0/1),层数dep
{
    next2[pos][t]+=modi;//正常更新当前节点数量
    ans[dep][x]+=modi*next2[trans[pos]][t^1];
    //计算dep层中,后缀树中当前节点为x,前缀树中对应x^1的方案数
}

void insert(LL x,int t)
{
    int u=0;
    for(int i=32;i>=0;i--)
    {
        int c=((x>>i)&1);
        if(!next1[u][c])
        {
            //me(next1[st],0);
            next1[u][c]=++st;
            if(next1[u][c^1])
            {
                trans[st]=next1[u][c^1];
                trans[next1[u][c^1]]=st;
            }
        }
        u=next1[u][c];
        update(u,c^t,1,t,i);
    }
}
void _delete(LL x,int t)
{
    int u=0;
    for (int i=32; i>=0; --i)
    {
        int c=((x>>i)&1);
        u=next1[u][c];
        update(u,c^t,-1,t,i);
    }
}
LL ans_sum=0;
void query(LL x)
{
    for (int i=32; i>=0; --i)
    {
        int c=((x>>i)&1);
        ans_sum+=ans[i][c];
    }
}
int main()
{
    //freopen( "in.txt" , "r" , stdin );
    int cas;
    scanf("%d",&cas);
    while(cas--)
    {
        n=Scan();
        ans_sum=0;st=0;
        if(n<=2)
        {
            puts("0");
            continue;
        }
        for(int i=1;i<=n;i++)
            a[i]=Scan();
        for(int i=1;i<=n;i++)
            insert(a[i],1);
        _delete(a[1],1);insert(a[1],0);
        for(int i=2;i<=n;i++)
        {
            _delete(a[i],1);
            query(a[i]);
            insert(a[i],0);
        }
        cout<<ans_sum<<endl;
        init();
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值