本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作。

本文作者:ljh2000
作者博客:http://www.cnblogs.com/ljh2000-jump/
转载请注明出处,侵权必究,保留最终解释权!

题目链接:codeforces242E

正解:线段树

解题报告:

  很快想出来之后写完就交,然后1A辣!

  考虑这种和异或这类的位运算有关的题目,显然拆成位来考虑方便的多,需要资瓷区间修改、区间查询,那么就用线段树好了。

  线段树上维护什么呢?

  考虑把区间异或上$x$,如果$x$的第$i$位为$1$,相当于是把区间内所有的数的第$i$位从$1$变$0$,从$0$变$1$,显然我在线段树上维护区间内每一位的$1$、$0$出现次数就好了,修改的时候打个标记,把$0$、$1$的出现次数$swap$一下,查询的时候扫一遍算贡献。

//It is made by ljh2000
//有志者,事竟成,破釜沉舟,百二秦关终属楚;苦心人,天不负,卧薪尝胆,三千越甲可吞吴。
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <complex>
#include <bitset>
using namespace std;
typedef long long LL;
typedef long double LB;
typedef complex<double> C;
const double pi = acos(-1);
const int MAXN = 100011;
int n,m,ql,qr,mo[45],CC,lin;
LL ans;
struct node{
int tag;
int s[21][2];
}a[MAXN*3]; inline int getint(){
int w=0,q=0; char c=getchar(); while((c<'0'||c>'9') && c!='-') c=getchar();
if(c=='-') q=1,c=getchar(); while (c>='0'&&c<='9') w=w*10+c-'0',c=getchar(); return q?-w:w;
} inline void update(int root){
int lc=root<<1,rc=root<<1|1;
for(int i=20;i>=0;i--)
for(int j=0;j<2;j++)
a[root].s[i][j]=a[lc].s[i][j]+a[rc].s[i][j];
} inline void build(int root,int l,int r){
if(l==r) {
lin=getint();
for(int i=20;i>=0;i--) a[root].s[i][(lin>>i)&1]++;
return ;
}
int mid=(l+r)>>1,lc=root<<1,rc=root<<1|1;
build(lc,l,mid); build(rc,mid+1,r);
update(root);
} inline void pushdown(int root,int l,int r){
if(a[root].tag==0) return ; if(l==r) { a[root].tag=0; return ; }
lin=a[root].tag; a[root].tag=0; int lc=root<<1,rc=root<<1|1;
a[lc].tag^=lin; a[rc].tag^=lin;
for(int i=0;i<=20;i++)
if((lin>>i)&1) {
swap(a[lc].s[i][0],a[lc].s[i][1]);
swap(a[rc].s[i][0],a[rc].s[i][1]);
}
} inline void query(int root,int l,int r){
pushdown(root,l,r);
if(ql<=l && r<=qr) {
for(int i=0;i<=20;i++)
ans+=1LL*mo[i]*a[root].s[i][1];
return ;
}
int mid=(l+r)>>1,lc=root<<1,rc=root<<1|1;
if(ql<=mid) query(lc,l,mid); if(qr>mid) query(rc,mid+1,r);
} inline void modify(int root,int l,int r){
pushdown(root,l,r);
if(ql<=l && r<=qr) {
a[root].tag^=CC;
for(int i=0;i<=20;i++)
if((CC>>i)&1)
swap(a[root].s[i][0],a[root].s[i][1]);
return ;
}
int mid=(l+r)>>1,lc=root<<1,rc=root<<1|1;
if(ql<=mid) modify(lc,l,mid); if(qr>mid) modify(rc,mid+1,r);
update(root);
} inline void work(){
n=getint(); build(1,1,n);
for(int i=0;i<21;i++) mo[i]=(1<<i);
m=getint(); int type;
while(m--) {
type=getint(); ql=getint(); qr=getint();
if(type==1) {
ans=0;
query(1,1,n);
printf("%I64d\n",ans);
}
else {
CC=getint();
modify(1,1,n);
}
}
} int main()
{
work();
return 0;
}
//有志者,事竟成,破釜沉舟,百二秦关终属楚;苦心人,天不负,卧薪尝胆,三千越甲可吞吴。

  

codeforces242E XOR on Segment的更多相关文章

  1. codeforces 22E XOR on Segment 线段树

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

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

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

  3. CF242E XOR on Segment

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

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

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

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

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

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

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

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

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

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

  9. XOR on segment(线段树区间异或更新)

    原题传送门 本题大意:给定n个数字和m个操作,操作共有两种,第一种是求解区间l到r上元素的和,第二种是将区间l到r的元素都异或一个x,作为某个位置的新值. 很容易想到线段树维护区间和,但是我们发现,在 ...

随机推荐

  1. Fang Fang---hud5455(字符串处理)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5455 就是求字符串中含有几个f[i], 输出最小的: 例如fff应该是2,有f[0]和f[1]组成的; ...

  2. PHP的生命周期

    了解PHP生命周期之前,先了解一下apache是怎么和php关联起来的吧~ 1.Apache运行机制剖析 ----------------------------- 总体示意图如下:   Apache ...

  3. JAVA math包

    Math类: java.lang.Math 类中包含基本的数字操作,如指数.对数.平方根和三角函数. java.math是一个包,提供用于执行任意精度整数(BigInteger)算法和任意精度小数(B ...

  4. PAT 1093 Count PAT's[比较]

    1093 Count PAT's (25 分) The string APPAPT contains two PAT's as substrings. The first one is formed ...

  5. Linux界面交互与目录结构

    一.交互通道 Linux系统环境默认有六个命令交互通道和一个图形界面交互通道,默认进入的是图形界面通道. 命令交互模式切换:ctrl+alt+F1-F6 图形界面交互模式:ctrl+alt+F7   ...

  6. python全栈开发从入门到放弃之装饰器函数

    什么是装饰器#1 开放封闭原则:对扩展是开放的,对修改是封闭的#2 装饰器本身可以是任意可调用对象,被装饰的对象也可以是任意可调用对象#3 目的:''' 在遵循 1. 不修改被装饰对象的源代码 2. ...

  7. 404 Not Found 探秘Nginx转发处理流程

    一.问题描述 访问一个链接地址后报404 Not Found nginx/1.10.2 1 112.95.211.154 - - [08/Mar/2018:15:22:21 +0800] " ...

  8. 【源码分享】仿网易客户端源码效果 apkbus的~

    http://www.apkbus.com/forum.php?mod=viewthread&tid=184867 内容我就不说了,直接点开看吧.

  9. Winter-2-STL-B Brackets 解题报告及测试数据

    Time Limit:2000MS     Memory Limit:65536KB Description Given a string consisting of brackets of two ...

  10. Docker+.Net Core 的那些事儿-4.还有这种操作!?

    1.通过docker run -v命令映射工作目录 通过一系列上述操作,我们可以发现我们的发布是基于镜像的,也就是说,在后期的迭代过程中,如果有些代码修改,我们就不得不删除旧的容器和镜像,dotnet ...