题目链接

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.

1≤T≤20

1≤∑n≤5∗105

0≤A[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[1]~a[n] ,求有多少个三元组(i,j,k) 满足1<=i<j<k<=n  且  a[i]异或a[j] < a[j]异或a[k]?

思路:对于a[i]与a[k],对于二进制从高位向低位进行判断,如果30位(A[i]<2^30)到25位相同,那么a[j]的这些位不管值是多少不影响异或后 a[i] 与 a[k] 的大小关系,现在第24位不同,那么a[j]的这一位必须和a[i]相同,这样a[k]异或a[j]的值一定大于a[i]异或a[j] ,第23位到第0位不管a[j]取何值不会影响大小关系了。  有上述可以得出我们只需要判断a[i]和a[k]的二进制最高不相同位就行,那么可以用一个二进制的字典树存储这n个数。

从a[i]~a[n]将a[k]插入字典树中,每次插入时需要记录 当前节点有多少数(num表示)、当前节点对应的a[j]有多少(count表示),用cn[32][2]记录第i位为0和1时的a[j]的个数,所以每次到一个节点时用count+=cn[i][1-t],表示当前的位(0或1),这样可以保证j<k,但是没有保证i<j ;

接下来将cn[][]清空,从a[1]~a[n]的进行删除,对于a[i]删除,可以保证i<k ,那么可以用count-num*cn[i][t] 保证i<j ;

代码如下:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
const int N=5e5+;
int a[N],p[],cn[][];
struct node
{
node *son[];
int count;
int num;
node() { count=; num=; son[]=son[]=NULL; }
};
node *root; void add(int x,int v)
{
node * now=root;
for(int i=;i>=;i--)
{
int t=(!!(p[i]&x));
if(now->son[t]==NULL) now->son[t]=new node();
now=now->son[t];
now->num+=v;
cn[i][t]++;
now->count+=v*cn[i][-t];///当前点对应的j的个数;
}
} LL cal(int x)
{
node * now=root;
LL sum=;
for(int i=;i>=;i--)
{
int t=(!!(p[i]&x));
node* bro=now->son[-t];
if(bro)
sum+=bro->count - ((LL)bro->num*(LL)cn[i][t]);
now=now->son[t];
if(!now) break;
}
return sum;
} int main()
{
///cout << "Hello world!" << endl;
int T; cin>>T;
p[]=;
for(int i=;i<;i++) p[i]=p[i-]<<;
while(T--)
{
int n; scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d",&a[i]);
root=new node();
memset(cn,,sizeof(cn));
for(int i=;i<=n;i++) add(a[i],);
memset(cn,,sizeof(cn));
LL ans=;
for(int i=;i<n;i++){
add(a[i],-);
ans+=cal(a[i]);
}
printf("%lld\n",ans);
}
return ;
}

hdu 6059---Kanade's trio(字典树)的更多相关文章

  1. HDU 6059 - Kanade's trio | 2017 Multi-University Training Contest 3

    思路来自题解(看着题解和标程瞎吉尔比划了半天) /* HDU 6059 - Kanade's trio [ 字典树 ]  |  2017 Multi-University Training Conte ...

  2. hdu 6059 Kanade's trio(字典树)

    Kanade's trio Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)T ...

  3. hdu6059 Kanade's trio 字典树+容斥

    转自:http://blog.csdn.net/dormousenone/article/details/76570172 /** 题目:hdu6059 Kanade's trio 链接:http:/ ...

  4. hdu 6059 Kanade's trio

    题 OwO http://acm.hdu.edu.cn/showproblem.php?pid=6059 解 由于每个数字最多是30位,枚举数字每一位考虑, 建一棵记录前缀(位的前缀,比如10拆成10 ...

  5. HDU 4287 Intelligent IME(字典树数组版)

    Intelligent IME Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  6. HDU 4757 Tree 可持久化字典树

    Tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4757 Des ...

  7. hdu 4099 Revenge of Fibonacci 字典树+大数

    将斐波那契的前100000个,每个的前40位都插入到字典树里(其他位数删掉),然后直接查询字典树就行. 此题坑点在于 1.字典树的深度不能太大,事实上,超过40在hdu就会MLE…… 2.若大数加法时 ...

  8. HDU 1247 - Hat’s Words - [字典树水题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1247 Problem DescriptionA hat’s word is a word in the ...

  9. HDU 1251 统计难题(字典树模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=1251 题意:给出一些单词,然后有多次询问,每次输出以该单词为前缀的单词的数量. 思路: 字典树入门题. #inc ...

随机推荐

  1. jqery 图片等 比例缩放

    后台添加加载图片的方法(从编辑器里为图上添加一个名为jzsmsimglightbox样式的类) public string GetHtmlImageUrlList(string sHtmlText) ...

  2. ecshop加入购物车效果(各个页面)

    ecshop中点击加入购物车出现下图 通过以下代码改成下图效果 1.后台网店设置 购物车确定提示  选择为“提示用户,点击“确定”进购物车” 2.打开js/common.js  104行就是funct ...

  3. Codeforces Round #419 (Div. 2)

    1.题目A:Karen and Morning 题意: 给出hh:mm格式的时间,问至少经过多少分钟后,该时刻为回文字符串? 思路: 简单模拟,从当前时刻开始,如果hh的回文rh等于mm则停止累计.否 ...

  4. Sqlserver2005 破解版下载地址

    Sqlserver2005 破解版下载地址:http://www.xiaidown.com/soft/from/1583.html

  5. Swift字符串可变性

    您可以通过将一个特定字符串分配给一个变量来对其进行修改,或者分配给一个常量来保证其不会被修改: var variableString = "Horse" variableStrin ...

  6. 将Win7系统将默认打开的库改为计算机

  7. [leetcode-628-Maximum Product of Three Numbers]

    Given an integer array, find three numbers whose product is maximum and output the maximum product. ...

  8. 一个"Median Maintenance"问题

    题目要求: Download the text file here. The goal of this problem is to implement the "Median Mainten ...

  9. 【CC2530入门教程-06】CC2530的ADC工作原理与应用

    第6课  CC2530的ADC工作原理与应用 广东职业技术学院  欧浩源 一.A/D转换的基本工作原理 将时间上连续变化的模拟量转化为脉冲有无的数字量,这一过程就叫做数字化,实现数字化的关键设备是AD ...

  10. Mac用ssh登录Ubuntu14.04

    在Ubuntu上配置ssh-server sudo apt-get install openssh-server  然后确认ssh-server是否启动  ps -e | grep ssh 如果存在s ...