题目链接: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. CAT Caterpillar ET Diagnostic Adapter has a powerful function

    As a excellent Professional Diagnostic Tools products, CAT Caterpillar ET Diagnostic Adapter has a p ...

  2. Caddy – 方便够用的 HTTPS server 新手教程

    最近发现了一个 golang 开发的 HTTP server,叫做 Caddy,它配置起来十分简便,甚至可以 28 秒配置好一个支持 http2 的 server ,而且对各种 http 新特性都支持 ...

  3. MyEclipse如何清除废弃的工作空间

    1.MyEclipse如何清除废弃的工作空间Windows--->Preferences--->General--->Startup and Shutdown--->Works ...

  4. selenium 模拟手机

    import time from selenium import webdriver mobileEmulation = {'deviceName': 'Galaxy S5'} options = w ...

  5. Kafka学习笔记之Kafka三款监控工具

    0x00 概述 在之前的博客中,介绍了Kafka Web Console这 个监控工具,在生产环境中使用,运行一段时间后,发现该工具会和Kafka生产者.消费者.ZooKeeper建立大量连接,从而导 ...

  6. bzoj3932 / P3168 [CQOI2015]任务查询系统(主席树+差分)

    P3168 [CQOI2015]任务查询系统 看到第k小,就是主席树辣 对于每一段任务(a,b,k),在版本a的主席树+k,版本b+1的主席树-k 同一时间可能有多次修改,所以开个vector存操作, ...

  7. ldap集成confluence

    confluence ldap配置跟jira ldap集成一样,请参考:https://www.cnblogs.com/imcati/p/9378668.html

  8. 《学习OpenCV3》第14章课后习题

    1.在一条含有 N 个点的封闭轮廓中,我们可以通过比较每个点与其它点的距离,找出最外层的点.(这个翻译有问题,而且这个问题是实际问题) a.这样一个算法的复杂度是多少? b.怎样用更快的速度完成这个任 ...

  9. topcoder srm 694 div1 -3

    1.给出$n$个数字,将其分成三个非空的组,每组的权值为该组所有数字的抑或.选择一种分法使得三组的权值和最大? 思路:记录前两组的权值且三组有没有数字时第三组的值.(当前两组的值知道时第三组的权值是确 ...

  10. CSS的再深入(更新中···)

    在上一章我们提到了一个新的概念,叫做块级样式,讲到这里就要科普一下: 标签又分为两种: (1)块级标签 元素特征:会独占一行,无论内容多少,可以设置宽高··· (2)内敛标签(又叫做行内标签) 元素特 ...