Sonya and Bitwise OR CodeForces - 1004F (线段树,分治)
大意: 给定序列$a$, 给定整数$x$. 两种操作(1)单点修改 (2)给定区间$[l,r]$,求有多少子区间满足位或和不少于$x$.
假设不带修改. 固定右端点, 合法区间关于左端点单调的. 可以预处理出最近的合法的左端点位置.那么每次询问答案就为$\sum\limits_{\substack{pre[i]\ge l\\ l \le i\le r}}(pre[i]-l+1)$. 用二维数点的方法处理即可.
带修改的话, 关键是要注意到或和最多改变$20$次, 线段树记录下来改变的位置, 暴力合并即可.
#include <iostream>
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#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 hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii; const int N = 1e5+10;
int n,m,X,a[N];
struct _ {
int lnum,rnum,x,L,R;
ll ans;
pii l[22],r[22];
_ () {}
_ (int lnum,int rnum,int x,int L,int R,ll ans):lnum(lnum),rnum(rnum),x(x),L(L),R(R),ans(ans) {}
_ (int pos, int val) {
L=R=l[1].y=r[1].y=pos;
l[1].x=r[1].x=x=val;
ans=(val<X);
lnum=rnum=1;
}
_ operator + (const _& rhs) const {
_ ret(lnum,rhs.rnum,x|rhs.x,L,rhs.R,ans+rhs.ans);
memcpy(ret.l,l,sizeof l);
memcpy(ret.r,rhs.r,sizeof r);
for (int i=rnum,j=0; i; --i) {
while (j<rhs.lnum&&(r[i].x|rhs.l[j+1].x)<X) ++j;
ret.ans += (ll)(r[i].y-(i<rnum?r[i+1].y:L-1))*((j<rhs.lnum?rhs.l[j+1].y:rhs.R+1)-rhs.L);
}
REP(i,1,rhs.lnum) if ((rhs.l[i].x|x)!=ret.l[ret.lnum].x) {
(ret.l[++ret.lnum]=rhs.l[i]).x |= x;
}
REP(i,1,rnum) if ((r[i].x|rhs.x)!=ret.r[ret.rnum].x) {
(ret.r[++ret.rnum]=r[i]).x |= rhs.x;
}
return ret;
}
} tr[N<<2];
void build(int o, int l, int r) {
if (l==r) tr[o]=_(l,a[l]);
else build(ls),build(rs),tr[o]=tr[lc]+tr[rc];
}
void update(int o, int l, int r, int x, int v) {
if (l==r) tr[o]=_(x,v);
else {
mid>=x?update(ls,x,v):update(rs,x,v);
tr[o]=tr[lc]+tr[rc];
}
}
_ query(int o, int l, int r, int ql, int qr) {
if (ql<=l&&r<=qr) return tr[o];
if (mid>=qr) return query(ls,ql,qr);
if (mid<ql) return query(rs,ql,qr);
return query(ls,ql,qr)+query(rs,ql,qr);
}
int main() {
scanf("%d%d%d", &n, &m, &X);
REP(i,1,n) scanf("%d", a+i);
build(1,1,n);
while (m--) {
int op,x,y;
scanf("%d%d%d", &op, &x, &y);
if (op==1) update(1,1,n,x,y);
else printf("%lld\n",(y-x+1ll)*(y-x+2)/2-query(1,1,n,x,y).ans);
}
}
Sonya and Bitwise OR CodeForces - 1004F (线段树,分治)的更多相关文章
- Codeforces 938G 线段树分治 线性基 可撤销并查集
Codeforces 938G Shortest Path Queries 一张连通图,三种操作 1.给x和y之间加上边权为d的边,保证不会产生重边 2.删除x和y之间的边,保证此边之前存在 3.询问 ...
- Codeforces 1140F 线段树 分治 并查集
题意及思路:https://blog.csdn.net/u013534123/article/details/89010251 之前cf有一个和这个相似的题,不过那个题只有合并操作,没有删除操作,直接 ...
- Sereja and Brackets CodeForces - 380C (线段树+分治思路)
Sereja and Brackets 题目链接: CodeForces - 380C Sereja has a bracket sequence s1, s2, ..., *s**n, or, in ...
- Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论
Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论 题意 给你一段数,然后小明去猜某一区间内的gcd,这里不一定是准确值,如果在这个区间内改变 ...
- loj#2312. 「HAOI2017」八纵八横(线性基 线段树分治)
题意 题目链接 Sol 线性基+线段树分治板子题.. 调起来有点自闭.. #include<bits/stdc++.h> #define fi first #define se secon ...
- BZOJ.4184.shallot(线段树分治 线性基)
BZOJ 裸的线段树分治+线性基,就是跑的巨慢_(:з」∠)_ . 不知道他们都写的什么=-= //41652kb 11920ms #include <map> #include < ...
- BZOJ.4137.[FJOI2015]火星商店问题(线段树分治 可持久化Trie)
BZOJ 洛谷 一直觉得自己非常zz呢.现在看来是真的=-= 注意题意描述有点问题,可以看BZOJ/洛谷讨论. 每个询问有两个限制区间,一是时间限制\([t-d+1,t]\),二是物品限制\([L,R ...
- 洛谷.3733.[HAOI2017]八纵八横(线性基 线段树分治 bitset)
LOJ 洛谷 最基本的思路同BZOJ2115 Xor,将图中所有环的异或和插入线性基,求一下线性基中数的异或最大值. 用bitset优化一下,暴力的复杂度是\(O(\frac{qmL^2}{w})\) ...
- bzoj4025二分图(线段树分治 并查集)
/* 思维难度几乎没有, 就是线段树分治check二分图 判断是否为二分图可以通过维护lct看看是否链接出奇环 然后发现不用lct, 并查集维护奇偶性即可 但是复杂度明明一样哈 */ #include ...
随机推荐
- oracle之nvl,nvl2,decode
oracle sql常用查询nvl,nvl2,decode区别及使用方法 1,NVL( E1, E2) 如果E1为NULL,则NVL函数返回E2的值,否则返回E1的值,如果两个参数都为NULL ,则返 ...
- 【Python 脚本报错】AttributeError: 'module 'yyy' has no attribute 'xxx'的解决方法
先参考这篇记录大概理解了原因, 再深入了解下python的import机制, 发现自己的模块之间存在互相import. 比如,A.py中import B,而B.py中也import A了, 现在执行模 ...
- 三种Timer使用
System.Windows.Forms.Timer, System.Threading.Timer, System.Timer,三种Timer使用如下 第一种:System.Windows.Fo ...
- Linux shell脚本 (十二)case语句
case语句 case ... esac 与其他语言中的 switch ... case 语句类似,是一种多分枝选择结构. case 语句匹配一个值或一个模式,如果匹配成功,执行相匹配的命令.case ...
- CDH 安装的博客地址记录
1. 集群环境配置 https://www.cnblogs.com/yinzhengjie/articles/11019333.html 2. 二进制方法安装 https://www.cnblogs. ...
- VUE判断可用对象是否为空
方法一: JSON.stringify(formmanage_listVue.updataObj)=='{}' var data = {}; var b = (JSON.stringify(data) ...
- web自动化01-自动化解决的问题-环境搭建-元素定位
自动化测试 概念:由程序代码代替人工完成验证系统功能的过程 解决的问题: 回归测试 压力测试 兼容性测试 提高测试效率,提升产品质量 自动化测试分类 web自动化测试 移动自动化测试 接口自动化测试 ...
- 【微信小程序】wx.navigateBack() 携带参数返回
第一个页面: go_pick_time:function(e){ var that = this; var type = e.currentTarget.dataset.type; wx.naviga ...
- 查看php 某个服务的进程数
查看进程就是使用ps命令而已,只不顾ps的参数太多了. 使用php查询的话,必须要开启几个函数(可以执行外部程序的函数),参考官网:http://php.net/manual/zh/book.exec ...
- Java点滴-List<Integer> list; 中尖括号的意思
这是jdk1.5后版本才有的新特性,泛型,指定传入的类型.这样定义之后,这个list只能接收Integer的对象. 以前没有加这个,传入的都是Object类型的,取出来的时候要强制类型转换为自己想要的 ...