跳到正文

剪贴板

剪贴板 2pejyvwl

原文
剪贴板信息

剪贴板信息

收录
更新
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;
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;
}
void print(cll x)
{
	if(x<10)
	{
		putchar(x+'0');
		return;
	}
	print(x/10);
	putchar(x%10+'0');
}
void princh(cll x,const char ch)
{
	print(x);
	putchar(ch);
}
int nh,n,q,tagx,tagy;
cint H=17,N=1<<(H+1);
int a[N];
ll cnt[2][H+1][H+1];
struct TRIE{
	struct Trie{
		struct node{
			int h,son[2],cnt[2];
		}t[N*(H+1)+1];
		int tot;
		inline int new_ver(cint h)
		{
			++tot;
			t[tot].h=h;
			t[tot].son[0]=t[tot].son[1]=t[tot].cnt[0]=t[tot].cnt[1]=0;
			return tot;
		}
		inline void clear()
		{
			tot=0;
			new_ver(nh);
		}
		void insert(cint x,const bool y)
		{
			int p=1;
			for(int i=nh;i>=0;--i)
			{
				const bool c=(x>>i&1);
				if(!t[p].son[c])
				{
					t[p].son[c]=new_ver(i-1);
				}
				p=t[p].son[c];
				++t[p].cnt[y];
			}
		}
		void solve(cint id,cint p)
		{
			if(!p||t[p].h==-1)return;
//			printf("%d(%d):\n",p,t[p].h);
//			printf("ls:%d %d\n",t[t[p].son[0]].cnt[0],t[t[p].son[0]].cnt[1]);
//			printf("rs:%d %d\n",t[t[p].son[1]].cnt[0],t[t[p].son[1]].cnt[1]);
			cnt[0][id][t[p].h]+=1ll*t[t[p].son[0]].cnt[1]*t[t[p].son[1]].cnt[0];
			cnt[1][id][t[p].h]+=1ll*t[t[p].son[0]].cnt[0]*t[t[p].son[1]].cnt[1];
			solve(id,t[p].son[0]);
			solve(id,t[p].son[1]);
		}
	}T;
	struct node{
		int h,son[2];
		vector<int>x;
	}t[N*(H+1)+1];
	int tot;
	inline int new_ver(cint h)
	{
		++tot;
		t[tot].h=h;
		t[tot].son[0]=t[tot].son[1]=0;
		t[tot].x.clear();
		return tot;
	}
	void clear()
	{
		tot=0;
		new_ver(nh);
	}
	void insert(cint x,cint y)
	{
		int p=1;
		for(int i=nh;i>=0;--i)
		{
			const bool c=(x>>i&1);
			if(!t[p].son[c])t[p].son[c]=new_ver(i-1);
			p=t[p].son[c];
//			printf("T[%d]:push_back(%d)\n",p,y);
			t[p].x.push_back(y);
		}
	}
	void solve(cint p)
	{
		if(!p||t[p].h==-1)return;
		T.clear();
		for(int x:t[t[p].son[0]].x)T.insert(x,0);
		for(int x:t[t[p].son[1]].x)T.insert(x,1);
		T.solve(t[p].h,1);
		solve(t[p].son[0]);
		solve(t[p].son[1]);
	}
}T;
void solve()
{
	nh=read()-1;
	n=(1<<nh+1);
	q=read();
	T.clear();
	for(int i=0;i<n;++i)
	{
		a[i]=read();
		T.insert(i,a[i]);
	}
	for(int i=0;i<=nh;++i)
	{
		for(int j=0;j<=nh;++j)
		{
			cnt[0][i][j]=cnt[1][i][j]=0;
		}
	}
	tagx=tagy=0;
	T.solve(1);
	while(q--)
	{
		cint op=read();
		if(op==1)tagy^=read();
		else tagx^=read();
		ll ans=0;
		for(int i=0;i<=nh;++i)
		{
			for(int j=0;j<=nh;++j)
			{
				ans+=cnt[(tagx>>i&1)^(tagy>>j&1)][i][j];
			}
		}
		princh(ans,'\n');
	}
}
int main()
{
	// freopen("xaiv.in","r",stdin);
	// freopen("xaiv.out","w",stdout);
	int T=read();
	while(T--)solve();
	return 0;
}