吉司机线段树处理按位与的模板题
传送门
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define per(i, a, n) for (int i = n; i >= a; i--)
#define lowbit(x) ((x) & -(x))
#define lson p << 1 , l, mid
#define rson p << 1 | 1, mid + 1, r
#define mem(a, b) memset(a, b, sizeof(a))
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
const double PI = acos(-1.0);
const int N = 4e5+50;
const int inf = 2147483647;
int n,m;
struct node{
int mx,sam,tag;
int l,r;
}tree[N<<2];
char s[6];
void push_up(int p){
tree[p].mx = max(tree[p<<1].mx,tree[p<<1|1].mx);
tree[p].sam = tree[p<<1].sam & tree[p<<1|1].sam & (~(tree[p<<1].mx^tree[p<<1|1].mx));
}
void push_tag(int p,int x){
tree[p].mx += x;
tree[p].tag += x;
}
void push_down(int p){
if(tree[p].tag == 0)
return;
push_tag(p<<1,tree[p].tag);
push_tag(p<<1|1,tree[p].tag);
tree[p].tag = 0;
}
void build_tree(int l,int r,int p){
tree[p].l = l,tree[p].r = r;
if(l == r){
scanf("%d",&tree[p].mx);
tree[p].sam = inf;
tree[p].tag = 0;
return;
}
int mid = (l + r) >> 1;
build_tree(l,mid,p<<1);
build_tree(mid+1,r,p<<1|1);
push_up(p);
}
bool check(int p,int x){
int tmp = (x ^ inf);
return (tmp & tree[p].sam) == tmp;
}
void modify_AND(int l,int r,int p,int x){
if(l <= tree[p].l && tree[p].r <= r && check(p,x)){
int t = (tree[p].mx & x ) - tree[p].mx;
push_tag(p,t);
return;
}
push_down(p);
int mid = (tree[p].l + tree[p].r) >> 1;
if(l <= mid)
modify_AND(l,r,p<<1,x);
if(mid < r)
modify_AND(l,r,p<<1|1,x);
push_up(p);
}
void modify_CHANGE(int l,int r,int p,int x){
if(l <= tree[p].l && tree[p].r <= r){
tree[p].mx = x;
tree[p].sam = inf;
tree[p].tag = 0;
return;
}
push_down(p);
int mid = (tree[p].l + tree[p].r) >> 1;
if(l <= mid)
modify_CHANGE(l,r,p<<1,x);
else
modify_CHANGE(l,r,p<<1|1,x);
push_up(p);
}
int ask_max(int l,int r,int p){
if(l <= tree[p].l && tree[p].r <= r){
return tree[p].mx;
}
int res = 0;
push_down(p);
int mid = (tree[p].l + tree[p].r) >> 1;
if(l <= mid)
res = max(res,ask_max(l,r,p<<1));
if(mid < r)
res = max(res,ask_max(l,r,p<<1|1));
return res;
}
int main()
{
scanf("%d%d",&n,&m);
build_tree(1,n,1);
int l,r,x;
for(int i = 1; i <= m ;i++){
scanf(" %s %d%d",s,&l,&r);
if(s[0] == 'A')
{
scanf("%d",&x);
modify_AND(l,r,1,x);
}
else if (s[0] == 'U')
modify_CHANGE(l,l,1,r);
else{
printf("%d\n",ask_max(l,r,1));
}
}
return 0;
}
1279

被折叠的 条评论
为什么被折叠?



