2021ccpc黑龙江省赛A题(吉司机线段树)

吉司机线段树处理按位与的模板题
传送门

#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;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值