P11160 【MX-X6-T6】機械生命体
Genius_Star · · 题解
或许更好的阅读体验。
思路:
先考虑询问,考虑建一个从低到高为的 01trie,然后在上面遍历一遍
对于路径中的一个点
那么询问是好做的,扫一遍即可,复杂度为
考虑操作本质是干什么,要使得 01trie 上的节点
直接想子树加还是过于牛了,先考虑全局
-
若
v 是奇数,那么先对k 子树做一次全局+1 ,此时v 就是偶数了。 -
若
v 是偶数,那么对当前位已经没有贡献了,将\frac{v}{2} 下传到ch_{k, 0/1} 。
此时若全部下传时间复杂度肯定是 swap(ch[k][0], ch[k][1]) 然后加到 ch[k][0] 上。
由于加法具有交换,结合律,是可合并的,故做法是对的,单次查询
现在考虑子树加怎么做,显然直接类比上面思路将标记打到子树处是错误的,因为
考虑对
那么子树加
时间复杂度为
link
完整代码:
#include<bits/stdc++.h>
#define lowbit(x) x & (-x)
#define ls(k) k << 1
#define rs(k) k << 1 | 1
#define fi first
#define se second
#define add(x, y) ((x + y >= mod) ? (x + y - mod) : (x + y))
#define dec(x, y) ((x - y < 0) ? (x - y + mod) : (x - y))
#define ctz(x) __builtin_ctz(x)
#define popcnt(x) __builtin_popcount(x)
#define open(s1, s2) freopen(s1, "r", stdin), freopen(s2, "w", stdout);
using namespace std;
typedef __int128 __;
typedef long double lb;
typedef double db;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef long long ll;
bool Begin;
const int N = 5e5 + 10;
inline ll read(){
ll x = 0, f = 1;
char c = getchar();
while(c < '0' || c > '9'){
if(c == '-')
f = -1;
c = getchar();
}
while(c >= '0' && c <= '9'){
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return x * f;
}
inline void write(ll x){
if(x < 0){
putchar('-');
x = -x;
}
if(x > 9)
write(x / 10);
putchar(x % 10 + '0');
}
namespace XorTree{
const int lim = 32, M = lim * N + 10;
int cnt;
int ch[M][2];
int siz[M];
uint tag[M], sum[M];
inline int newnode(){
++cnt;
siz[cnt] = sum[cnt] = tag[cnt] = 0;
ch[cnt][0] = ch[cnt][1] = 0;
return cnt;
}
inline void pushup(int k){
siz[k] = siz[ch[k][0]] + siz[ch[k][1]];
sum[k] = (siz[ch[k][1]] & 1) | ((sum[ch[k][0]] ^ sum[ch[k][1]]) << 1);
}
inline void push_down(int k){
if(tag[k]){
if(tag[k] & 1){
swap(ch[k][0], ch[k][1]);
if(ch[k][0])
++tag[ch[k][0]];
--tag[k];
}
if(ch[k][0])
tag[ch[k][0]] += (tag[k] >> 1);
if(ch[k][1])
tag[ch[k][1]] += (tag[k] >> 1);
tag[k] = 0;
}
}
inline void insert(int k, uint v, int dep){
if(dep >= lim){
++siz[k];
return ;
}
push_down(k);
if(!ch[k][v & 1])
ch[k][v & 1] = newnode();
insert(ch[k][v & 1], v >> 1, dep + 1);
pushup(k);
}
inline void del(int k, uint v, int dep){
if(dep >= lim){
--siz[k];
return ;
}
push_down(k);
del(ch[k][v & 1], v >> 1, dep + 1);
pushup(k);
}
inline int merge(int x, int y, int dep){
if(!x || !y)
return x + y;
if(dep >= lim){
siz[y] += siz[x];
return y;
}
push_down(x), push_down(y);
ch[y][0] = merge(ch[x][0], ch[y][0], dep + 1);
ch[y][1] = merge(ch[x][1], ch[y][1], dep + 1);
ch[x][0] = ch[x][1] = 0;
pushup(x), pushup(y);
return y;
}
inline void Add(int k, uint v){
tag[k] += v;
}
inline void Add(int k1, int k2, uint x, uint y, int k, int dep){
if(!k1)
return ;
if(dep == k){
Add(k1, y);
if(k1 != k2)
merge(k1, k2, k);
return ;
}
push_down(k1), push_down(k2);
if(!ch[k2][y & 1])
ch[k2][y & 1] = newnode();
Add(ch[k1][x & 1], ch[k2][y & 1], x >> 1, y >> 1, k, dep + 1);
pushup(k1), pushup(k2);
}
inline void Add(int rt, uint x, int k, uint v){
int y = x + v;
Add(rt, rt, x, y, k, 0);
}
inline int askmax(int k, uint x, int dep){
if(dep >= lim)
return lim;
push_down(k);
if(siz[ch[k][x & 1]])
return askmax(ch[k][x & 1], x >> 1, dep + 1);
return dep;
}
inline int askxor(int k){
return sum[k];
}
};
int rt, n, op;
uint x, k, v, ans;
bool End;
int main(){
rt = XorTree::newnode();
n = read();
while(n--){
op = read(), x = read();
if(op == 1)
XorTree::insert(rt, x, 0);
else if(op == 2)
XorTree::del(rt, x, 0);
else if(op == 3){
k = read(), v = read();
x &= (1ll << k) - 1;
XorTree::Add(rt, x, k, v);
}
else{
ans = XorTree::askmax(rt, x, 0);
write(1ll << ans);
putchar('\n');
}
}
cerr << '\n' << abs(&Begin - &End) / 1048576 << "MB";
return 0;
}