瞄一眼题解,看到了 bitset 瞬间就会了/tuu。
所以没想到 bitset 还是很丢脸,而且丢个分块的标签在这里干嘛啊想了半天。
这道题 bitset 能过还是没想到,不过也是合理时间复杂度。
我们考虑维护区间值域所有数出现次数的奇偶性,那么我们最后查询就只用看一个值是否出现奇数次即可知道它是否需要异或进答案里面了。
将区间加法看作 bitset 里面的数整体左移 位即可,超过的部分我们直接用 bitset 的长度作为限制自然溢出即可,就相当取模。
考虑一个区间加法该怎么维护,我们发现我们可以对于区间加的值取模后控制在 之内,那么现在一个区间的 bitset 左移后后面的 位需要和前面的 位进行一个整体交换,这个就 单次暴力做就好了。单次下传和单次修改都是 所以最后修改和下传的总时间复杂度就是 。
然后区间查询就直接找到 个线段树对应的区间节点后把对应的每一位直接按位异或起来即可,单次合并一个区间节点的时间复杂度也是 最后查询总的时间复杂度就是 。
最后总的时间复杂度是 大约 的样子可以通过此题。
想精细卡常的话可以阈值分治一下,当线段树表示的区间长度小于阈值 时直接暴力修暴力查,对于此题数据 取 跑的比较快。
这里给出的实现未卡常。
#include "bits/stdc++.h"
using namespace std;
const int Len = 1e5 + 5;
inline int read() {
char ch = getchar();
int x = 0, f = 1;
while (ch < '0' || ch > '9') {
if (ch == '-')
f = -1;
ch = getchar();
}
while ('0' <= ch && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
inline void write(int x) {
if (x < 0)
putchar('-'), x = -x;
if (x > 9)
write(x / 10);
putchar(x % 10 + '0');
}
bitset<2> bst1[Len << 2],ans1;
bitset<4> bst2[Len << 2],ans2;
bitset<8> bst3[Len << 2],ans3;
bitset<16> bst4[Len << 2],ans4;
bitset<32> bst5[Len << 2],ans5;
bitset<64> bst6[Len << 2],ans6;
bitset<128> bst7[Len << 2],ans7;
bitset<256> bst8[Len << 2],ans8;
bitset<512> bst9[Len << 2],ans9;
bitset<1024> bst10[Len << 2],ans10;
int n,m,mm,q,a[Len],tag[Len << 2],Print[Len],flag[Len];
#define ls(x) x << 1
#define rs(x) x << 1 | 1
void push_up(int x)
{
if(mm == 1) bst1[x] = bst1[ls(x)] ^ bst1[rs(x)];
if(mm == 2) bst2[x] = bst2[ls(x)] ^ bst2[rs(x)];
if(mm == 3) bst3[x] = bst3[ls(x)] ^ bst3[rs(x)];
if(mm == 4) bst4[x] = bst4[ls(x)] ^ bst4[rs(x)];
if(mm == 5) bst5[x] = bst5[ls(x)] ^ bst5[rs(x)];
if(mm == 6) bst6[x] = bst6[ls(x)] ^ bst6[rs(x)];
if(mm == 7) bst7[x] = bst7[ls(x)] ^ bst7[rs(x)];
if(mm == 8) bst8[x] = bst8[ls(x)] ^ bst8[rs(x)];
if(mm == 9) bst9[x] = bst9[ls(x)] ^ bst9[rs(x)];
if(mm == 10) bst10[x] = bst10[ls(x)] ^ bst10[rs(x)];
}
void push_down(int p)
{
if(tag[p])
{
tag[ls(p)] += tag[p] , tag[rs(p)] += tag[p];
if(tag[ls(p)] > m) tag[ls(p)] -= m;
if(tag[rs(p)] > m) tag[rs(p)] -= m;
if(mm == 1) bst1[ls(p)] = (bst1[ls(p)] >> (m - tag[p])) | (bst1[ls(p)] << tag[p]) , bst1[rs(p)] = (bst1[rs(p)] >> (m - tag[p])) | (bst1[rs(p)] << tag[p]);
if(mm == 2) bst2[ls(p)] = (bst2[ls(p)] >> (m - tag[p])) | (bst2[ls(p)] << tag[p]) , bst2[rs(p)] = (bst2[rs(p)] >> (m - tag[p])) | (bst2[rs(p)] << tag[p]);
if(mm == 3) bst3[ls(p)] = (bst3[ls(p)] >> (m - tag[p])) | (bst3[ls(p)] << tag[p]) , bst3[rs(p)] = (bst3[rs(p)] >> (m - tag[p])) | (bst3[rs(p)] << tag[p]);
if(mm == 4) bst4[ls(p)] = (bst4[ls(p)] >> (m - tag[p])) | (bst4[ls(p)] << tag[p]) , bst4[rs(p)] = (bst4[rs(p)] >> (m - tag[p])) | (bst4[rs(p)] << tag[p]);
if(mm == 5) bst5[ls(p)] = (bst5[ls(p)] >> (m - tag[p])) | (bst5[ls(p)] << tag[p]) , bst5[rs(p)] = (bst5[rs(p)] >> (m - tag[p])) | (bst5[rs(p)] << tag[p]);
if(mm == 6) bst6[ls(p)] = (bst6[ls(p)] >> (m - tag[p])) | (bst6[ls(p)] << tag[p]) , bst6[rs(p)] = (bst6[rs(p)] >> (m - tag[p])) | (bst6[rs(p)] << tag[p]);
if(mm == 7) bst7[ls(p)] = (bst7[ls(p)] >> (m - tag[p])) | (bst7[ls(p)] << tag[p]) , bst7[rs(p)] = (bst7[rs(p)] >> (m - tag[p])) | (bst7[rs(p)] << tag[p]);
if(mm == 8) bst8[ls(p)] = (bst8[ls(p)] >> (m - tag[p])) | (bst8[ls(p)] << tag[p]) , bst8[rs(p)] = (bst8[rs(p)] >> (m - tag[p])) | (bst8[rs(p)] << tag[p]);
if(mm == 9) bst9[ls(p)] = (bst9[ls(p)] >> (m - tag[p])) | (bst9[ls(p)] << tag[p]) , bst9[rs(p)] = (bst9[rs(p)] >> (m - tag[p])) | (bst9[rs(p)] << tag[p]);
if(mm == 10) bst10[ls(p)] = (bst10[ls(p)] >> (m - tag[p])) | (bst10[ls(p)] << tag[p]) , bst10[rs(p)] = (bst10[rs(p)] >> (m - tag[p])) | (bst10[rs(p)] << tag[p]);
tag[p] = 0;
}
}
void build(int p,int l,int r)
{
tag[p] = 0;
//printf("%d %d %d\n",p,l,r);
if(l == r)
{
if(mm == 1) bst1[p].set(a[l] , 1);
if(mm == 2) bst2[p].set(a[l] , 1);
if(mm == 3) bst3[p].set(a[l] , 1);
if(mm == 4) bst4[p].set(a[l] , 1);
if(mm == 5) bst5[p].set(a[l] , 1);
if(mm == 6) bst6[p].set(a[l] , 1);
if(mm == 7) bst7[p].set(a[l] , 1);
if(mm == 8) bst8[p].set(a[l] , 1);
if(mm == 9) bst9[p].set(a[l] , 1);
if(mm == 10) bst10[p].set(a[l] , 1);
return;
}
int mid = (l + r) >> 1;
build(ls(p) , l , mid) , build(rs(p) , mid + 1 , r);
push_up(p);
}
void update(int p,int l,int r,int nl,int nr,int w)
{
if(nl <= l && nr >= r)
{
if(mm == 1) bst1[p] = (bst1[p] >> (m - w)) | (bst1[p] << w);
if(mm == 2) bst2[p] = (bst2[p] >> (m - w)) | (bst2[p] << w);
if(mm == 3) bst3[p] = (bst3[p] >> (m - w)) | (bst3[p] << w);
if(mm == 4) bst4[p] = (bst4[p] >> (m - w)) | (bst4[p] << w);
if(mm == 5) bst5[p] = (bst5[p] >> (m - w)) | (bst5[p] << w);
if(mm == 6) bst6[p] = (bst6[p] >> (m - w)) | (bst6[p] << w);
if(mm == 7) bst7[p] = (bst7[p] >> (m - w)) | (bst7[p] << w);
if(mm == 8) bst8[p] = (bst8[p] >> (m - w)) | (bst8[p] << w);
if(mm == 9) bst9[p] = (bst9[p] >> (m - w)) | (bst9[p] << w);
if(mm == 10) bst10[p] = (bst10[p] >> (m - w)) | (bst10[p] << w);
tag[p] += w;
if(tag[p] > m) tag[p] -= m;
return;
}
push_down(p);
int mid = (l + r) >> 1;
if(nl <= mid) update(ls(p) , l , mid , nl , nr , w);
if(nr > mid) update(rs(p) , mid + 1 , r , nl , nr , w);
push_up(p);
}
void query(int p,int l,int r,int nl,int nr)
{
if(nl <= l && nr >= r)
{
if(mm == 1) ans1 ^= bst1[p];
if(mm == 2) ans2 ^= bst2[p];
if(mm == 3) ans3 ^= bst3[p];
if(mm == 4) ans4 ^= bst4[p];
if(mm == 5) ans5 ^= bst5[p];
if(mm == 6) ans6 ^= bst6[p];
if(mm == 7) ans7 ^= bst7[p];
if(mm == 8) ans8 ^= bst8[p];
if(mm == 9) ans9 ^= bst9[p];
if(mm == 10) ans10 ^= bst10[p];
return;
}
push_down(p);
int mid = (l + r) >> 1;
if(nl <= mid) query(ls(p) , l , mid , nl , nr);
if(nr > mid) query(rs(p) , mid + 1 , r , nl , nr);
}
int main()
{
n = read() , m = read() , q = read();mm = m;m = 1 << m;
for(int i = 1 ; i <= n ; i ++) a[i] = read();
build(1 , 1 , n);
for(int i = 1 ; i <= q ; i ++)
{
int opt,l,r,x;opt = read();
if(opt == 1)
{
l = read() , r = read() , x = read();
if(l > r) swap(l , r);
update(1 , 1 , n , l , r , x);
//for(int j = l ; j <= r ; j ++) a[j] += x;
}
else
{
flag[i] = 1;
l = read() , r = read();
if(l > r) swap(l , r);
if(mm == 1) ans1.reset();
if(mm == 2) ans2.reset();
if(mm == 3) ans3.reset();
if(mm == 4) ans4.reset();
if(mm == 5) ans5.reset();
if(mm == 6) ans6.reset();
if(mm == 7) ans7.reset();
if(mm == 8) ans8.reset();
if(mm == 9) ans9.reset();
if(mm == 10) ans10.reset();
query(1 , 1 , n , l , r);
int res = 0;
if(mm == 1) for(int j = 1 ; j < m ; j ++) if(ans1[j]) res ^= j;
if(mm == 2) for(int j = 1 ; j < m ; j ++) if(ans2[j]){res ^= j;}
if(mm == 3) for(int j = 1 ; j < m ; j ++) if(ans3[j]){res ^= j;}
if(mm == 4) for(int j = 1 ; j < m ; j ++) if(ans4[j]){res ^= j;}
if(mm == 5) for(int j = 1 ; j < m ; j ++) if(ans5[j]){res ^= j;}
if(mm == 6) for(int j = 1 ; j < m ; j ++) if(ans6[j]){res ^= j;}
if(mm == 7) for(int j = 1 ; j < m ; j ++) if(ans7[j]){res ^= j;}
if(mm == 8) for(int j = 1 ; j < m ; j ++) if(ans8[j]){res ^= j;}
if(mm == 9) for(int j = 1 ; j < m ; j ++) if(ans9[j]){res ^= j;}
if(mm == 10) for(int j = 1 ; j < m ; j ++) if(ans10[j]){res ^= j;}
Print[i] = res;
write(res) , putchar('\n');
}
}
return 0;
}