Trie 3
Trie 树小综合
CF241B Friends
前置芝士:Trie 2、Trie 1
这道题是异或粽子的加强版,由于
首先,如果不想可持久化,并解决
我们通过 Trie 2 这里的做法可以得出第
具体的,我们对于每一个
怎么进行累加呢?直接枚举每个数时间复杂度显然不可行,我们可以预处理出来一个
这一部分的代码:
void getans(int p, int pos, int id){
if (pos < 0){
return;
}
int c = (val>>pos&1);
int to1 = trie[p][a[id]>>pos&1^1];
int to0 = trie[p][a[id]>>pos&1];
if (c) getans(to1,pos-1,id);//如果当前位为 1 了,直接走
else{
for (int i = 31; i >= 0; i--){
if (a[id]>>i&1) (ans += 1ll*(sz[to1]-cnt[to1][i])*(1ll<<i))%=mod; //这位本来就是 1,要异或0才能累加贡献
else (ans += 1ll*cnt[to1][i]*(1ll<<i))%=mod;//直接累加贡献
}
ctmore += sz[to1];//比它大的数多了这么多个
getans(to0,pos-1,id);
}
}
其他的代码和 Trie 2 这里的几乎一样(不用滚存),只不过第
要注意的一点是,由于中间取过模,最后除以二要用逆元。