Strange Memory Gym - 102832F

Posted Jozky86

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Strange Memory Gym - 102832F相关的知识,希望对你有一定的参考价值。

Strange Memory Gym - 102832F

题意:

有一颗n个节点的树,求下面公式的值:

题解:

a ⊕ b = c 可以推出a ⊕ c = b
那么ai⊕aj= a l c a ( i , j ) a_{lca(i,j)} alca(i,j),可以得到:ai⊕ a l c a ( i , j ) a_{lca(i,j)} alca(i,j)=aj,ai就是我们要找到元素,也就是说在根为lca(i,j)的子树里,我们要找到值为aj的下标j,然后用j去和i异或,这样的复杂度是 O ( n 2 l o g n ) O(n^2logn) O(n2logn)
我们需要一个方法,在知道val的情况下,快速计算答案
根据异或的性质,相同为0,不同为1,那我们用一个桶结构来存,cnt[val][i][0/1]=num:表示在集合里权值为val的第i位有num个0或num个1
每一位与i不同的有多少,然后乘上相应的2的p次幂就可以
就比如aj的第k位与ai的第k位不一样,那么说明aj⊕ai,第k位就是1,就会贡献答案(1<<(k-1)),有num个aj,就会贡献答案num * (1<<(k-1))

代码:

// Problem: F - Strange Memory
// Contest: Virtual Judge - 2020CCPC长春(2021/7/9训练)
// URL: https://vjudge.net/contest/446212#problem/F
// Memory Limit: 262 MB
// Time Limit: 1000 ms
// Data:2021-08-23 19:30:17
// By Jozky

#include <bits/stdc++.h>
#include <unordered_map>
#define debug(a, b) printf("%s = %d\\n", a, b);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
clock_t startTime, endTime;
//Fe~Jozky
const ll INF_ll= 1e18;
const int INF_int= 0x3f3f3f3f;
void read(){};
template <typename _Tp, typename... _Tps> void read(_Tp& x, _Tps&... Ar)
{
    x= 0;
    char c= getchar();
    bool flag= 0;
    while (c < '0' || c > '9')
        flag|= (c == '-'), c= getchar();
    while (c >= '0' && c <= '9')
        x= (x << 3) + (x << 1) + (c ^ 48), c= getchar();
    if (flag)
        x= -x;
    read(Ar...);
}
template <typename T> inline void write(T x)
{
    if (x < 0) {
        x= ~(x - 1);
        putchar('-');
    }
    if (x > 9)
        write(x / 10);
    putchar(x % 10 + '0');
}
void rd_test()
{
#ifdef LOCAL
    startTime= clock();
    freopen("in.txt", "r", stdin);
#endif
}
void Time_test()
{
#ifdef LOCAL
    endTime= clock();
    printf("\\nRun Time:%lfs\\n", (double)(endTime - startTime) / CLOCKS_PER_SEC);
#endif
}
const int maxn= 1e5 + 9;
int a[maxn];
int siz[maxn];
int son[maxn];
int cnt[maxn * 15][21][2];
vector<int> vec[maxn];
void dfs1(int u, int fa)
{
    siz[u]= 1;
    for (auto v : vec[u]) {
        if (v == fa)
            continue;
        dfs1(v, u);
        siz[u]+= siz[v];
        if (siz[v] > siz[son[u]])
            son[u]= v;
    }
}
ll sum= 0;
int Son;
int base[22];
void cal(int x, int fa, int num)
{
    int val= a[x] ^ num;
    for (int i= 0; i <= 20; i++) {
        int f= (!((x >> i) & 1));
        sum+= 1ll * base[i] * cnt[val][i][f];
    }
    for (auto v : vec[x]) {
        if (v == fa || v == Son)
            continue;
        cal(v, x, num);
    }
}
void add(int u, int fa, int val)
{
    for (int i= 0; i <= 20; i++) {
        cnt[a[u]][i][(u >> i) & 1]+= val;
    }
    for (auto v : vec[u]) {
        if (v == fa || v == Son)
            continue;
        add(v, u, val);
    }
}
void dfs2(int u, int fa, int keep)
{
    for (auto v : vec[u]) {
        if (v != fa && v != son[u])
            dfs2(v, u, 0);
    }
    if (son[u]) {
        dfs2(son[u], u, 1);
        Son= son[u];
    }
    for (auto v : vec[u]) {
        if (v == fa || v == Son)
            continue;
        cal(v, u, a[u]);
        add(v, u, 1);
    }
    for (int i= 0; i <= 20; i++) {
        cnt[a[u]][i][(u >> i) & 1]+= 1;
    }
    Son= 0;
    if (!keep) {
        add(u, fa, -1);
    }
}
int main()
{
    //rd_test();
    int n;
    base[0]= 1;
    for (int i= 1; i <= 20; i++)
        base[i]= base[i - 1] * 2;
    cin >> n;
    for (int i= 1; i <= n; i++)
        cin >> a[i];
    for (int i= 1; i < n; i++) {
        int u, v;
        read(u, v);
        vec[u].push_back(v);
        vec[v].push_back(u);
    }
    dfs1(1, 0);
    dfs2(1, 0, 0);
    printf("%lld\\n", sum);
    return 0;
    //Time_test();
}

以上是关于Strange Memory Gym - 102832F的主要内容,如果未能解决你的问题,请参考以下文章

Gym101138D Strange Queries 莫队前缀和容斥

[2020-CCPC Changchun Onsite]-F. Strange Memory(dsu on tree)

How Much Memory Your Code Is Using? Gym - 101955J

UVA10519 Really Strange大数+递推

国赛 strange_int

ruby sidetiq_strange.rb