记录区间and/or,修改时如果对整个区间影响都相同就打标记,否则递归。复杂度不太会证。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
#define N 200010
char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<''||c>'')) c=getchar();return c;}
int gcd(int n,int m){return m==?n:gcd(m,n%m);}
int read()
{
int x=,f=;char c=getchar();
while (c<''||c>'') {if (c=='-') f=-;c=getchar();}
while (c>=''&&c<='') x=(x<<)+(x<<)+(c^),c=getchar();
return x*f;
}
int n,m,a[N];
struct data{int l,r,max,AND,OR,lazy_and,lazy_or;
}tree[N<<];
void up(int k)
{
tree[k].max=max(tree[k<<].max,tree[k<<|].max),
tree[k].AND=tree[k<<].AND&tree[k<<|].AND,
tree[k].OR=tree[k<<].OR|tree[k<<|].OR;
}
void update(int k,int x,int op)
{
if (op==)
{
tree[k].AND&=x,tree[k].OR&=x;
if (tree[k].lazy_and==-) tree[k].lazy_and=x;
else tree[k].lazy_and&=x;
if (tree[k].lazy_or!=-) tree[k].lazy_or&=x;
tree[k].max&=x;
}
else
{
tree[k].AND|=x,tree[k].OR|=x;
if (tree[k].lazy_and!=-) tree[k].lazy_and|=x;
if (tree[k].lazy_or==-) tree[k].lazy_or=x;
else tree[k].lazy_or|=x;
tree[k].max|=x;
}
}
void down(int k)
{
if (tree[k].lazy_and!=-)
{
update(k<<,tree[k].lazy_and,),
update(k<<|,tree[k].lazy_and,),
tree[k].lazy_and=-;
}
if (tree[k].lazy_or!=-)
{
update(k<<,tree[k].lazy_or,),
update(k<<|,tree[k].lazy_or,),
tree[k].lazy_or=-;
}
}
void build(int k,int l,int r)
{
tree[k].l=l,tree[k].r=r,tree[k].lazy_and=tree[k].lazy_or=-;
if (l==r) {update(k,a[l],);return;}
int mid=l+r>>;
build(k<<,l,mid);
build(k<<|,mid+,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))==) {update(k,x,op);return;}
down(k);
int mid=tree[k].l+tree[k].r>>;
if (r<=mid) modify(k<<,l,r,x,op);
else if (l>mid) modify(k<<|,l,r,x,op);
else modify(k<<,l,mid,x,op),modify(k<<|,mid+,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>>;
if (r<=mid) return query(k<<,l,r);
else if (l>mid) return query(k<<|,l,r);
else return max(query(k<<,l,mid),query(k<<|,mid+,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=;i<=n;i++) a[i]=read();
build(,,n);
while (m--)
{
int op=read();
if (op==)
{
int l=read(),r=read(),x=read();
modify(,l,r,x,);
}
else if (op==)
{
int l=read(),r=read(),x=read();
modify(,l,r,x,);
}
else
{
int l=read(),r=read();
printf("%d\n",query(,l,r));
}
}
return ;
}