1 条题解
-
0
MBC1D 题解
原题是联合省选 2019 异或粽子。
一道考察 01-Trie 的一些 trick 的题目。
60 pts
一个简单的想法是我们先求出 ,那么一个区间 的值就可以表示为 (由异或性质,)。
然后我们注意到数据范围里 有 60 pts,那么我们可以在 的复杂度下暴力求出前 大区间,于是即有 60pts。
code:
#include <bits/stdc++.h> #define ui unsigned int #define ull unsigned long long using namespace std; const int N = 5e5 + 6; int n, k; ui a[N], s[N]; priority_queue<ui> q; ull ans; inline ui rd() { ui x = 0; char ch = getchar(); while (ch < '0' || ch > '9') ch = getchar(); while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x; } int main() { freopen("last.in","r",stdin); freopen("last.out","w",stdout); cin >> n >> k; for (int i = 1; i <= n; i++) a[i] = rd(), s[i] = s[i-1] ^ a[i]; for (int l = 1; l <= n; l++) for (int r = l; r <= n; r++) q.push(s[r] ^ s[l-1]); while (k--) ans += q.top(), q.pop(); cout << ans << endl; return 0; }80 pts
这一档分是给被卡常的可持久化 Trie 的,由于比正解复杂且可持久化思想不是提高级内容,这里暂且跳过。
有兴趣的同学可以自行搜索“可持久化 Trie”相关内容。
code:
#include <cstdio> #include <queue> #define RI register int using namespace std; const int N = 500005; inline unsigned int read() { unsigned int x = 0 ; char ch = getchar(); while(ch < '0' || ch > '9') { ch = getchar();} while(ch >= '0' && ch <= '9') {x = (x << 3) + (x << 1) + (ch ^ 48); ch = getchar();} return x; } int n , k , cnt; unsigned int a[N]; long long ans; struct Persistent_Trie { struct node { node *ch[2]; int lt; node() {ch[0]=ch[1]=NULL;lt = 0;} }*root[N] , pool[N * 36] , *tail; Persistent_Trie(){tail = pool;} void build(register node *&p,RI dep) { p = new (tail ++)node(); if(dep < 0) return; build(p -> ch[0],dep-1); } void insert(register node *&p,register node *o,RI i,RI dep) { p = new (tail ++)node(); if(o) *p = *o; if(dep < 0) return (void)(p -> lt = i); register bool tmp = (a[i] >> dep) & 1; if(tmp) insert(p -> ch[1],o ? o -> ch[1] : NULL,i,dep - 1); else insert(p -> ch[0],o ? o -> ch[0] : NULL,i,dep - 1); if(p -> ch[0]) p -> lt = max(p -> lt,p -> ch[0] -> lt); if(p -> ch[1]) p -> lt = max(p -> lt,p -> ch[1] -> lt); } int query(register node *p,register unsigned int x,RI dep,RI limit) { if(dep < 0) return p -> lt; register bool tmp = (x >> dep) & 1; if(p -> ch[tmp ^ 1] && p -> ch[tmp ^ 1] -> lt >= limit) return query(p -> ch[tmp ^ 1],x,dep-1,limit); return query(p -> ch[tmp],x,dep - 1,limit); } struct zz { int t , l , r , pos; unsigned int val; friend bool operator < (const zz & a,const zz & b) {return a.val < b.val;} }; priority_queue<zz> q; inline void LOL() { n = read(); k = read(); build(root[0],31); RI i; for(i = 1;i <= n;i ++) { a[i] = a[i-1] ^ read(); insert(root[i],root[i-1],i,31); cnt = query(root[i-1],a[i],31,0); q.push((zz){i,1,i,cnt,a[i] ^ a[cnt]}); } zz res; for(i = 1;i <= k;i ++) { res = q.top(); ans += (long long)res.val; q.pop(); if(res.pos + 1 < res.r) { cnt = query(root[res.r-1],a[res.t],31,res.pos+1); q.push((zz){res.t,res.pos+2,res.r,cnt,a[res.t] ^ a[cnt]}); } if(res.l < res.pos + 1) { cnt = query(root[res.pos-1],a[res.t],31,res.l-1); q.push((zz){res.t,res.l,res.pos,cnt,a[res.t] ^ a[cnt]}); } } printf("%lld\n",ans); } }DNF; int main() { freopen("last.in","r",stdin); freopen("last.out","w",stdout); DNF.LOL(); return 0; }100 pts
下面讲解正解。
可以发现,我们可以用 01-Trie 求出与 配对之后结果第 大的 ,这可以通过维护 Trie 上每个节点被多少个数经过来决定向哪里走下一步。
为了消除 的大小顺序,我们令 ,构造对偶,并把最终答案 。
接下来我们考虑如何求出前 大的区间。
首先我们用一个优先队列维护区间权值大小,先把对于每个 配对之后最大的 的运算结果塞进去。
然后每次取出堆顶,设这次是与 配对第 大的结果,那么我们寻找与 配对第 大的结果并塞进去。
如此循环 次即可。
code:
#include<bits/stdc++.h> #pragma GCC optimize(3) #define pb(a) push_back(a) #define mp(a,b) make_pair(a,b) #define mem(a,b) memset(a,b,sizeof(a)) #define N 500005 using namespace std; typedef unsigned int uint; typedef long long LL; int n,m,i,j,k,cnt; uint a[N]; struct node{ int s[2],cnt; node(){s[0]=s[1]=cnt=0;} } tr[N*32];//这里千万不要忘了乘32 #define root 0 void build(int u,uint x,uint t){ tr[u].cnt++; bool f=t&x; if (!t) return; if (!tr[u].s[f]){ tr[u].s[f]=++cnt; build(cnt,x,t>>1); } else build(tr[u].s[f],x,t>>1); } LL ans=0; vector<int> A,B; void dfs(int dep,int remain,uint now){ if (dep==32 || remain==0) {ans+=1ll*now*remain; return;} LL sum1=0; for (int i=0; i<A.size(); i++) sum1+=(1ll*tr[tr[A[i]].s[0]].cnt*tr[tr[B[i]].s[1]].cnt + 1ll*tr[tr[A[i]].s[1]].cnt*tr[tr[B[i]].s[0]].cnt)/((A[i]==B[i])?2:1); if (sum1<remain){ vector<int> tA=A,tB=B; A.clear(), B.clear(); for (int i=0; i<tA.size(); i++)//1 { if (tr[tA[i]].s[0] && tr[tB[i]].s[1]) A.pb(tr[tA[i]].s[0]), B.pb(tr[tB[i]].s[1]); if (tA[i]==tB[i]) continue;// Warning! if (tr[tA[i]].s[1] && tr[tB[i]].s[0]) A.pb(tr[tA[i]].s[1]), B.pb(tr[tB[i]].s[0]); } dfs(dep+1,sum1,now|(1ll<<31-dep)); A.clear(), B.clear(); for (int i=0; i<tA.size(); i++)//0 { if (tr[tA[i]].s[1] && tr[tB[i]].s[1]) A.pb(tr[tA[i]].s[1]), B.pb(tr[tB[i]].s[1]); if (tr[tA[i]].s[0] && tr[tB[i]].s[0]) A.pb(tr[tA[i]].s[0]), B.pb(tr[tB[i]].s[0]); } dfs(dep+1,remain-sum1,now); } else{ vector<int> tA=A,tB=B; A.clear(), B.clear(); for (int i=0; i<tA.size(); i++)//1 { if (tr[tA[i]].s[0] && tr[tB[i]].s[1]) A.pb(tr[tA[i]].s[0]), B.pb(tr[tB[i]].s[1]); if (tA[i]==tB[i]) continue;// Warning! if (tr[tA[i]].s[1] && tr[tB[i]].s[0]) A.pb(tr[tA[i]].s[1]), B.pb(tr[tB[i]].s[0]); } dfs(dep+1,remain,now|(1ll<<31-dep)); } } int main() { freopen("last.in","r",stdin); freopen("last.out","w",stdout); cin>>n>>k; for (i=1; i<=n; i++) cin>>a[i], a[i]^=a[i-1]; for (i=0; i<=n; i++) build(root,a[i],1ll<<31); tr[0].cnt=0; A.pb(0),B.pb(0), dfs(0,k,0); cout<<ans; return 0; }
- 1
信息
- ID
- 26
- 时间
- 2500ms
- 内存
- 512MiB
- 难度
- 7
- 标签
- 递交数
- 216
- 已通过
- 3
- 上传者