hdu6059 Kanade's trio(字典树)
来源:互联网 发布:wps vb编辑器下载 编辑:程序博客网 时间:2024/06/05 01:08
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6059
题意:
给你一组数,让你在其中找到多少个三元组,ai,aj,ak满足ai^aj<=aj^ak。
解:
我们需要判定ai^aj<=aj^ak的话,那么ai和ak的前t-1位都相等的话,第t位不相等时,只有ai[t]==aj[t],ak[t]!=aj[t]时成立。
那么我们利用字典树进行储存数据,然后记录每个数的相同的位数以及最高不同位,进行每位不相同出现的次数。
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <algorithm>using namespace std;const int maxn=5e5+10;const int node=maxn*31;int cnt[31][2],num[30];long long ext=0,ans;int size;struct Tree{ int son[2]; int cnt,ext;}tree[node];int a[maxn];typedef long long ll;void cal(int tmp,long long c){ ans+= tree[tmp].cnt*1ll*(tree[tmp].cnt-1)/2; ext+=(c-tree[tmp].cnt)*1ll*tree[tmp].cnt-tree[tmp].ext;}void insert(int x){ int tmp=0; for(int i=0;i<30;i++) { if(!tree[tmp].son[num[i]]){ tree[tmp].son[num[i]]=++size; } if(tree[tmp].son[1-num[i]]) { cal(tree[tmp].son[1-num[i]],cnt[i][1-num[i]]); } tmp = tree[tmp].son[num[i]]; tree[tmp].cnt++; tree[tmp].ext += cnt[i][num[i]] - tree[tmp].cnt; } return ;}int main(){ int t,n; scanf("%d",&t); while(t--) { scanf("%d",&n); memset(tree,0,sizeof(tree)); memset(cnt,0,sizeof(cnt)); size=0,ans=0,ext=0; for(int i=1;i<=n;i++) { scanf("%d",&a[i]); int tmp = a[i]; for(int j=29;j>=0;j--) { num[j] = tmp%2; cnt[j][tmp%2]++; tmp/=2; } insert(i); } printf("%lld\n",ans+ext); } return 0;}
阅读全文
0 0
- hdu6059 Kanade's trio(字典树)
- HDU6059 Kanade's trio(异或字典树)
- HDU6059-Kanade's trio
- HDU6059 Kanade's trio (Trie)
- HDU 6059 Kanade's trio 字典树
- Kanade's trio hdu 6059 字典树
- hdu 6059 Kanade's trio(字典树)
- HDU 6059 Kanade's trio【字典树】
- HDU 6059 Kanade's trio 字典树 统计 容斥
- HDU-6059 Kanade's trio(字典树)
- HDU 6059 Kanade's trio(字典树)
- hdu 6059 Kanade's trio(字典树+位压缩)
- HDU 6059 Kanade's trio (字典树)
- 2017多校联合第三场/hdu6059 Kanade's trio(tire tree)
- Kanade's trio
- Kanade's trio
- HDU 6059 Kanade's trio (字典树, 2017 Multi-Univ Training Contest 3)
- HDU 6059-Kanade's trio(多校训练第三场->01字典树)
- C# 调用wav文件播放音乐
- Arduino学习日记四
- VS配置文件,风格!
- 如何更改linux下的Apache端口号(亲测有效)
- 关于RPC的介绍
- hdu6059 Kanade's trio(字典树)
- Tyvj1034
- iptables--命令行解析
- 百度网盘视频解析,获取真实源地址
- 十四、bootstrap-table 展示数据
- WOJ1049-Words
- @SuppressWarnings注解用法详解
- Zero Cost—40G Migration with QSFP BiDi Transceiver
- 解决0x00000FD:Stack overflow(参数:0x00000000,0x002F2000)栈溢出问题