题意:

已知\(n\)个数字,进行以下操作:

  • \(1.\)区间\([L,R]\) 按位与\(x\)
  • \(2.\)区间\([L,R]\) 按位或\(x\)
  • \(3.\)区间\([L,R]\) 询问最大值

思路:

吉司机线段树。

我们按位考虑,维护区间或\(\_or\)和区间与\(\_and\),那么得到区间非公有的\(1\)为\((\_or \oplus \_and)\),那么如果对所有的非公有的\(1\)影响都一样就不会对最大值有影响,那么就直接打标机,否则继续往下更新。即

\[[(\_or[rt] \oplus \_and[rt]) \& x] == 0 || [(\_or[rt] \oplus \_and[rt]) \& x] == (\_or[rt] \oplus \_and[rt])
\]

时就直接打标记。

代码:

#include<map>
#include<set>
#include<queue>
#include<cmath>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const int maxn = 2e5 + 5;
const int MAXM = 3e6;
const ll MOD = 998244353;
const ull seed = 131;
const int INF = 0x3f3f3f3f; #define lson (rt << 1)
#define rson (rt << 1 | 1)
inline bool read(int &num){
char in;
bool IsN=false;
in = getchar();
if(in == EOF) return false;
while(in != '-' && (in < '0' || in > '9')) in = getchar();
if(in == '-'){ IsN = true; num = 0;}
else num = in - '0';
while(in = getchar(),in >= '0' && in <= '9'){
num *= 10, num += in-'0';
}
if(IsN) num = -num;
return true;
} int a[maxn], all = (1 << 21) - 1;
int _or[maxn << 2], _and[maxn << 2], Max[maxn << 2];
int lazya[maxn << 2], lazyo[maxn << 2];
inline void pushup(int rt){
_or[rt] = _or[lson] | _or[rson];
_and[rt] = _and[lson] & _and[rson];
Max[rt] = max(Max[lson], Max[rson]);
}
inline void pushdown(int rt, int l, int r){
int m = (l + r) >> 1;
if(lazya[rt] != all){
Max[lson] &= lazya[rt];
Max[rson] &= lazya[rt];
_or[lson] &= lazya[rt];
_or[rson] &= lazya[rt];
_and[lson] &= lazya[rt];
_and[rson] &= lazya[rt];
lazya[lson] &= lazya[rt];
lazya[rson] &= lazya[rt];
lazyo[lson] &= lazya[rt];
lazyo[rson] &= lazya[rt];
lazya[rt] = all;
}
if(lazyo[rt] != 0){
Max[lson] |= lazyo[rt];
Max[rson] |= lazyo[rt];
_or[lson] |= lazyo[rt];
_or[rson] |= lazyo[rt];
_and[lson] |= lazyo[rt];
_and[rson] |= lazyo[rt];
lazya[lson] |= lazyo[rt];
lazya[rson] |= lazyo[rt];
lazyo[lson] |= lazyo[rt];
lazyo[rson] |= lazyo[rt];
lazyo[rt] = 0;
}
}
void build(int l, int r, int rt){
lazya[rt] = all, lazyo[rt] = 0;
if(l == r){
_and[rt] = _or[rt] = Max[rt] = a[l];
return;
}
int m = (l + r) >> 1;
build(l, m, lson);
build(m + 1, r, rson);
pushup(rt);
}
void update(int L, int R, int l, int r, int op, int x, int rt){
if(L <= l && R >= r){
if(op == 1){ //&
if(((_or[rt] ^ _and[rt]) & x) == 0 || ((_or[rt] ^ _and[rt]) & x) == (_or[rt] ^ _and[rt])){
Max[rt] &= x;
_or[rt] &= x;
_and[rt] &= x;
lazya[rt] &= x;
lazyo[rt] &= x;
return;
}
}
else{ //|
if(((_or[rt] ^ _and[rt]) & x) == 0 || ((_or[rt] ^ _and[rt]) & x) == (_or[rt] ^ _and[rt])){
Max[rt] |= x;
_or[rt] |= x;
_and[rt] |= x;
lazya[rt] |= x;
lazyo[rt] |= x;
return;
}
}
}
int m = (l + r) >> 1;
pushdown(rt, l, r);
if(L <= m)
update(L, R, l, m, op, x, lson);
if(R > m)
update(L, R, m + 1, r, op, x, rson);
pushup(rt);
}
int query(int L, int R, int l, int r, int rt){
if(L <= l && R >= r){
return Max[rt];
}
pushdown(rt, l, r);
int m = (l + r) >> 1, MAX = -INF;
if(L <= m)
MAX = max(MAX, query(L, R, l, m, lson));
if(R > m)
MAX = max(MAX, query(L, R, m + 1, r, rson));
return MAX; }
int main(){
int n, m;
read(n), read(m);
for(int i = 1; i <= n; i++) read(a[i]);
build(1, n, 1);
while(m--){
int op, l, r, x;
read(op), read(l), read(r);
if(op < 3) read(x);
if(op < 3){
update(l, r, 1, n, op, x, 1);
}
else{
printf("%d\n", query(l, r, 1, n, 1));
}
}
return 0;
}

bzoj5312 冒险(吉司机线段树)题解的更多相关文章

  1. bzoj4355 Play with sequence(吉司机线段树)题解

    题意: 已知\(n\)个数字,进行以下操作: \(1.\)区间\([L,R]\) 赋值为\(x\) \(2.\)区间\([L,R]\) 赋值为\(max(a[i] + x, 0)\) \(3.\)区间 ...

  2. bzoj4695 最假女选手(势能线段树/吉司机线段树)题解

    题意: 已知\(n\)个数字,进行以下操作: \(1.\)给一个区间\([L,R]\) 加上一个数\(x\) \(2.\)把一个区间\([L,R]\) 里小于\(x\) 的数变成\(x\) \(3.\ ...

  3. BZOJ4355: Play with sequence(吉司机线段树)

    题意 题目链接 Sol 传说中的吉司机线段树??感觉和BZOJ冒险那题差不多,就是强行剪枝... 这题最坑的地方在于对于操作1,$C >= 0$, 操作2中需要对0取max,$a[i] > ...

  4. HDU - 5306 Gorgeous Sequence (吉司机线段树)

    题目链接 吉司机线段树裸题... #include<bits/stdc++.h> using namespace std; typedef long long ll; ,inf=0x3f3 ...

  5. UVALive - 4108 SKYLINE (吉司机线段树)

    题目链接 题意:在一条直线上依次建造n座建筑物,每座建筑物建造完成后询问它在多长的部分是最高的. 比较好想的方法是用线段树分别维护每个区间的最小值mi和最大值mx,当建造一座高度为x的建筑物时,若mi ...

  6. HDU - 6315 吉司机线段树

    题意:给出a,b数组,区间上两种操作,给\(a[L,R]\)+1s,或者求\(\sum_{i=l}^{r}a_i/b_i\) 一看就知道是吉司机乱搞型线段树(低配版),暴力剪枝就好 维护区间a的最大值 ...

  7. BZOJ5312 冒险(势能线段树)

    BZOJ题目传送门 表示蒟蒻并不能一眼看出来这是个势能线段树. 不过仔细想想也并非难以理解,感性理解一下,在一个区间里又与又或,那么本来不相同的位也会渐渐相同,线段树每个叶子节点最多修改\(\log ...

  8. HDU 5306 吉司机线段树

    思路: 后面nlogn的部分是伪证... 大家可以构造数据证明是这是nlog^2n的啊~ 吉老司机翻车了 //By SiriusRen #include <cstdio> #include ...

  9. hdu6521 吉司机线段树

    http://acm.hdu.edu.cn/showproblem.php?pid=6521 待填 代码 #include<bits/stdc++.h> #define ls o<& ...

随机推荐

  1. ubuntu20.04并添加桌面快捷方式,以安装火狐可浏览器开发版(水狐)为例

    @参考原文 1. 下载linux版源文件 从火狐官网下载linux版的水狐源文件压缩包,@火狐浏览器开发版(水狐)下载地址. 2. 解压下载源文件 将下载的"tar.bz2"文件解 ...

  2. [USACO13DEC]牛奶调度Milk Scheduling

    原题链接https://www.lydsy.com/JudgeOnline/problem.php?id=4096 容易想到的一个测略就是,优先考虑结束时间小的牛.所以我们对所有牛按照结束时间排序.然 ...

  3. 前端知识(二)08-Vue.js的路由-谷粒学院

    目录 一.锚点的概念 二.路由的作用 三.路由实例 1.复制js资源 2.创建 路由.html 3.引入js 4.编写html 5.编写js 一.锚点的概念 案例:百度百科 特点:单页Web应用,预先 ...

  4. spring源码分析之玩转ioc:bean初始化和依赖注入(一)

    最近赶项目,天天加班到十一二点,终于把文档和代码都整完了,接上继续整. 上一篇聊了beanProcess的注册以及对bean的自定义修改和添加,也标志着创建bean的准备工作都做好了,接下来就是开大招 ...

  5. 一个实体对象不能由多个 IEntityChangeTracker 实例引用

    因为需求需要EF 实现批量的删除后插入,所以出现了这个报错, 这个报错的原因是,EF查询是有带跟踪的,跟踪后其他上下文想操作这个实体就会报错. 所以,查询使用 ef AsNoTracking 查后无追 ...

  6. 透明小电视上线——GitHub 热点速览 v.21.05

    作者:HelloGitHub-小鱼干 这周的 GitHub Trending 真是棒极了.小鱼干喜欢的科技博主又开源了他的硬件玩具,一个透明的小电视机,HG 的小伙伴看完项目,再买个电路板和分光棱镜, ...

  7. 四:Spring Security 登录使用 JSON 格式数据

    Spring Security 登录使用 JSON 格式数据 1.基本登录方案 1.1 创建 Spring Boot 工程 1.2 添加 Security 配置 2.使用JSON登录 江南一点雨-Sp ...

  8. OpenStack (云计算与openstck简介)

    云计算 什么是云计算 云计算是一种按使用量付费的模式,这种模式提供可用的,便捷的,按需的网络访问,通过互联网进入可配置的计算资源共享池(资源包括,计算,存储,应用软件和服务) 云计算的特征 易于管理: ...

  9. WPF权限控制——【2】模块、菜单、按钮

    周末没有工作,没有写博客,因为觉得休息很必要:曾听到一句话是这样说的:"你们得救在乎归回安息:你们得力在乎平静安稳".当我想到太阳没秒钟要燃烧420万吨的燃料时,想到的就是造物主的 ...

  10. 给jekyll博客添加搜索功能

    使用SWIFTYPE为jekyll博客添加搜索引擎 步骤 1.首先去swiftype注册一个账号 2.接着添加自己想要配置的网站地址并为新设定的引擎添加一个名字(非会员只能设置一个引擎). 3.收到验 ...