题意:

已知\(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. ReactRouter的实现

    ReactRouter的实现 ReactRouter是React的核心组件,主要是作为React的路由管理器,保持UI与URL同步,其拥有简单的API与强大的功能例如代码缓冲加载.动态路由匹配.以及建 ...

  2. MySQL增删改操作

    增删改操作 增加 看语法 1. 插入完整数据(顺序插入) 语法一: INSERT INTO 表名(字段1,字段2,字段3-字段n) VALUES(值1,值2,值3-值n); #指定字段来插入数据,插入 ...

  3. uni-app开发经验分享十: 封装request请求

    http.js //封装requset,uploadFile和downloadFile请求,新增get和post请求方法 let http = { 'setBaseUrl': (url) => ...

  4. Spring Security,没有看起来那么复杂(附源码)

    权限管理是每个项目必备的功能,只是各自要求的复杂程度不同,简单的项目可能一个 Filter 或 Interceptor 就解决了,复杂一点的就可能会引入安全框架,如 Shiro, Spring Sec ...

  5. MySQL调优性能监控之show profile

    用show profile查询工具指定具体的type show profile在mysql5.7之后过时 show profile命令用于跟踪执行过的sql语句的资源消耗信息,可以帮助查看sql语句的 ...

  6. python 使用函数名的字符串调用函数(4种方法)_black-heart的专栏-CSDN博客 https://blog.csdn.net/mrqingyu/article/details/84403924

    funcs = ['fetch_data_' + i for i in ( 'activities', 'banners', 'server_list')]# from operator import ...

  7. Redis 底层数据结构设计

    10万+QPS 真的只是因为单线程和基于内存?_Howinfun的博客-CSDN博客_qps面试题 https://blog.csdn.net/Howinfun/article/details/105 ...

  8. TCP/IP网络中的显式拥塞通告(ECN)

    当前的TCP 实现将TCP 端节点之间的中间网络视为一个不透明的"黑盒".TCP 包进入和流出这个盒子.有些时候进入盒子的包被丢失了.因为今天的数字和光媒体上出现比特级错误的机会非 ...

  9. Java Socket InetAddress类 Socket DatagramPacket TCP、UDP示例

    java.net :为实现网络应用程序提供类. InetAddress类 方法摘要 方法摘要 boolean equals(Object obj) : 将此对象与指定对象比较. byte[] getA ...

  10. Python程序中首行#!/usr/bin/env python的作用

    1.通常我们在pycharm中写程序的时候会在首行写上#!/usr/bin/env python 如: #!/usr/bin/env python3#-*-coding: UTF-8 -*-#Auth ...