根号分治

· · 题解

笑死,瞪着大眼 5 分钟才看见 \sum d_i\le5\times10^7。

本题考虑根号分治,小于 10000 的数最多有 10000 种,大于 10000 的数最多有 5000 个,枚举 10000 以内的数两两之间对答案产生的贡献,10000 以内的数与 10000 以外的数间对答案的贡献(这个贡献需要乘 2,这个贡献需要乘 2,这个贡献需要乘 2,重要的事情说三遍,我因为这个调了 5 分钟),10000 以外的数两两之间对答案产生的贡献,加和即可。

废话不多说,直接看代码:

// Problem: T412755 [LMXOI Round 1] Size
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/T412755?contestId=152140
// Memory Limit: 512 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
//#include <bits/extc++.h>
#define int long long
#define min(x,y) (((x)<(y))?(x):(y))
#define max(x,y) (((x)>(y))?(x):(y))
#define lowbit(x) ((x)&-(x))
#define abs(x) (((x)<(0))?(-(x)):(x))
using namespace std;
//using namespace __gnu_cxx;
//using namespace __gnu_pbds;
int quikpower(int a, int b)
{
//  if (b < 0)
//  {
//      return quikpower(a, b + mod - 1);
//  }
    int c = 1;
    while (b)
    {
        if (b & 1)
        {
            c *= a;
//          c %= mod;
        }
        a *= a;
//      a %= mod;
        b >>= 1;
    }
    return c;
}
int countbit(int x)
{
    // cout << x << "\n";
    int ans = 0;
    while (x)
    {
        ans += (x & 1);
        x >>= 1;
    }
    return ans;
}
int n, x, box[10005], ans;
map<int,int> st;
signed main()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> x;
        if (x <= 10000)
        {
            box[x]++;
        }
        else
        {
            st[x]++;
        }
    }
    for (int i = 1; i <= 1000; i++)
    {
        for (pair<int,int> j : st)
        {
            ans += countbit(i + j.first) * box[i] * j.second + countbit(abs(i - j.first)) * box[i] * j.second;
        }
    }
    ans *= 2;
    for (int i = 0; i <= 10000; i++)
    {
        for (int j = 0; j <= 10000; j++)
        {
            ans += countbit(i + j) * box[i] * box[j] + countbit(abs(i - j)) * box[i] * box[j];
        }
    }
    for (pair<int,int> i : st)
    {
        for (pair<int,int> j : st)
        {
            ans += countbit(i.first + j.first) * i.second * j.second + countbit(abs(i.first - j.first)) * i.second * j.second;
        }
    }
    cout << ans;
    return 0;
}

提交之后,你会发现全 T 了,我也不知道为啥,但是将分治边界改为 1000 就跑地飞快,可能数据不随机?