本文版权归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. Apache mahout 源码阅读笔记--DataModel之FileDataModel

    要做推荐,用户行为数据是基础. 用户行为数据有哪些字段呢? mahout的DataModel支持,用户ID,ItemID是必须的,偏好值(用户对当前Item的评分),时间戳 这四个字段 {@code ...

  2. 如何用 PyCharm 调试 scrapy 项目

    原理: 首先 scrapy 命令其实就是一个python脚本,你可以使用 which scrapy 查看该脚本的内容: from scrapy.cmdline import execute sys.a ...

  3. linux 目录与文件命令

    目录与文件常用命令 1.cd命令 cd [相对路径或绝对路径或特殊符号] 功用:变换目录 ps: 不加参数时,默认切换到用户主目录,即环境变量HOME指定的目录,如root用户的HOME变量为/roo ...

  4. Uboot命令U_BOOT_CMD

    转载:http://blog.csdn.net/shengzhadon/article/details/52766263 U_BOOT_CMD是一个宏定义,具体功能是定义一个struct cmd_tb ...

  5. Spring框架第五篇之Spring与AOP

    一.AOP概述 AOP(Aspect Orient Programming),面向切面编程,是面向对象编程OOP的一种补充.面向对象编程是从静态角度考虑程序的结构,而面向切面编程是从动态角度考虑程序运 ...

  6. 多个JS文件性能优化

    页面中引入的JS文件是阻塞式加载的,这样会影响页面性能.以下是JS文件性能优化方法: 一:将所有的<script>标签放到页面底部,也就是</body>闭合标签之前,这能确保在 ...

  7. MySQL · 引擎特性 · InnoDB redo log漫游(转)

    前言 InnoDB 有两块非常重要的日志,一个是undo log,另外一个是redo log,前者用来保证事务的原子性以及InnoDB的MVCC,后者用来保证事务的持久性. 和大多数关系型数据库一样, ...

  8. Creating an AVI in memory with C++

    Creating an AVI in memory with C++        The following example demonstrates how an avi file is comp ...

  9. windows安装redis, php5.5

    全套安装包地址 http://download.csdn.net/detail/whellote/9572797   解压 redis-2.2.5-win32-win64, 将里面的内容拷贝到j:/r ...

  10. windows安装oracle client 18c 和plsql工具

    安装须知: (1)安装平台选择.linux/windows (2)软件位数选择.32/64,如果你的plsql工具是32位,那么你就安装32位客户端,如果是64位,你就安装64位客户端. 安装过程: ...