题解 CF1401F 【Reverse and Swap】

Solution- CF 1401 F \mathrm{CF1401F}C F 1 4 0 1 F

Topic meaning

Topic portal

S o l \mathrm{Sol} Sol

A not difficult ds question

We consider putting the sequence on the line segment tree, 2 n 2^n2The number of n is equivalent to the number of leaf nodes of the line segment tree.

For 1 ∼ 4 1\sim 41The operation of 4 is also2, 3 2, 32,3 is more difficult to maintain, we consider2 22 operation, assuming theiii node isdd on theline segment treed layer (1 11 node isnnn layer). Then the inversion operation is equivalent to inverting the left and right sons of all the layers below respectively, so we set0 ∼ d 0\sim d0Just mark layer d . For3 33 operation is equivalent tod + 1 d+1d+The 1- layer branding machine can realize the exchange of adjacent blocks.

For the marked layer, we can do the reverse recursion, and the rest is the sum of the interval of the line segment tree. You can see the code implementation for details.

Time complexity O (2 18 × 18) O(2^{18}\times 18)O ( 218×18)

C o d e \mathrm{Code} Code

const int mo=998244353;
const int N=1<<18;

int n,m,S,a[N+5],t[N*4+5],flg[21];

inline void Fix(int x,int l,int r,int p,int val,int d)
{
    
    
	if(p>r||p<l) return;
	if(l==r)
	{
    
    
		t[x]=val;
		return;
	}
	int mid=l+r>>1;
	if(flg[d]) Fix(x<<1|1,l,mid,p,val,d-1),Fix(x<<1,mid+1,r,p,val,d-1);
	else Fix(x<<1,l,mid,p,val,d-1),Fix(x<<1|1,mid+1,r,p,val,d-1);
	t[x]=t[x<<1]+t[x<<1|1];
}

inline int Ask(int x,int l,int r,int ll,int rr,int d)
{
    
    
	if(ll>r||rr<l) return 0;
	if(ll<=l&&r<=rr) return t[x];
	int mid=l+r>>1;
	int ans=0;
	if(flg[d]) ans=Ask(x<<1,mid+1,r,ll,rr,d-1)+Ask(x<<1|1,l,mid,ll,rr,d-1);//如果打上标记,那么对于左儿子要在[mid+1,r]里递归下去
	else ans=Ask(x<<1,l,mid,ll,rr,d-1)+Ask(x<<1|1,mid+1,r,ll,rr,d-1); 
	return ans;
}

signed main()
{
    
    
	io.read(n),io.read(m);
	S=(1ll<<n);
	For(i,1,S) 
	{
    
    
		io.read(a[i]);
		Fix(1,1,S,i,a[i],n);
	}
	for (;m--;)
	{
    
    
		int type,x,y;
		io.read(type);
		io.read(x);
		if((type^2)&&(type^3)) io.read(y);
		if(type==1) Fix(1,1,S,x,y,n);
		if(type==3) flg[x+1]^=1;
		if(type==2) For(i,0,x) flg[i]^=1;
		if(type==4) io.write(Ask(1,1,S,x,y,n)),puts("");
	}
	return 0;
}
			
		
	
	

Guess you like

Origin blog.csdn.net/wangyiyang2/article/details/108175961