题目链接: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的更多相关文章

  1. Codeforces 242E:XOR on Segment(位上的线段树)

    http://codeforces.com/problemset/problem/242/E 题意:给出初始n个数,还有m个操作,操作一种是区间求和,一种是区间xor x. 思路:昨天比赛出的一道类似 ...

  2. 【codeforces 242E】XOR on Segment

    [原题题面]传送门 [题面翻译]传送门 [解题思路] 操作涉及到区间求和和区间异或,考虑到异或操作,我们对每个数二进制分解. 把每一位单独提出来做,异或要么取反要么变为不变,对于每一位建一颗线段树,那 ...

  3. codeforces 22E XOR on Segment 线段树

    题目链接: http://codeforces.com/problemset/problem/242/E E. XOR on Segment time limit per test 4 seconds ...

  4. codeforces 242E - XOR on Segment (线段树 按位数建树)

    E. XOR on Segment time limit per test 4 seconds memory limit per test 256 megabytes input standard i ...

  5. CF242E XOR on Segment

    CF242E XOR on Segment codeforces 洛谷 关于异或,无法运用懒标记实现区间异或: 可以像trie树一样拆位,将每个值拆成二进制数,对此建相应个数的线段树. 0 1与 0异 ...

  6. 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. 这题 ...

  7. CodeForces 242E - XOR on Segment 二维线段树?

    今天练习赛的题....又是线段树的变换..拿到题我就敲了个点更新区间查询的..果断超时...然后想到了可以将每个数与合表示成不进位的二进制数..这样就可以区间进行更新了..比赛的时候写搓了..刚重写了 ...

  8. codeforces 242E. XOR on Segment 线段树

    题目链接 给n个数, 两种操作, 一种是求区间内的数的和, 一种是将区间内的数异或x. 异或x没有什么思路, 单个异或肯定超时, 区间异或也没有办法做....后来才知道可以按位建线段树, 这样建20棵 ...

  9. CodeForces 242E "XOR on Segment"(线段树)

    传送门 •题意 给你一个包含 n 个数的序列 a,定义序列上的两个操作: (1)$1,l,r\ :\ ans=\sum_{i=l}^{r}a_i$; (2)$2,l,r,x\ :\ \forall\ ...

随机推荐

  1. linux mysql主从复制

    centos7 安装 mariadb 1 yum 源  -- 配置阿里的 2 rmp 方式 3 源码编译方式  -- 专业DBA 一些知识点: 虚拟环境 不影响 redis/ mariadb/mysq ...

  2. FAQ About WOYO PDR007 Dent Removal Heat Induction System

    WOYO PDR 007 is a dent repair tool for auto maintence. WOYO PDR007 Auto Body Paintless Dent Repair K ...

  3. 龙珠超·布罗利【MGRT&幻之】【720P】剧场版

    [上传]龙珠超·布罗利[MGRT&幻之][720P]剧场版 这是一个,全新的“赛亚人”故事.“力量大会”之后,和平的地球.悟空了解到宇宙中还存在着自己未曾见过的强者,于是每天都为了变得更强而不 ...

  4. 原生Ajax和jqueryAjax写法

    原生写法: $('#send').click(function(){ //请求的5个阶段,对应readyState的值 //0: 未初始化,send方法未调用: //1: 正在发送请求,send方法已 ...

  5. Web开发相关笔记 #04# WebSocket

    本文的主要内容: HTTP VS. WebSocket WebSocket 的客户端实现(JavaScript) WebSocket 的服务端实现(Java & apache WebSocke ...

  6. Java学习笔记之linux配置java环境变量(三种环境变量)

    0x00 压安装jdk 在shell终端下进入jdk-6u14-linux-i586.bin文件所在目录, 执行命令 ./jdk-6u14-linux-i586.bin 这时会出现一段协议,连继敲回车 ...

  7. yii2 mysql根据多个字段的数据计算排序

    mysql根据多个字段的数据计算排序 select *,num1+num2*10+num3*100 num from $tableName order by num desc yii2框架活动记录ac ...

  8. mysql判断两个时间段是否有交集

    //判断两个时间段是否有交集 private function checkTimeCross($start_time,$end_time){ $sql ) AND ((start_time > ...

  9. js通过 URL下载文件

    页面上一个button,点击之后触发一个function去请求数据,返回 pdf/epub 的URL,然后下载这个文件. 本来是直接用 a 写的,href里放资源地址,target设为'_blank' ...

  10. php mysqli 的使用方法

    原文链接:https://blog.csdn.net/solly793755670/article/details/52217456 Mysqli是php5之后才有的功能 需要修改php.ini的配置 ...