记录区间and/or,修改时如果对整个区间影响都相同就打标记,否则递归。复杂度不太会证。
#include#include #include #include #include #include using namespace std;#define ll long long#define N 200010char getc(){ char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<'0'||c>'9')) c=getchar();return c;}int gcd(int n,int m){ return m==0?n:gcd(m,n%m);}int read(){ int x=0,f=1;char c=getchar(); while (c<'0'||c>'9') { if (c=='-') f=-1;c=getchar();} while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar(); return x*f;}int n,m,a[N];struct data{ int l,r,max,AND,OR,lazy_and,lazy_or;}tree[N<<2];void up(int k){ tree[k].max=max(tree[k<<1].max,tree[k<<1|1].max), tree[k].AND=tree[k<<1].AND&tree[k<<1|1].AND, tree[k].OR=tree[k<<1].OR|tree[k<<1|1].OR;}void update(int k,int x,int op){ if (op==0) { tree[k].AND&=x,tree[k].OR&=x; if (tree[k].lazy_and==-1) tree[k].lazy_and=x; else tree[k].lazy_and&=x; if (tree[k].lazy_or!=-1) tree[k].lazy_or&=x; tree[k].max&=x; } else { tree[k].AND|=x,tree[k].OR|=x; if (tree[k].lazy_and!=-1) tree[k].lazy_and|=x; if (tree[k].lazy_or==-1) tree[k].lazy_or=x; else tree[k].lazy_or|=x; tree[k].max|=x; }}void down(int k){ if (tree[k].lazy_and!=-1) { update(k<<1,tree[k].lazy_and,0), update(k<<1|1,tree[k].lazy_and,0), tree[k].lazy_and=-1; } if (tree[k].lazy_or!=-1) { update(k<<1,tree[k].lazy_or,1), update(k<<1|1,tree[k].lazy_or,1), tree[k].lazy_or=-1; }}void build(int k,int l,int r){ tree[k].l=l,tree[k].r=r,tree[k].lazy_and=tree[k].lazy_or=-1; if (l==r) {update(k,a[l],1);return;} int mid=l+r>>1; build(k<<1,l,mid); build(k<<1|1,mid+1,r); up(k);}void modify(int k,int l,int r,int x,int op){ if (tree[k].l==l&&tree[k].r==r&&(tree[k].OR&(~tree[k].AND))==0) {update(k,x,op);return;} down(k); int mid=tree[k].l+tree[k].r>>1; if (r<=mid) modify(k<<1,l,r,x,op); else if (l>mid) modify(k<<1|1,l,r,x,op); else modify(k<<1,l,mid,x,op),modify(k<<1|1,mid+1,r,x,op); up(k);}int query(int k,int l,int r){ if (tree[k].l==l&&tree[k].r==r) return tree[k].max; down(k); int mid=tree[k].l+tree[k].r>>1; if (r<=mid) return query(k<<1,l,r); else if (l>mid) return query(k<<1|1,l,r); else return max(query(k<<1,l,mid),query(k<<1|1,mid+1,r));}int main(){#ifndef ONLINE_JUDGE freopen("bzoj5312.in","r",stdin); freopen("bzoj5312.out","w",stdout); const char LL[]="%I64d\n";#else const char LL[]="%lld\n";#endif n=read(),m=read(); for (int i=1;i<=n;i++) a[i]=read(); build(1,1,n); while (m--) { int op=read(); if (op==1) { int l=read(),r=read(),x=read(); modify(1,l,r,x,0); } else if (op==2) { int l=read(),r=read(),x=read(); modify(1,l,r,x,1); } else { int l=read(),r=read(); printf("%d\n",query(1,l,r)); } } return 0;}