huangyuze1's blog

By huangyuze1, history, 44 minutes ago, In English
#include <bits/stdc++.h>
using namespace std;
typedef long long ll; 
#define F(i,a,b) for(int i=a; i<=b; ++i)
#define F_(i,a,b) for(int i=a; i>=b; i--)
#define pii pair<int,int>
#define fr first
#define sc second
#define pb push_back
const int N = 2e5+10, INF = 1<<30;
int n,q,ar[N],ans[N],tmp[N],nar[N],cg[N];
int tot,rt[N],ch[N<<7][2],cnt[N<<7];
void ins(int p,int pre,int x,int op){
	int u = ++tot;
	pre = rt[pre], rt[p] = u;
	F_(i,30,0){
		int o = (x>>i)&1;
		ch[u][o] = ++tot, ch[u][!o] = ch[pre][!o];
		pre = ch[pre][o], u = ch[u][o];
		cnt[u] = cnt[pre] + op;
	}
}
int qry(int p,int x){
	int u = rt[p], res = 0;
	F_(i,30,0){
		int o = (x>>i)&1;
		if (cnt[ch[u][o]]) u = ch[u][o];
		else u = ch[u][!o], res += (1<<i);
	}
	return res;
}
void work(){
	cin >> n >> q;
	F(i,1,n) cin >> ar[i];
	int k = 0;
	while (ar[n]){
		int mi = INF, ma = 0;
		F(i,1,n) mi = min(mi,ar[i]), ma = max(ma,ar[i]);
		ans[k++] = ma-mi;

		F(i,0,n) rt[i] = 0;
		F(i,0,tot) ch[i][0]=ch[i][1]=cnt[i]=0;
		tot = 0;
		F(i,1,n) ins(0,0,ar[i],1);
		F(i,1,n) ins(i,0,ar[i],-1);

		int cur = 0;
		priority_queue<pii,vector<pii>,greater<pii> > pq;
		F(i,1,n) pq.push({qry(i,ar[i]),i});
		F(i,1,2*n){
			int pos = pq.top().sc, val = pq.top().fr;
			if (i&1) nar[i/2+1] = val;
			ins(pos,pos,val^ar[pos],-1);
			pq.pop(); pq.push({qry(pos,ar[pos]),pos});
		}
		F(i,1,n) ar[i] = nar[i];
	}
	while (q--){
		int x; cin >> x;
		if (x >= k) cout << "0\n";
		else cout << ans[x] << "\n";
	}
}
int main(){
	int _; cin >> _;
	while (_--) work();
	return 0;
}
  • Vote: I like it
  • -2
  • Vote: I do not like it