hdu6059[字典树+思维] 2017多校3
2024-08-30 11:47:41
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int trie[ * ][];
int no[ * ];
int sz[ * ];
int sum[][];
int T, n, temp, tot = ;
LL ans = ;
void solve(int x) {
int now = ;
for (int i = ; ~i; i--) {
int bit = (x & ( << i)) ? : ;
sum[i][bit]++;
if (!trie[now][bit]) {
trie[now][bit] = ++tot;
}
if (trie[now][ ^ bit]) { //存在兄弟节点,即有符合的(Ai,Aj)
int bro = trie[now][ ^ bit];
ans += 1LL * sz[bro] * (sz[bro] - ) / ;
ans += 1LL * (sum[i][ ^ bit] - sz[bro]) * sz[bro] - no[bro];
}
now = trie[now][bit];
sz[now]++;
no[now] += sum[i][bit] - sz[now];//除去不符合j>i的(Ai,Aj),要累加
//cout << now << ' ' << no[now] << endl;
}
}
void init() {
memset(trie, , sizeof(trie));
memset(no, , sizeof(no));
memset(sum, , sizeof(sum));
memset(sz, , sizeof(sz));
ans = , tot = ;
}
int main() {
scanf("%d", &T);
while (T--) {
init();
scanf("%d", &n);
for (int i = ; i < n; i++) {
scanf("%d", &temp);
solve(temp);
}
printf("%lld\n", ans);
}
}
最新文章
- w3svc服务启动 不了,错误 1068:依赖服务或组件无法启动
- poj1417 带权并查集 + 背包 + 记录路径
- winform(容器、打印、对话框)
- mysql完整备份时过滤掉某些库
- ES6-函数扩展
- hadoop作业调优参数整理及原理(转)
- Eclipse Gtk+
- java如何追加写入txt文件
- 【Android】跟着教程做の学习笔记
- Android多媒体数据库之MediaStore研究
- 结构体buf_block_t
- C# and JSON
- vcastr2.0插件超级简单使用
- MFC获取当前时间
- ASP.NET导入Excel到SQL数据库
- Ffmpeg和SDL如何同步视频(转)
- hadoop 学习
- asp.net中实现MD5加密、解密的方法
- String详解说明
- [2]十道算法题【Java实现】