E - Xenia and Bit Operations CodeForces - 339D 线段树

本文介绍了一种使用位运算和树状数组解决特定序列更新和查询问题的方法。通过构建一棵特殊的树,该树的节点执行交替的位或运算和位异或运算,可以高效地处理序列中元素的更新,并快速计算出更新后的序列值v。文章详细解释了算法的实现过程,包括树的构建、更新操作和查询处理,同时提供了完整的AC代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 aor a2, aor a4, ..., a2n - 1 or a2n, consisting of 2n - 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 ap = b. After each query, you need to print the new value v for the new sequence a.

Input

The first line contains two integers n and m (1 ≤ n ≤ 17, 1 ≤ m ≤ 105). The next line contains 2n integers a1, a2, ..., a2n (0 ≤ ai < 230). Each of the next m lines contains queries. The i-th line contains integers pi, bi (1 ≤ pi ≤ 2n, 0 ≤ bi < 230) — the i-th query.

Output

Print m integers — the i-th integer denotes value v for sequence a after the i-th query.

Examples

Input

2 4
1 6 3 5
1 4
3 4
1 2
1 2

Output

1
3
3
3

Note

For more information on the bit operations, you can follow this link: http://en.wikipedia.org/wiki/Bitwise_operation

给出2的n次方个数,叶子节点两两进行位运算,倒数最后一行进行或运算,倒数第二行进行异或运算,倒数第三行进行或运算,以此类推,最后根节点的数据就是答案。我们用0表示进行或运算,用1表示进行异或运算。一旦到达叶子节点,就返回0,表示进行或操作,否则进行递归调用,将从子树传回来的位操作信息应用到两棵子树的数据,得到该节点的数据,然后将位操作信息异或1返回给当前节点的父节点,因为位操作是交替进行的。注意:用0表示或操作,1表示异或操作,所以从0开始每次异或1就能够进行交替的操作了,一个小技巧而已。

参考博客:https://blog.youkuaiyun.com/silenceneo/article/details/52068244

AC代码:

#include <iostream>
#include <cstdio>
#include <string>
#include <string>
#include <algorithm>
#include <cmath>
using namespace std;
const int maxn=1e5;
int s[maxn<<2];
void pushup(int rt,int ok)
{
    if(!ok)
    {
        s[rt]=s[rt<<1]|s[rt<<1|1];//或运算
    }
    else
    {
        s[rt]=s[rt<<1]^s[rt<<1|1];//异或运算
    }
}
int build(int l,int r,int rt)
{
    if(l==r)
    {
        int n;
        cin>>n;
        s[rt]=n;
        return 0;//叶子节点返回0
    }
    int m=(l+r)>>1;
    int t=build(l,m,rt<<1);
    build(m+1,r,rt<<1|1);
    pushup(rt,t);
    return t^1;
}
int update(int L,int C,int l,int r,int rt)
{
    if(l==r)
    {
        s[rt]=C;
        return 0;
    }
    int m=(l+r)>>1;
    int t;
    if(L<=m)
        t=update(L,C,l,m,rt<<1);
    else
        t=update(L,C,m+1,r,rt<<1|1);
    pushup(rt,t);
    return t^1;
}
int main()
{
    int n,q;
    cin>>n>>q;
    build(1,1<<n,1);
    int p,v;
    while(q--)
    {
        cin>>p>>v;
        update(p,v,1,1<<n,1);
        cout<<s[1]<<endl;
    }
    return 0;
}

 

基于C2000 DSP的电力电子、电机驱动和数字滤波器的仿真模型构建及其C代码实现方法。首先,在MATLAB/Simulink环境中创建电力电子系统的仿真模型,如三相逆变器,重点讨论了PWM生成模块中死区时间的设置及其对输出波形的影响。接着,深入探讨了C2000 DSP内部各关键模块(如ADC、DAC、PWM定时器)的具体配置步骤,特别是EPWM模块采用上下计数模式以确保对称波形的生成。此外,还讲解了数字滤波器的设计流程,从MATLAB中的参数设定到最终转换为适用于嵌入式系统的高效C代码。文中强调了硬件在环(HIL)和支持快速原型设计(RCP)的重要性,并分享了一些实际项目中常见的陷阱及解决方案,如PCB布局不当导致的ADC采样异常等问题。最后,针对中断服务程序(ISR)提出了优化建议,避免因ISR执行时间过长而引起的系统不稳定现象。 适合人群:从事电力电子、电机控制系统开发的技术人员,尤其是那些希望深入了解C2000 DSP应用细节的研发工程师。 使用场景及目标:①掌握利用MATLAB/Simulink进行电力电子设备仿真的技巧;②学会正确配置C2000 DSP的各项外设资源;③能够独立完成从理论设计到实际产品落地全过程中的各个环节,包括但不限于数字滤波器设计、PWM信号生成、ADC采样同步等。 其他说明:文中提供了大量实用的代码片段和技术提示,帮助读者更好地理解和实践相关知识点。同时,也提到了一些常见错误案例,有助于开发者规避潜在风险。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值