C/C++教程

【CF339D Xenia and Bit Operations】题解

本文主要是介绍【CF339D Xenia and Bit Operations】题解,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

题目链接

题目

Xenia the beginner programmer has a sequence $ a $ , consisting of $ 2^{n} $ non-negative integers: $ a_{1},a_{2},...,a_{2^{n}} $ . Xenia is currently studying bit operations. To better understand how they work, Xenia decided to calculate some value $ v $ for $ a $ .

Namely, it takes several iterations to calculate value $ v $ . At the first iteration, Xenia writes a new sequence $ a_{1} or a_{2},a_{3} or a_{4},...,a_{2^{n}-1} or a_{2^{n}} $ , consisting of $ 2^{n-1} $ elements. In other words, she writes down the bit-wise OR of adjacent elements of sequence $ a $ . At the second iteration, Xenia writes the bitwise exclusive OR of adjacent elements of the sequence obtained after the first iteration. At the third iteration Xenia writes the bitwise OR of the adjacent elements of the sequence obtained after the second iteration. And so on; the operations of bitwise exclusive OR and bitwise OR alternate. In the end, she obtains a sequence consisting of one element, and that element is $ v $ .

Let's consider an example. Suppose that sequence $ a=(1,2,3,4) $ . Then let's write down all the transformations $ (1,2,3,4) $ $ → $ $ (1 or 2=3,3 or 4=7) $ $ → $ $ (3 xor 7=4) $ . The result is $ v=4 $ .

You are given Xenia's initial sequence. But to calculate value $ v $ for a given sequence would be too easy, so you are given additional $ m $ queries. Each query is a pair of integers $ p,b $ . Query $ p,b $ means that you need to perform the assignment $ a_{p}=b $ . After each query, you need to print the new value $ v $ for the new sequence $ a $ .

有\(2^n\)个数,有\(m\)个操作,每次修改一个数,然后你要输出$\ \ \ \ \ $( (a1|a2)xor(a3|a4) )|( (a5|a6)xor(a7|a8) )....

即$\ or\ \ \ xor\ $交替计算。

第一行两个数字\(n,m\)。

第二行\(2^n\)个数字。

下面\(m\)行,每行两个数字\(x,y\),将第\(x\)个数字改为\(y\)。

保证\(1\le n \le 17\ , \ 1\le m \le 10^5\),数字任意时刻满足\(0\le x \le 2^{30}\)

共\(m\)行,输出每次改完数字后上述表达式的值。

思路

把这个数列当成一个高为 \(n\) 的线段树,从下往上奇数层异或,偶数层或,就可以计算初始答案。

而每次修改最多修改 \(n\) 个节点,就是线段树上所有包含这个数的区间。

总复杂度:\(O(mn)\),预处理可能还要带个 \(O(2^n\times n)\)

Code

// Problem: CF339D Xenia and Bit Operations
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/CF339D
// Memory Limit: 250 MB
// Time Limit: 2000 ms

#include<bits/stdc++.h>
using namespace std;
#define int long long
inline int read(){int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;
ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+
(x<<3)+(ch^48);ch=getchar();}return x*f;}
#define N 2000000
//#define M
//#define mo
int n, m, i, j, k, T; 
int v[N], a[N], b[N]; 
int x, y; 

void build(int k, int l, int r)
{
	if(l==r) v[k]=a[l], b[k]=0; 
	else
	{
		int mid=(l+r)>>1; 
		build(k<<1, l, mid); 
		build(k<<1|1, mid+1, r); 
		b[k]=(b[k<<1]^1); 
		if(b[k]) v[k]=(v[k<<1]|v[k<<1|1]); 
		else v[k]=(v[k<<1]^v[k<<1|1]); 
	}
}

void print(int k, int l, int r)
{
	if(l==r) printf("%lld ", v[k]); 
	else
	{
		int mid=(l+r)>>1; 
		print(k<<1, l, mid); 
		print(k<<1|1, mid+1, r); 
	}
}

void gai(int k, int l, int r, int x, int y)
{
	if(l==r) v[k]=y; 
	else
	{
		int mid=(l+r)>>1; 
		if(mid>=x) gai(k<<1, l, mid, x, y); 
		else gai(k<<1|1, mid+1, r, x, y); 
		
		if(b[k]) v[k]=(v[k<<1]|v[k<<1|1]); 
		else v[k]=(v[k<<1]^v[k<<1|1]); 
		// printf("(%lld, %lld, %lld)%lld %lld %lld\n", k, l, r, v[k], v[k<<1], v[k<<1|1]); 
	}
}

signed main()
{
//	freopen("tiaoshi.in","r",stdin);
//	freopen("tiaoshi.out","w",stdout);
	n=read(); m=read(); 
	for(i=1; i<=(1<<n); ++i) a[i]=read(); 
	build(1, 1, (1<<n)); 
	// printf("%lld\n", v[1]); 
	// print(1, 1, (1<<n)); 
	while(m--)
	{
		x=read(); y=read(); 
		gai(1, 1, (1<<n), x, y); 
		// print(1, 1, (1<<n)); 
		printf("%lld\n", v[1]); 
	}
	return 0;
}

总结

对于很多序列类题目,如果其长度为 \(2^n\),一般可以多考虑线段树。

类似题可以多往线段树的方向去想。

这篇关于【CF339D Xenia and Bit Operations】题解的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!