#include<bits/stdc++.h>
#define cint const int
#define uint unsigned int
#define cuint const unsigned int
#define ll long long
#define cll const long long
#define ull unsigned long long
#define cull const unsigned long long
#define sh short
#define csh const short
#define ush unsigned short
#define cush const unsigned short
using namespace std;
inline int read()
{
int x=0;
char ch=getchar();
while(ch<'0'||ch>'9')
{
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
x=(x<<1)+(x<<3)+(ch-'0');
ch=getchar();
}
return x;
}
inline void print(cll &x)
{
if(x<10)
{
putchar(x+'0');
return;
}
print(x/10);
putchar(x%10+'0');
}
inline void princh(cll &x,const char &ch)
{
print(x);
putchar(ch);
}
csh H=19;
cint N=1<<(H+1);
sh nh;
int n,q;
int a[N];
struct dat{
int x;
bool c;
};
ll ans[2][H+1][H+1],tot[2][H+1][N];
struct Trie{
struct node{
int son[2],cnt;
}t[(N+1)*(H+2)+1];
int tot;
int rt[N<<1|1];
inline int new_ver()
{
++tot;
t[tot].son[0]=t[tot].son[1]=t[tot].cnt=0;
return tot;
}
inline void clear()
{
tot=0;
new_ver();
}
inline void insert(int p,cint x)
{
rt[p]=new_ver();
p=rt[p];
for(sh i=nh;i>=0;--i)
{
const bool c=(x>>i&1);
t[p].son[c]=new_ver();
p=t[p].son[c];
++t[p].cnt;
}
}
inline int merge(cint p,cint q,csh hx,csh hy)
{
if(!p||!q)return (p|q);
t[p].cnt+=t[q].cnt;
ans[0][hx][hy]+=1ll*t[t[p].son[1]].cnt*t[t[q].son[0]].cnt;
ans[1][hx][hy]+=1ll*t[t[p].son[0]].cnt*t[t[q].son[1]].cnt;
t[p].son[0]=merge(t[p].son[0],t[q].son[0],hx,hy-1);
t[p].son[1]=merge(t[p].son[1],t[q].son[1],hx,hy-1);
return p;
}
inline void merge_into(cint p,cint q,cint r,csh h)
{
rt[r]=merge(rt[p],rt[q],h,nh);
}
}T;
void doit(cint p,csh h,cint l,cint r)
{
if(l==r)
{
T.insert(p,a[l]);
return;
}
cint mid=l+r>>1;
doit(p<<1,h-1,l,mid);
doit(p<<1|1,h-1,mid+1,r);
T.merge_into(p<<1,p<<1|1,p,h);
}
void solve()
{
nh=read()-1;
n=(1<<nh+1);
q=read();
for(int i=0;i<n;++i)
{
a[i]=read();
}
for(sh i=0;i<=nh;++i)
{
for(sh j=0;j<=nh;++j)
{
ans[0][i][j]=ans[1][i][j]=0;
}
}
T.clear();
doit(1,nh,0,n-1);
for(sh i=0;i<=nh;++i)
{
tot[0][i][0]=tot[1][i][0]=0;
for(sh j=0;j<=nh;++j)
{
for(int x=(1<<j+1)-1;x>=0;--x)
{
tot[0][i][x]=(tot[0][i][x&((1<<j)-1)]+ans[(x>>j&1)][i][j]);
tot[1][i][x]=(tot[1][i][x&((1<<j)-1)]+ans[!(x>>j&1)][i][j]);
}
}
}
int tagx=0,tagy=0;
while(q--)
{
cint op=read();
if(op==1)tagy^=read();
else tagx^=read();
ll ans=0;
for(sh i=0;i<=nh;++i)
{
ans+=tot[(tagx>>i&1)][i][tagy];
}
princh(ans,'\n');
}
}
int main()
{
// freopen("xaiv.in","r",stdin);
// freopen("xaiv.out","w",stdout);
int T=read();
while(T--)solve();
return 0;
}剪贴板
剪贴板 ni61yalu
剪贴板信息
剪贴板信息
- 收录
- 更新