【CF339D Xenia and Bit Operations】题解

题目链接

题目

Xenia the beginner programmer has a sequence a , consisting of 2n non-negative integers: a1,a2,...,a2n . 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 a1ora2,a3ora4,...,a2n1ora2n , consisting of 2n1 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) (1or2=3,3or4=7) (3xor7=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 ap=b . After each query, you need to print the new value v for the new sequence a .

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

 or   xor 交替计算。

第一行两个数字n,m

第二行2n个数字。

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

保证1n17 , 1m105,数字任意时刻满足0x230

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

思路

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

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

总复杂度:O(mn),预处理可能还要带个 O(2n×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;
}

总结

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

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

posted @   zhangtingxi  阅读(56)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示