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\ ...
随机推荐
- C++ 开源库列表
https://zh.cppreference.com/w/cpp/links/libs
- BATJ等大厂最全经典面试题分享
金九银十,又到了面试求职高峰期,最近有很多网友都在求大厂面试题.正好我之前电脑里面有这方面的整理,于是就发上来分享给大家. 这些题目是网友去百度.蚂蚁金服.小米.乐视.美团.58.猎豹.360.新浪. ...
- printf("loops %u / %u%c[K\n", loops + 1, opts->loops, 27); printf("%cM", 27);
serialcheck.c中的一段代码一直弄不明白: do { status = stress_test_uart_once(opts, fd, data, data_len); memset(opt ...
- Solr基本操作
/update 使用/update进行索引维护,进入Solr管理界面SolrCore下的Document下: 我们进行更新操作可以用json和xml多种格式,这里以xml格式为例说明.先来看看界面上的 ...
- End of script output before headers错误解决方法
注意是否丢失两个换行符\n\n printf("Content-type: text/html;charset=utf-8\n\n");
- linux交换区使用过多导致的性能问题
近日,我们开发发现有一台配置相同的服务器跑的特别慢,相同数据量的情况下,其他服务器只要跑10分钟,这台服务器要跑50分钟,经确认,所有的应用层配置参数都相同.上去之后,发现该服务器swap使用比较多, ...
- plsql developer 10注册码----亲测截止2017年5月6可用
亲测截止2017年5月6可用 Product Code:4t46t6vydkvsxekkvf3fjnpzy5wbuhphqzserial Number:601769password:xs374ca
- vim 快捷键(update)
网上盗的图,233 2018年08月31日14:31:19 update: 批量行操作 esc->ctrl+v->选中批量行->shift+i->operation-> ...
- centos-6.5安装部署LNMP环境
安装部署前,确保安装了gcc和gcc-c++ 系统信息: [root@zww ~]# cat /etc/redhat-release CentOS release 6.5 (Final) [root@ ...
- ldap集成grafana
grafana版本: 5.0.3 grafana通过k8s方式安装,所以需将配置文件挂载过去. cat grafana-configmap.yaml apiVersion: v1 kind: Conf ...