Codeforces 242 E. XOR on Segment
题目链接:http://codeforces.com/problemset/problem/242/E
线段树要求支持区间求和,区间内每一个数字异或上一个值。
既然是异或,考虑每一个节点维护一个长度为$21*2$的数组${c[x][w][0,1]}$,表示这个节点的子树所包含的线段的点中,这个$2$进制位置上的为$0$和为$1$的分别有多少个。那么一次异或操作其实就是交换了这个位置上的0,1的个数。线段树普通做就可以了。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstdlib>
#include<cmath>
#include<cstring>
using namespace std;
#define maxn 400100
#define llg long long
#define SIZE 22
#define yyj(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
llg n,m,c[maxn][SIZE][],xorv[maxn],cnt,T;
llg ans; inline int getint()
{
int w=,q=; char c=getchar();
while((c<'' || c>'') && c!='-') c=getchar(); if(c=='-') q=,c=getchar();
while (c>='' && c<='') w=w*+c-'', c=getchar(); return q ? -w : w;
} void update(llg o)
{
llg lc=o<<,rc=o<<|;
for (llg i=;i<SIZE;i++) c[o][i][]=c[lc][i][]+c[rc][i][],c[o][i][]=c[lc][i][]+c[rc][i][];
} void setv(llg o,llg val)
{
llg wz=;
while (wz<SIZE)
{
if (val&) c[o][wz][]++; else c[o][wz][]++;
val/=;
wz++;
}
} void xor_(llg o,llg val)
{
llg wz=;
while (val)
{
if (val&) swap(c[o][wz][],c[o][wz][]);
val/=;
wz++;
}
} void pushdown(llg o,llg l,llg r)
{
llg lc=o<<,rc=o<<|;
if (l==r) return ;
if (xorv[o])
{
xor_(lc,xorv[o]); xor_(rc,xorv[o]);
xorv[lc]^=xorv[o]; xorv[rc]^=xorv[o];
xorv[o]=;
}
} void build(llg o,llg l,llg r)
{
if (l==r)
{
llg x=getint();
setv(o,x);
return ;
}
llg lc=o<<,rc=o<<|,mid=(l+r)>>;
build(lc,l,mid); build(rc,mid+,r);
update(o);
} void X(llg o,llg l,llg r,llg L,llg R,llg v)
{
pushdown(o,l,r);
if (l>=L && r<=R)
{
xorv[o]^=v;
xor_(o,v);
return ;
}
llg lc=o<<,rc=o<<|,mid=(l+r)>>;
if (mid>=L) X(lc,l,mid,L,R,v);
if (R>mid) X(rc,mid+,r,L,R,v);
update(o);
} void sum(llg o,llg l,llg r,llg L,llg R)
{
pushdown(o,l,r);
if (l>=L && r<=R)
{
for (llg i=;i<SIZE;i++) ans+=(<<i)*c[o][i][];
return ;
}
llg lc=o<<,rc=o<<|,mid=(l+r)>>;
if (mid>=L) sum(lc,l,mid,L,R);
if (R>mid) sum(rc,mid+,r,L,R);
update(o);
} int main()
{
yyj("seg");
cin>>n;
build(,,n);
cin>>T;
while (T--)
{
ans=;
llg x,y,z,type;
type=getint();
if (type==)
{
x=getint(),y=getint(),ans=;
sum(,,n,x,y);
printf("%lld\n",ans);
}
else
{
x=getint(),y=getint(),z=getint();
X(,,n,x,y,z);
}
}
return ;
}
Codeforces 242 E. XOR on Segment的更多相关文章
- Codeforces 242E:XOR on Segment(位上的线段树)
http://codeforces.com/problemset/problem/242/E 题意:给出初始n个数,还有m个操作,操作一种是区间求和,一种是区间xor x. 思路:昨天比赛出的一道类似 ...
- 【codeforces 242E】XOR on Segment
[原题题面]传送门 [题面翻译]传送门 [解题思路] 操作涉及到区间求和和区间异或,考虑到异或操作,我们对每个数二进制分解. 把每一位单独提出来做,异或要么取反要么变为不变,对于每一位建一颗线段树,那 ...
- codeforces 22E XOR on Segment 线段树
题目链接: http://codeforces.com/problemset/problem/242/E E. XOR on Segment time limit per test 4 seconds ...
- codeforces 242E - XOR on Segment (线段树 按位数建树)
E. XOR on Segment time limit per test 4 seconds memory limit per test 256 megabytes input standard i ...
- CF242E XOR on Segment
CF242E XOR on Segment codeforces 洛谷 关于异或,无法运用懒标记实现区间异或: 可以像trie树一样拆位,将每个值拆成二进制数,对此建相应个数的线段树. 0 1与 0异 ...
- Codeforces Round #149 (Div. 2) E. XOR on Segment (线段树成段更新+二进制)
题目链接:http://codeforces.com/problemset/problem/242/E 给你n个数,m个操作,操作1是查询l到r之间的和,操作2是将l到r之间的每个数xor与x. 这题 ...
- CodeForces 242E - XOR on Segment 二维线段树?
今天练习赛的题....又是线段树的变换..拿到题我就敲了个点更新区间查询的..果断超时...然后想到了可以将每个数与合表示成不进位的二进制数..这样就可以区间进行更新了..比赛的时候写搓了..刚重写了 ...
- codeforces 242E. XOR on Segment 线段树
题目链接 给n个数, 两种操作, 一种是求区间内的数的和, 一种是将区间内的数异或x. 异或x没有什么思路, 单个异或肯定超时, 区间异或也没有办法做....后来才知道可以按位建线段树, 这样建20棵 ...
- CodeForces 242E "XOR on Segment"(线段树)
传送门 •题意 给你一个包含 n 个数的序列 a,定义序列上的两个操作: (1)$1,l,r\ :\ ans=\sum_{i=l}^{r}a_i$; (2)$2,l,r,x\ :\ \forall\ ...
随机推荐
- jquery遍历获取每一行数据进行对比
$("#dtlTable tr:gt(0)").each(function(i){ var orderQtyBy = $("input[name='orderQtyBys ...
- js 显示刚刚上传的图片 (onchange事件)
<table> <tr width="100"> <td>上传商场图片:</td> <td> <input typ ...
- Spring数据库开发
Spring的数据库开发 #Spring中JDBC模板的作用 JDBC模板负责数据库资源管理和错误处理: #熟悉Spring JDBC的配置 配置数据源和jdbc模板 <?xml versio ...
- mybatis之关联映射
###mybatis使用之一对一关联映射 1)分析并画ER图.(特别是一对一.一对多.多对多的情况) 2)启动终端数据库,并建库建表,在表中插入值和字段,并查看结果.(后期把navicat用上) 3) ...
- Caused by: java.net.BindException: Address already in use: bind
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'brandService ...
- K8S学习笔记之Kubernetes核心概念
0x00 Kubernetes简介 Kubernetes(K8S)是Google开源的容器集群管理系统,其设计源于Google在容器编排方面积累的丰富经验,并结合社区创新的最佳实践. K8S在Doc ...
- linux中权限对文件和目录的意义
1.权限对文件的意义: 读:可查看文件的内容 写:可修改文件的内容(但不能删除文件) 执行:可执行文件 2.权限对目录的意义: 读:可以查看目录下的内容,即可以读取该目录下的结构列表 写:可修改目录下 ...
- Springboot项目修改html后不需要重启---springboot项目的热部署
一.spring-boot-devtools 在pom中直接引入依赖 <dependency> <groupId>org.springframework.boot</gr ...
- Java 判断字符串是否为空的四种方法、优缺点与注意事项
以下是Java 判断字符串是否为空的四种方法: 方法一: 最多人使用的一个方法, 直观, 方便, 但效率很低: if(s == null ||"".equals(s));方法二: ...
- Python3 tkinter基础 Label imag显示图片
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...