【CF339D Xenia and Bit Operations】题解
题目链接
题目
Xenia the beginner programmer has a sequence , consisting of non-negative integers: . Xenia is currently studying bit operations. To better understand how they work, Xenia decided to calculate some value for .
Namely, it takes several iterations to calculate value . At the first iteration, Xenia writes a new sequence , consisting of elements. In other words, she writes down the bit-wise OR of adjacent elements of sequence . 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 .
Let's consider an example. Suppose that sequence . Then let's write down all the transformations . The result is .
You are given Xenia's initial sequence. But to calculate value for a given sequence would be too easy, so you are given additional queries. Each query is a pair of integers . Query means that you need to perform the assignment . After each query, you need to print the new value for the new sequence .
有个数,有个操作,每次修改一个数,然后你要输出( (a1|a2)xor(a3|a4) )|( (a5|a6)xor(a7|a8) )....
即交替计算。
第一行两个数字。
第二行个数字。
下面行,每行两个数字,将第个数字改为。
保证,数字任意时刻满足
共行,输出每次改完数字后上述表达式的值。
思路
把这个数列当成一个高为 的线段树,从下往上奇数层异或,偶数层或,就可以计算初始答案。
而每次修改最多修改 个节点,就是线段树上所有包含这个数的区间。
总复杂度:,预处理可能还要带个
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;
}
总结
对于很多序列类题目,如果其长度为 ,一般可以多考虑线段树。
类似题可以多往线段树的方向去想。
本文来自博客园,作者:zhangtingxi,转载请注明原文链接:https://www.cnblogs.com/zhangtingxi/p/16207447.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!