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 ...
随机推荐
- DOM与document的区别
DOM: DOM 全称是 Document Object Model,也就是文档对象模型. DOM 就是针对 HTML 和 XML 提供的一个API.什么意思?就是说为了能以编程的方法操作这个 HTM ...
- 牛客练习赛32-D-MST+tarjin割边
链接:https://ac.nowcoder.com/acm/contest/272/D来源:牛客网 题目描述 小p和他的朋友约定好去游乐场游玩,但是他们到了游乐场后却互相找不到对方了. 游乐场可以看 ...
- Leetcode 863. 二叉树中所有距离为 K 的结点
863. 二叉树中所有距离为 K 的结点 显示英文描述 我的提交返回竞赛 用户通过次数39 用户尝试次数59 通过次数39 提交次数174 题目难度Medium 给定一个二叉树(具有根结点 ro ...
- python爬虫基本原理及入门
爬虫:请求目标网站并获得数据的程序 爬虫的基本步骤: 使用python自带的urllib库请求百度: import urllib.request response = urllib.request.u ...
- git相关知识(github,idea等的配置)
本地git提交文件到github上: 1.在github上创建项目 2.使用git clone https://github.com/xxxxxxx/xxxxx.git克隆到本地 3.编辑项目 4.g ...
- 九、持久层框架(MyBatis)
一.基于MyBatis的对象关系配置(基于XML方式的配置) 注: MyBatis不能像Hibernate那样,在实体类上配置上注解或者配置xml映射文件,系统启动后就可以自动创建表.因为MyBati ...
- cogs448
☆ 输入文件:1.in 输出文件:1.out 简单对比时间限制:1 s 内存限制:128 MB [题目描述] 在某次膜拜大会上,一些神牛被要求集体膜拜.这些神牛被奖励每人吃一些神牛果. ...
- Object转Integer,String
object先转为字符串,然后通过int 的封装类(Integer)的pasreInt()方法转为int 例如: Object ob = 123; Integer.parseInt(String.v ...
- 【内存泄漏】 C/C++内存泄漏及其检测工具
对于一个c/c++程序员来说,内存泄漏是一个常见的也是令人头疼的问题.已经有许多技术被研究出来以应对这个问题,比如 Smart Pointer,Garbage Collection等.Smart Po ...
- Spring Boot 如何极简入门?
Spring Boot已成为当今最流行的微服务开发框架,本文是如何使用Spring Boot快速开始Web微服务开发的指南,我们将创建一个可运行的包含内嵌Web容器(默认使用的是Tomcat)的可运行 ...