[十二省联考2019]D1T1异或粽子

嘟嘟嘟


做这题之前,强烈推荐先把这道题切了P1631序列合并
这两道题思路基本一模一样。


首先把异或处理成前缀异或,然后维护一个大根堆,每一次取出堆顶加到答案里面,然后把堆顶所在元素的次大的异或值放进堆里。这样循环\(k\)次,就是答案。


关键在于对于数\(sum[i]\),怎么找异或第几大。众人皆知是建01trie,然后在trie上像平衡树找第\(k\)大一样二分就可以了。因为对于每一个\(i\),查找的范围是\(0\) ~ \(i - 1\),建\(n\)棵trie树当然不行,所以我们要建一棵可持久化trie树就好啦。


但是有更好的方法。我们之所以要建可持久化trie树,就是因为每一个点的查找范围不同,否则建一棵就够了。那范围为什么不同呢?就是为了怕找重。但重了就是每一个答案算了两遍,所以我们直接建一棵trie树,然后循环\(2k\)次,然后最后的答案除以2不就是真正的答案了吗。

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("") 
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 5e5 + 5;
const int maxt = 1e7 + 5;
const int N = 31;
inline ll read()
{
  ll ans = 0;
  char ch = getchar(), last = ' ';
  while(!isdigit(ch)) last = ch, ch = getchar();
  while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
  if(last == '-') ans = -ans;
  return ans;
}
inline void write(ll x)
{
  if(x < 0) x = -x, putchar('-');
  if(x >= 10) write(x / 10);
  putchar(x % 10 + '0');
}
In void MYFILE()
{
#ifndef mrclr
  freopen("xor.in", "r", stdin);
  freopen("xor.out", "w", stdout);
#endif
}

int n, K;
ll a[maxn], sum[maxn];

ll b[maxn], cnt = 0;
In void work0()
{
  for(int i = 1; i <= n; ++i)
    {
      ll sum = 0;
      for(int j = i; j <= n; ++j)
    sum ^= a[j], b[++cnt] = sum;
    }
  sort(b + 1, b + cnt + 1);
  ll ans = 0;
  for(int i = cnt; i >= cnt - K + 1; --i) ans += b[i];
  write(ans), enter;
}

struct Node
{
  ll val, num; int rk;
  In bool operator < (const Node& oth)const
  {
    return val < oth.val;
  }
};
priority_queue<Node> q;

struct Tree
{
  int ch[2], siz;
}t[maxt];
int root, tcnt = 0;
In void insert(int& now, ll x, int d)
{
  if(!now) now = ++tcnt;
  if(d == -1) {++t[now].siz; return;}
  insert(t[now].ch[(x >> d) & 1], x, d - 1);
  t[now].siz = t[t[now].ch[0]].siz + t[t[now].ch[1]].siz;
}
In ll query(ll x, int k)
{
  ll ret = 0; int now = root;
  for(int i = N; i >= 0; --i)
    {
      int p = (x >> i) & 1;
      if(t[t[now].ch[p ^ 1]].siz >= k) now = t[now].ch[p ^ 1], ret |= ((1LL * 1) << i);
      else k -= t[t[now].ch[p ^ 1]].siz, now = t[now].ch[p];
    }
  return ret;
}

int main()
{
  //MYFILE();
  n = read(), K = read();
  for(int i = 1; i <= n; ++i) a[i] = read();
  if(n <= 5000) {work0(); return 0;}
  for(int i = 1; i <= n; ++i) sum[i] = sum[i - 1] ^ a[i];
  for(int i = 0; i <= n; ++i) insert(root, sum[i], N);
  for(int i = 0; i <= n; ++i)
    q.push((Node){query(sum[i], 1), sum[i], 1});
  ll ans = 0;
  for(int i = 1; i <= (K << 1); ++i)
    {
      Node tp = q.top(); q.pop();
      ans += tp.val;
      if(tp.rk <= n) q.push((Node){query(tp.num, tp.rk + 1), tp.num, tp.rk + 1});
    }
  write(ans >> 1), enter;
  return 0;
}
posted @ 2019-04-28 17:18  mrclr  阅读(311)  评论(0编辑  收藏  举报