CodeForces - 587E[线段树+线性基+差分] ->(线段树维护区间合并线性基)
题意:给你一个数组,有两种操作,一种区间xor一个值,一个是查询区间xor的结果的种类数
做法一:
对于一个给定的区间,我们可以通过求解线性基的方式求出结果的种类数,而现在只不过将其放在线树上维护区间线性基。
用线段树去维护区间合并
- #include <bits/stdc++.h>
- using namespace std;
- const int maxn = 1e5;
- struct node {
- int bas[],lazy,st;
- void init() {
- memset(bas,,sizeof(bas));
- lazy = ;st = ;
- }
- int Count() {
- int ans = ;
- for(int i = ;i<=;i++) {
- if(bas[i]) ans++;
- }
- return ans;
- }
- void add(int x) {
- for(int i = ;i>=;i--) {
- if((x>>i)&){
- if(bas[i])x^=bas[i];
- else {
- bas[i] = x;
- break;
- }
- }
- }
- }
- node operator + (const node &a) const{
- node ans; ans.init();
- for(int i = ;i<=;i++) {
- ans.add(bas[i]);
- ans.add(a.bas[i]);
- }
- ans.add(st^a.st);
- ans.st = a.st;
- return ans;
- }
- }tr[*maxn],ans;
- int n,q;
- void pushdown(int st) {
- if(tr[st].lazy) {
- tr[st<<].lazy ^= tr[st].lazy;
- tr[st<<].st ^= tr[st].lazy;
- tr[st<<|].lazy ^= tr[st].lazy;
- tr[st<<|].st ^= tr[st].lazy;
- tr[st].lazy = ;
- }
- }
- void build(int l,int r,int st) {
- tr[st].init();
- if(l == r) {
- scanf("%d",&tr[st].st);
- return ;
- }
- int mid = (l+r)>>;
- build(l,mid,st<<);
- build(mid+,r,st<<|);
- tr[st] = tr[st<<] + tr[st<<|];
- }
- void update(int l,int r,int st,int L,int R,int d) {
- if(l>=L && r<=R) {
- tr[st].lazy ^= d;
- tr[st].st ^= d;
- return ;
- }
- pushdown(st);
- int mid = (l+r) >>;
- if(L<=mid) update(l,mid,st<<,L,R,d);
- if(R> mid) update(mid+,r,st<<|,L,R,d);
- tr[st] = tr[st<<] + tr[st<<|];
- }
- void Query(int l,int r,int st,int L,int R) {
- if(l >= L && r <= R) {
- ans = ans + tr[st];
- return ;
- }
- pushdown(st);
- int mid = (l+r) >>;
- if(L<=mid) Query(l,mid,st<<,L,R);
- if(R>mid) Query(mid+,r,st<<|,L,R);
- tr[st] = tr[st<<] + tr[st<<|];
- }
- int main() {
- int op,l,r,d;
- scanf("%d %d",&n,&q);
- build(,n,);
- while(q--) {
- scanf("%d %d %d",&op,&l,&r);
- if(op == ) {
- scanf("%d",&d);
- update(,n,,l,r,d);
- }
- else {
- ans.init();
- Query(,n,,l,r);
- printf("%d\n",<<ans.Count());
- }
- }
- return ;
- }
做法二:
对原序列a进行差分,使得b[i] = a[i] ^ a[i+1],那么al,al+1,al+2,ar可以构成的一个异或和,在al,bl,bl+1,bl+2,br-1中肯定也可以被构造出来,因此二者是等价的。
对于a序列的区间[l,r]的更新实际上b序列上只有bl-1和br被影响,中间的数由于是两两所以抵消掉了。
所以现在就变成了单点更新线段树维护区间线性基,然后再用一个树状数组维护l位置被修改的值,这也用到了差分,这里的作用是要求a[l]的值。
- #include <bits/stdc++.h>
- #define lson l,mid,rt<<1
- #define rson mid+1,r,rt<<1|1
- using namespace std;
- const int mx = 2e5 + ;
- int n,m,a[mx],b[mx];
- int pre[mx],mv;
- struct node{
- int gao[];
- void add(int x){
- for(int i=;i>=;i--){
- if(x&(<<i)){
- if(!gao[i]){
- gao[i] = x;
- break;
- }else x ^= gao[i];
- }
- }
- }
- node operator + (node A)const
- {
- node ret = A;
- for(int i=;i>=;i--)
- if(gao[i]) ret.add(gao[i]);
- return ret;
- }
- }s[mx<<];
- void add(int x,int v){
- while(x<=n){
- pre[x] ^= v;
- x += x&(-x);
- }
- }
- int getpre(int x){
- int ans = ;
- while(x){
- ans ^= pre[x];
- x -= x&(-x);
- }
- return ans;
- }
- void build(int l,int r,int rt)
- {
- if(l==r){
- b[l] = a[l]^a[l+];
- for(int i=;i>=;i--)
- if((<<i)&b[l]){
- s[rt].gao[i] = b[l];
- break;
- }
- return ;
- }
- int mid = (l+r)>>;
- build(lson);build(rson);
- s[rt] = s[rt<<] + s[rt<<|];
- }
- void update(int l,int r,int rt,int M)
- {
- if(l==r){
- b[l] ^= mv;
- memset(s[rt].gao,,sizeof(s[rt].gao));
- for(int i=;i>=;i--)
- if((<<i)&b[l]){
- s[rt].gao[i] = b[l];
- break;
- }
- return ;
- }
- int mid = (l+r)>>;
- if(M<=mid) update(lson,M);
- else update(rson,M);
- s[rt] = s[rt<<] + s[rt<<|];
- }
- node query(int l,int r,int rt,int L,int R)
- {
- if(L<=l&&r<=R) return s[rt];
- int mid = (l+r)>>;
- if(L<=mid&&R>mid) return query(lson,L,R) + query(rson,L,R);
- if(L<=mid) return query(lson,L,R);
- return query(rson,L,R);
- }
- int main()
- {
- scanf("%d%d",&n,&m);
- for(int i=;i<=n;i++) scanf("%d",a+i);
- if(n>) build(,n-,);
- int o,l,r;
- while(m--){
- scanf("%d%d%d",&o,&l,&r);
- if(o==){
- scanf("%d",&mv);
- add(l,mv);add(r+,mv);
- if(l>) update(,n-,,l-);
- if(r<n) update(,n-,,r);
- }else{
- int v = a[l]^getpre(l);
- if(n==||l==r) printf("%d\n",v?:);
- else{
- node ans = query(,n-,,l,r-);
- ans.add(v);
- int ret = ;
- for(int i=;i>=;i--)
- if(ans.gao[i]) ret++;
- printf("%d\n",<<ret);
- }
- }
- }
- return ;
- }
CodeForces - 587E[线段树+线性基+差分] ->(线段树维护区间合并线性基)的更多相关文章
- [ZJOI2019]语言——树剖+树上差分+线段树合并
原题链接戳这儿 SOLUTION 考虑一种非常\(naive\)的统计方法,就是对于每一个点\(u\),我们维护它能到达的点集\(S_u\),最后答案就是\(\frac{\sum\limits_{i= ...
- HDU 1166 【线段树 || 树状数组,单点修改 维护区间和】
题目链接 HDU 1166 大概题意: 第一行一个整数T,表示有T组数据.每组数据第一行一个正整数N(N<=50000),表示敌人有N个工兵营地,接下来有N个正整数,第i个正整数ai代表第i个工 ...
- 【Luogu3733】[HAOI2017]八纵八横(线性基,线段树分治)
[Luogu3733][HAOI2017]八纵八横(线性基,线段树分治) 题面 洛谷 题解 看到求异或最大值显然就是线性基了,所以只需要把所有环给找出来丢进线性基里就行了. 然后线性基不资磁撤销?线段 ...
- G - Greg and Array CodeForces - 296C 差分+线段树
题目大意:输入n,m,k.n个数,m个区间更新标记为1~m.n次操作,每次操作有两个数x,y表示执行第x~y个区间更新. 题解:通过差分来表示某个区间更新操作执行的次数.然后用线段树来更新区间. #i ...
- CodeForces 1000C Covered Points Count(区间线段覆盖问题,差分)
https://codeforces.com/problemset/problem/1000/C 题意: 有n个线段,覆盖[li,ri],最后依次输出覆盖层数为1~n的点的个数. 思路: 区间线段覆盖 ...
- Codeforces 558E A Simple Task (计数排序&&线段树优化)
题目链接:http://codeforces.com/contest/558/problem/E E. A Simple Task time limit per test5 seconds memor ...
- [Vani有约会]雨天的尾巴(树上差分+线段树合并)
首先村落里的一共有n座房屋,并形成一个树状结构.然后救济粮分m次发放,每次选择两个房屋(x,y),然后对于x到y的路径上(含x和y)每座房子里发放一袋z类型的救济粮. 然后深绘里想知道,当所有的救济粮 ...
- Codeforces 750E New Year and Old Subsequence - 线段树 - 动态规划
A string t is called nice if a string "2017" occurs in t as a subsequence but a string &qu ...
- [NOIP2016 DAY1 T2]天天爱跑步-[差分+线段树合并][解题报告]
[NOIP2016 DAY1 T2]天天爱跑步 题面: B[NOIP2016 DAY1]天天爱跑步 时间限制 : - MS 空间限制 : 565536 KB 评测说明 : 2s Description ...
随机推荐
- Dynamic Programming and Policy Evaluation
Dynamic Programming divides the original problem into subproblems, and then complete the whole task ...
- PS把一张白色背景的图片设为透明
方法一: 1.双击图层缩略图上的小锁图标(注意,这里不要拖动小锁进行删除锁定),弹出“新建图层”,确定 2.右键左侧第四个功能菜单,选择魔棒工具 3.用魔棒工具在白色背景区域点击一下,选中白色区域背景 ...
- 前端 CSS的选择器 高级选择器
高级选择器分为: 后代选择器 儿子选择器 并集选择器 交集选择器 后代选择器 使用空格表示后代选择器.父元素的后代(包括儿子,孙子,重孙子) 后代选择器 在CSS中使用非常频繁 因为HTML元素可以嵌 ...
- TimeUnit类 java.util.concurrent.TimeUnit
TimeUnit是什么? TimeUnit是java.util.concurrent包下面的一个类,表示给定单元粒度的时间段 主要作用 时间颗粒度转换 延时 常用的颗粒度 TimeUnit.DAYS ...
- React手稿之 React-Saga
Redux-Saga redux-saga 是一个用于管理应用程序副作用(例如异步获取数据,访问浏览器缓存等)的javascript库,它的目标是让副作用管理更容易,执行更高效,测试更简单,处理故障更 ...
- Python所有转义字符总汇
转义字符就是让程序按照已经设置好的字符输出,不然程序当成其他的输出了,下面总结所有python转义字符 \\ 反斜杠符号\' 单引号\" 双引号\a 响铃\b 退格(Backspace)\e ...
- VirtualStringTree常用类和属性
重要的类:TBaseVirtualTree = class(TCustomControl)TCustomVirtualStringTree = class(TBaseVirtualTree)TVirt ...
- [APIO2019] 奇怪装置
$solution:$ 问题其实就是求两个式子的循环节. 钦定 $t\mod B=0$且 $(t\neq 0)$,其 $t$ 为循环节. 则将 $1$ 式拆开得 $\frac{t\times (B+1 ...
- MVC框架与MTC框架
3.WEB框架 MVC Model View Controller 数据库 模板文件 业务处理 MTV Model Template View 数据库 模板文件 业务处理 ############## ...
- vue打开新窗口
1. <router-link tag="a" target="_blank" :to="{name:'detail',query:{goods ...