CF339D Xenia and Bit Operations(线段树)

Posted oneman233

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CF339D Xenia and Bit Operations(线段树)相关的知识,希望对你有一定的参考价值。

乱搞线段树,单点修改,每次询问整个表达式的值

不同点在于up函数需要进行修改,根据区间长度不同运算符号也有所不同

代码:

#include <bits/stdc++.h>
#define int long long
#define sc(a) scanf("%lld",&a)
#define scc(a,b) scanf("%lld %lld",&a,&b)
#define sccc(a,b,c) scanf("%lld %lld %lld",&a,&b,&c)
#define schar(a) scanf("%c",&a)
#define pr(a) printf("%lld",a)
#define fo(i,a,b) for(int i=a;i<b;++i)
#define re(i,a,b) for(int i=a;i<=b;++i)
#define rfo(i,a,b) for(int i=a;i>b;--i)
#define rre(i,a,b) for(int i=a;i>=b;--i)
#define prn() printf("\n")
#define prs() printf(" ")
#define mkp make_pair
#define pii pair<int,int>
#define pub(a) push_back(a)
#define pob() pop_back()
#define puf(a) push_front(a)
#define pof() pop_front()
#define fst first
#define snd second
#define frt front()
#define bak back()
#define mem0(a) memset(a,0,sizeof(a))
#define memmx(a) memset(a,0x3f3f,sizeof(a))
#define memmn(a) memset(a,-0x3f3f,sizeof(a))
#define debug
#define db double
#define yyes cout<<"YES"<<endl;
#define nno cout<<"NO"<<endl;
using namespace std;
typedef vector<int> vei;
typedef vector<pii> vep;
typedef map<int,int> mpii;
typedef map<char,int> mpci;
typedef map<string,int> mpsi;
typedef deque<int> deqi;
typedef deque<char> deqc;
typedef priority_queue<int> mxpq;
typedef priority_queue<int,vector<int>,greater<int> > mnpq;
typedef priority_queue<pii> mxpqii;
typedef priority_queue<pii,vector<pii>,greater<pii> > mnpqii;
const int maxn=500005;
const int inf=0x3f3f3f3f3f3f3f3f;
const int MOD=100000007;
const db eps=1e-10;
int qpow(int a,int b)int tmp=a%MOD,ans=1;while(b)if(b&1)ans*=tmp,ans%=MOD;tmp*=tmp,tmp%=MOD,b>>=1;return ans;
int lowbit(int x)return x&-x;
int max(int a,int b)return a>b?a:b;
int min(int a,int b)return a<b?a:b;
int mmax(int a,int b,int c)return max(a,max(b,c));
int mmin(int a,int b,int c)return min(a,min(b,c));
void mod(int &a)a+=MOD;a%=MOD;
bool chk(int now)
int half(int l,int r)while(l<=r)int m=(l+r)/2;if(chk(m))r=m-1;else l=m+1;return l;
int ll(int p)return p<<1;
int rr(int p)return p<<1|1;
int mm(int l,int r)return (l+r)/2;
int lg(int x)if(x==0) return 1;return (int)log2(x)+1;
bool smleql(db a,db b)if(a<b||fabs(a-b)<=eps)return true;return false;
db len(db a,db b,db c,db d)return sqrt((a-c)*(a-c)+(b-d)*(b-d));
bool isp(int x)if(x==1)return false;if(x==2)return true;for(int i=2;i*i<=x;++i)if(x%i==0)return false;return true;
inline int read()
    char ch=getchar();int s=0,w=1;
    while(ch<48||ch>57)if(ch==-)w=-1;ch=getchar();
    while(ch>=48&&ch<=57)s=(s<<1)+(s<<3)+ch-48;ch=getchar();
    return s*w;

inline void write(int x)
    if(x<0)putchar(-),x=-x;
    if(x>9)write(x/10);
    putchar(x%10+48);


int n,m,x,y;
int a[maxn],tr[maxn];
void up(int p,int l,int r)
    int k=1;
    while((1LL<<k)<(r-l+1)) k++;
    if(k&1) tr[p]=(tr[ll(p)]|tr[rr(p)]);
    else tr[p]=(tr[ll(p)]^tr[rr(p)]);

void build(int p,int l,int r)
    if(l==r)
        tr[p]=a[l];
        return;
    
    int m=mm(l,r);
    build(ll(p),l,m);
    build(rr(p),m+1,r);
    up(p,l,r);

int ask()return tr[1];
void modify(int p,int l,int r,int x,int y)
    if(l==r&&l==x)
        tr[p]=y;
        return;
    
    int m=mm(l,r);
    if(x<=m) modify(ll(p),l,m,x,y);
    else modify(rr(p),m+1,r,x,y);
    up(p,l,r);


signed main()
    ios_base::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    cin>>n>>m;
    re(i,1,(1LL<<n)) cin>>a[i];
    build(1,1,(1LL<<n));
    while(m--)
        cin>>x>>y;
        modify(1,1,(1LL<<n),x,y);
        cout<<ask()<<endl;
    
    return 0;

 

以上是关于CF339D Xenia and Bit Operations(线段树)的主要内容,如果未能解决你的问题,请参考以下文章

D - Xenia and Bit Operations

CF R 635 div2 1337D Xenia and Colorful Gems 贪心 二分 双指针

codeforces 342E :Xenia and Tree

CF1109A Sasha and a Bit of Relax

CodeForces-339B-Xenia and Ringroad (循环队列,水题)

B. Xenia and Ringroad1000 / 水题 模拟