hdu6059[字典树+思维] 2017多校3

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int trie[31 * 500005][2];
int no[31 * 500005];
int sz[31 * 500005];
int sum[31][2];
int T, n, temp, tot = 0;
LL ans = 0;
void solve(int x) {
    int now = 0;
    for (int i = 29; ~i; i--) {
        int bit = (x & (1 << i)) ? 1 : 0;
        sum[i][bit]++;
        if (!trie[now][bit]) {
            trie[now][bit] = ++tot;
        }
        if (trie[now][1 ^ bit]) { //存在兄弟节点,即有符合的(Ai,Aj)
            int bro = trie[now][1 ^ bit];
            ans += 1LL * sz[bro] * (sz[bro] - 1) / 2;
            ans += 1LL * (sum[i][1 ^ 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, 0, sizeof(trie));
    memset(no, 0, sizeof(no));
    memset(sum, 0, sizeof(sum));
    memset(sz, 0, sizeof(sz));
    ans = 0, tot = 0;
}
int main() {
    scanf("%d", &T);
    while (T--) {
        init();
        scanf("%d", &n);
        for (int i = 0; i < n; i++) {
            scanf("%d", &temp);
            solve(temp);
        }
        printf("%lld\n", ans);
    }
}
时间: 2024-08-01 13:13:31

hdu6059[字典树+思维] 2017多校3的相关文章

hdu6035[dfs+思维] 2017多校1

/*hdu6035[dfs+思维] 2017多校1*/ //合并色块, 妙啊妙啊 #include<bits/stdc++.h> using namespace std; const double eps=1e-8; const int inf=0x3f3f3f3f; typedef long long LL; vector<int>G[200005]; LL sum[200005]; int c[200005],son[200005],mark[200005]; int n,u,

hdu6074[并查集+LCA+思维] 2017多校4

看了标答感觉思路清晰了许多,用并查集来维护全联通块的点数和边权和. 用另一个up[]数组(也是并查集)来保证每条边不会被重复附权值,这样我们只要将询问按权值从小到大排序,一定能的到最小的边权和与联通块中的点数. 下面是过程分析.过程中一共运用了两个并查集. [数据]110 31 21 32 42 53 63 74 85 95 109 6 3 1 501 4 2 5 209 10 8 10 40[分析]首先我们将图构建出来我们将m次询问按权值从小到大排序后: 1 4 2 5 209 10 8 10

HDU6038-Function-数学+思维-2017多校Team01

学长讲座讲过的,代码也讲过了,然而,当时上课没来听,听代码的时候也一脸o((⊙﹏⊙))o 我的妈呀,语文不好是硬伤,看题意看了好久好久好久(死一死)... 数学+思维题,代码懂了,也能写出来,但是还是有一点不懂,明天继续. 感谢我的队友不嫌弃我是他的猪队友(可能他心里已经骂了无数次我是猪队友了_(:з」∠)_ ) Function Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Other

hdu6059 Kanade&#39;s trio 字典树+容斥

转自:http://blog.csdn.net/dormousenone/article/details/76570172 /** 题目:hdu6059 Kanade's trio 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6059 题意:含 N 个数字的 A 数组,求有多少个三元组 (i,j,k) 满足 i<j<k 且 (Ai⊕Aj)<(Aj⊕Ak) 思路: 利用字典树维护前 k-1 个数.当前处理第 k 个数. 显然对于 k 与 i 的

HDU 6059 17多校3 Kanade&#39;s trio(字典树)

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 fi

2017广西邀请赛 Query on A Tree (可持续化字典树)

Query on A Tree 时间限制: 8 Sec  内存限制: 512 MB提交: 15  解决: 3[提交][状态][讨论版] 题目描述 Monkey A lives on a tree. He always plays on this tree.One day, monkey A learned about one of the bit-operations, xor. He was keen of this interesting operation and wanted to pr

Trie树/字典树题目(2017今日头条笔试题:异或)

1 /* 2 本程序说明: 3 4 [编程题] 异或 5 时间限制:1秒 6 空间限制:32768K 7 给定整数m以及n各数字A1,A2,..An,将数列A中所有元素两两异或,共能得到n(n-1)/2个结果,请求出这些结果中大于m的有多少个. 8 输入描述: 9 第一行包含两个整数n,m. 10 11 第二行给出n个整数A1,A2,...,An. 12 13 数据范围 14 15 对于30%的数据,1 <= n, m <= 1000 16 17 对于100%的数据,1 <= n, m,

[2019杭电多校第五场][hdu6625]three arrays(01字典树)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6625 大意为给你两个数组a和b,对应位置异或得到c数组,现在可以将a,b数组从新排序求c数组,使得字典序最小. 大致的做法就是用两个数组中的数字二进制 建两颗字典树,同时记录每个位置的个数.然后在两颗字典树上同时dfs,优先往0-0和1-1方向走,不能走再走0-1,1-0方向. 因为0-0和1-1两种情况不分先后,所以走出来的不一定是最小的,走完得到的c数组要排序. 1 #include<iostr

hiho #1014 : Trie树 (字典树的建立和查找)

#1014 : Trie树 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编程的学习道路上一同前进. 这一天,他们遇到了一本词典,于是小Hi就向小Ho提出了那个经典的问题:"小Ho,你能不能对于每一个我给出的字符串,都在这个词典里面找到以这个字符串开头的所有单词呢?" 身经百战的小Ho答道:"怎么会不能呢!你每给我一个字符串,我就依次遍历词典里的所有