HDU 6059 17多校3 Kanade's trio(字典树)
There are T test cases.
1≤T≤20
1≤∑n≤5∗105
0≤A[i]<230
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]
利用字典树维护前 k-1 个数。当前处理第 k 个数。
显然对于 k 与 i 的最高不相同位 kp 与 ip :
当 ip=0 , kp=1 时,该最高不相同位之前的 ihigher=khigher 。则 jhigher 可以为任意数,均不对 i, k 更高位(指最高不相同位之前的高位,后同)的比较产生影响。而此时 jp 位必须为 0 才可保证不等式 (Ai⊕Aj)<(Aj⊕Ak) 成立。
当 ip=1,kp=0 时,jp 位必须为 1 ,更高位任意。
故利用数组 cnt[31][2]
统计每一位为 0 ,为 1 的有多少个(在前 K-1 个数中)。在字典树插入第 k 个数时,同时统计最高不相同位,即对于每次插入的 p 位为 num[p]
(取值 0 或 1),在同父节点对应的 1-num[p]
为根子树的所有节点均可作为 i 来寻找 j 以获取对答案的贡献。其中又仅要求 jp 与 ip (ip 值即 1-num[p]
) 相同,故 jp 有 cnt[p][ 1-num[p] ]
种取值方案。
但是,同时需要注意 i 与 j 有在 A 数组的先后关系 (i<j) 需要保证。故在字典树中额外维护一个 Ext
点,记录将每次新加入的点与多少原有点可构成 i, j
关系。在后续计算贡献时去掉。
其余详见代码注释。
#include <iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<cmath>
#include<cstring>
using namespace std;
long long ans1,ans2;
int num[];//每一个数字的二进制转化
int cnt[][];//cnt[i][j]记录全部插入数字第i位为0、1分别的数字 struct trie
{
trie* next[];
int cnt,ext;
trie()
{
next[]=NULL;
next[]=NULL;
cnt=;//拥有当前前缀的数字个数
ext=;//
}
}; void calc(trie* tmp,long long c)
{
ans1+=tmp->cnt*(tmp->cnt-)/;
ans2+=(c-tmp->cnt)*tmp->cnt-tmp->ext;
} void insert(trie* r)
{
int i;
for(i=;i<=;i++)
{
if(r->next[num[i]]==NULL)
r->next[num[i]]= new trie;
if(r->next[-num[i]]!=NULL)
calc(r->next[-num[i]],cnt[i][-num[i]]);
r=r->next[num[i]];
r->cnt++;
r->ext+=cnt[i][num[i]]-r->cnt;
//每个点存下同位同数不同父亲节点的数字个数且序号比本身小的
}
return ;
} int main()
{
int T,n,tmp;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
trie root;
ans1=;//i,j选自同父亲节点
ans2=;//i选自同父亲节点,j选择同位不同数不同父亲节点
memset(cnt,,sizeof(cnt));
while(n--)
{
scanf("%d",&tmp);
for(int i=;i>=;i--)//这样可以保证不同大小的数字前缀都为0
{
num[i]=tmp%;
cnt[i][num[i]]++;
tmp/=;
}
insert(&root);
}
printf("%lld\n",ans1+ans2);
}
return ;
}
HDU 6059 17多校3 Kanade's trio(字典树)的更多相关文章
- hdu6059 Kanade's trio 字典树+容斥
转自:http://blog.csdn.net/dormousenone/article/details/76570172 /** 题目:hdu6059 Kanade's trio 链接:http:/ ...
- HDU 6060 17多校3 RXD and dividing(树+dfs)
Problem Description RXD has a tree T, with the size of n. Each edge has a cost.Define f(S) as the th ...
- HDU 6140 17多校8 Hybrid Crystals(思维题)
题目传送: Hybrid Crystals Problem Description > Kyber crystals, also called the living crystal or sim ...
- HDU 6143 17多校8 Killer Names(组合数学)
题目传送:Killer Names Problem Description > Galen Marek, codenamed Starkiller, was a male Human appre ...
- HDU 6045 17多校2 Is Derek lying?
题目传送:http://acm.hdu.edu.cn/showproblem.php?pid=6045 Time Limit: 3000/1000 MS (Java/Others) Memory ...
- HDU 6124 17多校7 Euler theorem(简单思维题)
Problem Description HazelFan is given two positive integers a,b, and he wants to calculate amodb. Bu ...
- HDU 3130 17多校7 Kolakoski(思维简单)
Problem Description This is Kolakosiki sequence: 1,2,2,1,1,2,1,2,2,1,2,2,1,1,2,1,1,2,2,1……. This seq ...
- HDU 6038 17多校1 Function(找循环节/环)
Problem Description You are given a permutation a from 0 to n−1 and a permutation b from 0 to m−1. D ...
- HDU 6034 17多校1 Balala Power!(思维 排序)
Problem Description Talented Mr.Tang has n strings consisting of only lower case characters. He want ...
随机推荐
- Matlab-2:二分法工具箱
function g=dichotomy(f,tol) %this routine uses bisection to find a zero of user-supplied %continuous ...
- leetcode-algorithms-21 Merge Two Sorted Lists
leetcode-algorithms-21 Merge Two Sorted Lists Merge two sorted linked lists and return it as a new l ...
- Oracle表空间状态
1.表空间只读 查看当前表空间状态 SYS@userdata>column file_name format a60 SYS@userdata>column tablespace_name ...
- 小Z的袜子(hose)
小Z的袜子(hose) 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天,小Z再也无法忍受这恼人的找袜子过程,于是他决定听天由命……具体来说,小Z把这N只袜 ...
- 三、存储过程(Stored Procedure)与游标(Cursor)
一.存储过程 一组为了完成特定功能的SQL语句集,经编译后存储在数据库中,用户通过指定存储过程的名字并给出参数,来执行它. 在大型数据库中,存储过程和触发器具有重要的作用.无论是存储过程还是触发器,都 ...
- RockerMQ消息消费、重试
消息中间件—RocketMQ消息消费(一) 消息中间件—RocketMQ消息消费(二)(push模式实现) 消息中间件—RocketMQ消息消费(三)(消息消费重试) MQ中Pull和Push的两种消 ...
- dubbo 框架和 tomcat 的比较
接触 dubbo 有一段时间,特别想拿 dubbo 和 tomcat 比较一番. tomcat 是 web 服务器,提供 http 服务,当 tomcat 收到浏览器发送的 http 请求时,根据 u ...
- ffmpeg+libmp3lame库源码安装教程(CentOS)
lame--libmp3lame的安装包,支持MP3编码:yasm--NASM的重写,用于编译ffmpeg. 1.下载 ffmpeg下载链接:http://ffmpeg.org/download.ht ...
- HTTP、TCP、UDP的区别
TCP.UDP的区别 1.TCP面向连接(如打电话要先拨号建立连接);UDP是无连接的,即发送数据之前不需要建立连接 2.TCP提供可靠的服务.也就是说,通过TCP连接传送的数据,无差错,不丢失,不重 ...
- Python自然语言处理---信息提取
1.数据 目前的数据总体上分为结构化和非结构化的数据.结构化的数据是指实体和关系的规范和可预测的组织.大部分的需要处理的数据都属于非结构化的数据. 2.信息提取 简言之就是从文本中获取信息意义的方法. ...