跳到正文

剪贴板

剪贴板 7bra03dz

原文
剪贴板信息

剪贴板信息

收录
更新
C++
#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][2][1024][1024];
struct Trie{
	struct node{
		int son[2],cnt;
	}t[(N+1)*4+1];
	int tot;
	int rt[N<<1|1];
	queue<int> Q;
	inline int new_ver()
	{
		int o;
		if (Q.size()) {
			o=Q.front();
			Q.pop();
		} else o=++tot;
		t[o].son[0]=t[o].son[1]=t[o].cnt=0;
		return o;
	}
	inline void clear()
	{
		tot=0;
		while (Q.size()) Q.pop();
		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);
		Q.push(q);
		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();
	}
	T.clear();
	doit(1,nh,0,n-1);
	int o{nh/2+1};
	for (int a:{0,1})
		for (int b:{0,1})
			for (int i{0};i<1<<o;++i) {
				tot[a][b][i][0]=0;
				for (int j{0};j<o;++j) {
					ll h[2]{};
					for (int k{0};k<o;++k) {
						h[0]+=ans[i>>k&1][k+a*o][j+b*o];
						h[1]+=ans[~i>>k&1][k+a*o][j+b*o];
					}
					for (int x{(1<<j+1)-1};x>=0;--x) {
						tot[a][b][i][x]=tot[a][b][i][x&((1<<j)-1)]+h[x>>j&1];
					}
				}
			}
	for(sh i=0;i<=nh;++i)
	{
		for(sh j=0;j<=nh;++j)
		{
			ans[0][i][j]=ans[1][i][j]=0;
		}
	}
	int tagx=0,tagy=0;
	while(q--)
	{
		cint op=read();
		if(op==1)tagy^=read();
		else tagx^=read();
		ll ans=0;
		for (int a:{0,1})
			for (int b:{0,1})
				ans+=tot[a][b][tagx>>(a*o)&((1<<o)-1)][tagy>>(b*o)&((1<<o)-1)];
		princh(ans,'\n');
	}
	// cerr<<T.tot<<endl;
}
int main()
{
	// freopen("xaiv.in","r",stdin);
	// freopen("xaiv.out","w",stdout);
	int T=read();
	while(T--)solve();
	return 0;
}