题意:

已知\(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. Gradle使用及配置

    构建工具:Gradle(6.8) 下载地址:https://gradle.org/releases/ 下载最新版的二进制文件即可,下载"gradle-6.8.1-bin.zip文件,下载后完 ...

  2. Java SPI机制详解

    Java SPI机制详解 1.什么是SPI? SPI 全称为 (Service Provider Interface) ,是JDK内置的一种服务提供发现机制.SPI是一种动态替换发现的机制, 比如有个 ...

  3. 攻击JWT的一些方法

    JWT安全隐患之绕过访问控制 https://mp.weixin.qq.com/s/xe8vOVhaysmgvxl-A3nkBA 记录一次JWT的越权渗透测试 https://mp.weixin.qq ...

  4. Enabling Session Persistence 粘性会话

    NGINX Docs | HTTP Load Balancing https://docs.nginx.com/nginx/admin-guide/load-balancer/http-load-ba ...

  5. win api 窗口操作-窗口置顶与寻找与激活

    https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setwindowpos https://docs.micr ...

  6. Linux命令——netcat

    简介 netcat的简写是nc,被设计为一个简单.可靠的网络工具,主要作用如下: 1 实现任意TCP/UDP端口的侦听,nc可以作为server以TCP或UDP方式侦听指定端口 2 端口的扫描,nc可 ...

  7. 从零开始学Java (三)基础语法

    1. 基本数据类型 整数类型:byte,short,int,long 浮点数类型:float,double 字符类型:char 布尔类型:boolean java最小单位是bit,一个byte占用8个 ...

  8. springboot+Jenkins+docker-compose自动部署项目实践

    DevOps思想 一个开发.测试.运维的整个过程的思想. plan:需求.计划 code:编码 build:构建 test: 测试 release:发布版本 deploy:部署 operate:项目运 ...

  9. 反弹SHELL介绍及原理

    如果我们需要到服务器上执行 Shell 命令,但是因为防火墙等原因,无法由客户端主动发起连接的情况,就可以使用反弹 Shell 来满足登陆和操作的需求. 什么是反弹Shell 正常情况下,我们登陆服务 ...

  10. GeoServer发布Shapfile、PostGIS数据

    GeoServer发布Shapfile.PostGIS数据 一.GeoServer发布Shapfile数据 1.1 创建工作区 1.1.1 工作区 1.2 在工作区中加入新的数据存储 1.2.1 数据 ...