codeforces242E XOR on Segment
本文版权归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的更多相关文章
- 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 242E "XOR on Segment"(线段树)
传送门 •题意 给你一个包含 n 个数的序列 a,定义序列上的两个操作: (1)$1,l,r\ :\ ans=\sum_{i=l}^{r}a_i$; (2)$2,l,r,x\ :\ \forall\ ...
- 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(位上的线段树)
http://codeforces.com/problemset/problem/242/E 题意:给出初始n个数,还有m个操作,操作一种是区间求和,一种是区间xor x. 思路:昨天比赛出的一道类似 ...
- XOR on segment(线段树区间异或更新)
原题传送门 本题大意:给定n个数字和m个操作,操作共有两种,第一种是求解区间l到r上元素的和,第二种是将区间l到r的元素都异或一个x,作为某个位置的新值. 很容易想到线段树维护区间和,但是我们发现,在 ...
随机推荐
- lvs、haproxy、nginx 负载均衡的比较分析(转)
原文:http://blog.csdn.net/gzh0222/article/details/8540604 对软件实现负载均衡的几个软件,小D详细看了一下,从性能和稳定上还是LVS最牛,基本达到了 ...
- 【我的Android进阶之旅】Jenkins挂载slave节点,增强分布式编译的效率
由于公司的Jenkins任务越来越多,而且所有的Android Jenkins任务都在同一台服务器上进行编译,而且该服务器配置Jenkins任务最多3个任务同时运行,所以有时候大家一起编译的时候,只能 ...
- yarn的使用
yarn 的安装 npm install -g yarn yarn -version 查看yarn是否安装成功 一.首先需要了解的命令 npm install === yarn —— install ...
- python 的弹框
import easygui easygui.msgbox("This is a message!", title="simple gui")
- HttpServletRequest获取请求参数中所有的信息
/** * 获取客户端请求参数中所有的信息 * @param request * @return */ private Map<String, String> getAllRequestP ...
- 理解ASM的Extent
理解ASM的Extent 分类: Oracle 2017-04-14 10:19:44 ASM中分配空间的单位是AU,Extent包含1个或多个AU.在11g之前,1个Extent对应1个AU.而 ...
- Java设计原则—开闭原则(转)
原文出自:http://www.cnblogs.com/muzongyan/archive/2010/08/05/1793454.html 开闭原则(Open Closed Principle)是Ja ...
- SqoopFlume、Flume、HDFS之间比较
Sqoop Flume HDFS Sqoop用于从结构化数据源,例如,RDBMS导入数据 Flume 用于移动批量流数据到HDFS HDFS使用 Hadoop 生态系统存储数据的分布式文件系统 Sqo ...
- 通过存储过程创建SQL作业
USE dbNameGO/****** Object: StoredProcedure [dbo].[usp_Createjob] Script Date: 03/26/2014 14:36:30 * ...
- PHP分页及原理
在看本文之前,请确保你已掌握了PHP的一些知识以及MYSQL的查询操作基础哦. 作为一个Web程序,经常要和不计其数的数据打交道,比如会员的数据,文章数据,假如只有几十个会员那很好办,在一页显示就可以 ...