Codeforces 241B Friends 字典树

Posted cjlhy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces 241B Friends 字典树相关的知识,希望对你有一定的参考价值。

Friends

首先确定第 m 大的是谁, 建出字典树之后二分去check, 找到第 m 大之后, 在跑一次字典树去统计总和。

为什么这个要取模啊, 卡了我半天。

#include<bits/stdc++.h>
#define LL long long
#define LD long double
#define ull unsigned long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ALL(x) (x).begin(), (x).end()
#define fio ios::sync_with_stdio(false); cin.tie(0);

using namespace std;

const int N = 5e4 + 10;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double PI = acos(-1);

template<class T, class S> inline void add(T& a, S b) a += b; if(a >= mod) a -= mod;
template<class T, class S> inline void sub(T& a, S b) a -= b; if(a < 0) a += mod;
template<class T, class S> inline bool chkmax(T& a, S b) return a < b ? a = b, true : false;
template<class T, class S> inline bool chkmin(T& a, S b) return a > b ? a = b, true : false;

const int LOG = 30;

int n, a[N];
LL m;
LL sum, num;

struct Trie 
    int ch[N * LOG][2], cnt[N * LOG];
    int f[N * LOG][30];
    int tot;
    void init() 
        tot = 1;
    
    void ins(int x) 
        int u = 1;
        for(int i = LOG - 1; i >= 0; i--) 
            cnt[u]++;
            for(int j = LOG - 1; j >= 0; j--) f[u][j] += (x >> j & 1);
            if(!ch[u][x >> i & 1]) ch[u][x >> i & 1] = ++tot;
            u = ch[u][x >> i & 1];
        
        for(int j = LOG - 1; j >= 0; j--) f[u][j] += (x >> j & 1);
        cnt[u]++;
    
    PLL query(int x, int k, int op) 
        int u = 1;
        int now = 0;
        LL ans = 0;
        LL sum = 0;
        for(int i = LOG - 1; i >= 0 && u; i--) 
            if(x >> i & 1) 
                if(now + (1 << i) >= k) 
                    ans += cnt[ch[u][0]];
                    if(op) 
                        for(int j = LOG - 1; j >= 0; j--) 
                            if(x >> j & 1) sum += 1LL * (1 << j) * (cnt[ch[u][0]] - f[ch[u][0]][j]);
                            else sum += 1LL * (1 << j) * f[ch[u][0]][j];
                        
                    
                    u = ch[u][1];
                 else 
                    u = ch[u][0];
                    now += 1 << i;
                
             else 
                if(now + (1 << i) >= k) 
                    ans += cnt[ch[u][1]];
                    if(op) 
                        for(int j = LOG - 1; j >= 0; j--) 
                            if(x >> j & 1) sum += 1LL * (1 << j) * (cnt[ch[u][1]] - f[ch[u][1]][j]);
                            else sum += 1LL * (1 << j) * f[ch[u][1]][j];
                        
                    
                    u = ch[u][0];
                 else 
                    u = ch[u][1];
                    now += 1 << i;
                
            
        
        if(now == k) ans += cnt[u], sum += 1LL * cnt[u] * now;
        return mk(ans, sum);
    
 trie;

bool check(int x, int op) 
    sum = 0; num = 0;
    for(int i = 1; i <= n; i++) 
        PLL tmp = trie.query(a[i], x, op);
        num += tmp.fi; sum += tmp.se;
    
    num /= 2; sum /= 2;
    return num >= m;



int main() 
    trie.init();
    scanf("%d%lld", &n, &m);
    for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
    for(int i = 1; i <= n; i++) trie.ins(a[i]);
    int low = 0, high = (1 << 30) - 1, up = 0;
    while(low <= high) 
        int mid = low + high >> 1;
        if(check(mid, 0)) up = mid, low = mid + 1;
        else high = mid - 1;
    
    check(up, 1);
    sum -= 1LL * (num - m) * up;
    printf("%lld", sum % mod);
    return 0;


/*
*/

 

以上是关于Codeforces 241B Friends 字典树的主要内容,如果未能解决你的问题,请参考以下文章

[2016-04-04][codeforces][639][A][Bear and Displayed Friends]

codeforces 723A : The New Year: Meeting Friends

codeforces #305 E Mike and friends

Codeforces483B. Friends and Presents(二分+容斥原理)

codeforces 761B Dasha and friends

Codeforces 483B - Friends and Presents(二分+容斥)