339D. Xenia and Bit Operations - Codeforces Solution C++

  Problem Link : 339D. Xenia and Bit Operations 


✅ C++ Solution :

 
#include<bits/stdc++.h>
using namespace std;

#define ll long long int
#define mp make_pair

ll st[400002];
ll arr[200001];

void build(ll v, ll l, ll r)
{
	if(l==r)
	{
		st[v]=arr[l];
		return;
	}
	int mid=(l+r)/2;
	build(2*v,l,mid);
	build(2*v+1,mid+1,r);

	int len=r-l+1;
	len=log2(len);
	if(len%2==0)
		st[v]=st[2*v]^st[2*v+1];
	else
		st[v]=st[2*v] | st[2*v+1];
}

void update(ll v, ll s, ll e, ll ind, ll val)
{
	if(s==e)
	{
		st[v]=val;
		return;
	}
	ll mid=(s+e)/2;
	if(ind>=s && ind<=mid)
		update(2*v,s,mid,ind,val);
	else
		update(2*v+1,mid+1,e,ind,val);

	int len=e-s+1;
	len=log2(len);
	if(len%2==0)
		st[v]=st[2*v]^st[2*v+1];
	else
		st[v]=st[2*v] | st[2*v+1];

}

int main()
{
	int n,m;
	ll p,b;
	cin>>n>>m;
	n=pow(2,n);
	for(int i=1;i<=n;i++)
		cin>>arr[i];

	build(1,1,n);

	while(m--)
	{
		cin>>p>>b;
		update(1,1,n,p,b);
		cout<<st[1]<<"\n";
	}
}

 

Thank you for your patience reading. If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Whatsapp or Facebook. 

😇Happy Learning!!