hdu—6059

Kanade's trio

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


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
 
【分析】
字典树.....枚举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>
#define X 33
#define N 600000
#define M 20000000
using namespace std;
int n, a[N], cnt, f[M][3], s[X+3], trans[M];
long long ans_case, ans[X+3][3], g[M][3];

void update(int pos,int x,int modi,int t,int dep)
//当前节点pos,当前位01状态x,add/delete状态modi 
//前缀树和后缀树区分t(0/1),层数dep 
{
	g[pos][t]+=modi;//正常更新当前节点数量 
	ans[dep][x]+=modi*g[trans[pos]][t^1];
	//计算dep层中,后缀树中当前节点为x,前缀树中对应x^1的方案数 
}
void add(int x,int t)
{
	int j=0, ct=0;
	while (x)
	{
		s[++ct]=x&1;
		x>>=1;
	}
	for (int i=ct+1;i<=X;i++) s[i]=0;
	for (int i=X;i;i--)
	{
		if (!f[j][s[i]])
		{
			f[j][s[i]]=++cnt;
			if (f[j][s[i]^1])
			{
				trans[cnt]=f[j][s[i]^1];
				//记录当前cnt节点在另一棵树中对应的节点编号,用于后面计算答案 
				trans[f[j][s[i]^1]]=cnt;
			}
		}
		j=f[j][s[i]];
		update(j,s[i]^t,1,t,i);
	}
}
void del(int x,int t)
{
	int j=0, ct=0;
	while (x)
	{
		s[++ct]=x&1;
		x>>=1;
	}
	for (int i=ct+1;i<=X;i++) s[i]=0;
	for (int i=X;i;i--)
	{
		j=f[j][s[i]];
		update(j,s[i]^t,-1,t,i);
	}
}
void solve(int x)
{
	int j=0, ct=0;
	while (x)
	{
		s[++ct]=x&1;
		x>>=1;
	}
	for (int i=ct+1;i<=X;i++) s[i]=0;
	for (int i=X;i;i--) ans_case+=ans[i][s[i]];
}
int main()
{
	int T_T;scanf("%d",&T_T);
	while (T_T--)
	{
		cnt=ans_case=0;
		scanf("%d",&n);
		for (int i=1;i<=n;i++) scanf("%d",&a[i]);
		
		if (n==1 || n==2){cout<<0<<endl;continue;}
		//高中生的题目....总有些坑留在这 
		for (int i=1;i<=n;i++) add(a[i],1);
		del(a[1],1);add(a[1],0);
		for (int i=2;i<=n;i++)
		{
			del(a[i],1);
			solve(a[i]);
			add(a[i],0);
		}
		cout<<ans_case<<endl;
		for (int i=1;i<=X;i++) ans[i][0]=ans[i][1]=0;
		for (int i=0;i<=cnt;i++) f[i][0]=f[i][1]=g[i][0]=g[i][1]=trans[i]=0;
	}
	return 0;
}

标程
#include<bits/stdc++.h>
#define fi first
#define se second
#define rep(i,j,k) for(int i=(int)j;i<=(int)k;i++)
#define per(i,j,k) for(int i=(int)j;i>=(int)k;i--)
using namespace std;
typedef long long LL;
const int N=510000;
inline void read(int &x){
    x=0;char p=getchar();
    while(!(p<='9'&&p>='0'))p=getchar();
    while(p<='9'&&p>='0')x*=10,x+=p-48,p=getchar();
}
int a[N];
int go[N*32][2],tot;
LL ss[N*32];
int num[N*32];
int sum[N][32][2];
int n,T;
int main(){
    read(T);
    while(T--){
        rep(i,1,tot)rep(j,0,1)go[i][j]=0;
        rep(i,1,tot)ss[i]=num[i]=0;
        rep(i,1,n)rep(j,0,29)rep(k,0,1)sum[i][j][k]=0;
        tot=1;
        read(n);
        rep(i,1,n)read(a[i]);
        rep(i,1,n){
            rep(j,0,29)rep(k,0,1)sum[i][j][k]=sum[i-1][j][k];
            rep(j,0,29){
                int v=((a[i]&(1<<j))>0);
                sum[i][j][v]++;
            }
        }
        LL ans=0;
        per(i,n,1){
            int now=1;
            per(j,29,0){
                int v=((a[i]&(1<<j))>0);
                if(go[now][v^1]){
                    ans+=ss[go[now][v^1]]-num[go[now][v^1]]*1ll*sum[i][j][v];
                }
                if(!go[now][v])break;
                now=go[now][v];
            }
            now=1;
            per(j,29,0){
                int v=((a[i]&(1<<j))>0);
                if(!go[now][v])go[now][v]=++tot;
                now=go[now][v];
                ss[now]+=sum[i-1][j][v^1];
                num[now]++;
            }
        }
        cout<<ans<<endl;
    }
    //cerr<<clock()<<endl;
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值