codeforces 22E XOR on Segment 线段树
题目链接:
http://codeforces.com/problemset/problem/242/E
E. XOR on Segment
time limit per test 4 secondsmemory limit per test 256 megabytes
#### 问题描述
> You've got an array a, consisting of n integers a1, a2, ..., an. You are allowed to perform two operations on this array:
>
> Calculate the sum of current array elements on the segment [l, r], that is, count value al + al + 1 + ... + ar.
> Apply the xor operation with a given number x to each array element on the segment [l, r], that is, execute . This operation changes exactly r - l + 1 array elements.
> Expression means applying bitwise xor operation to numbers x and y. The given operation exists in all modern programming languages, for example in language C++ and Java it is marked as "^", in Pascal — as "xor".
>
> You've got a list of m operations of the indicated type. Your task is to perform all given operations, for each sum query you should print the result you get.
#### 输入
> The first line contains integer n (1 ≤ n ≤ 105) — the size of the array. The second line contains space-separated integers a1, a2, ..., an (0 ≤ ai ≤ 106) — the original array.
>
> The third line contains integer m (1 ≤ m ≤ 5·104) — the number of operations with the array. The i-th of the following m lines first contains an integer ti (1 ≤ ti ≤ 2) — the type of the i-th query. If ti = 1, then this is the query of the sum, if ti = 2, then this is the query to change array elements. If the i-th operation is of type 1, then next follow two integers li, ri (1 ≤ li ≤ ri ≤ n). If the i-th operation is of type 2, then next follow three integers li, ri, xi (1 ≤ li ≤ ri ≤ n, 1 ≤ xi ≤ 106). The numbers on the lines are separated by single spaces.
#### 输出
> For each query of type 1 print in a single line the sum of numbers on the given segment. Print the answers to the queries in the order in which the queries go in the input.
>
> Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams, or the %I64d specifier.
#### 样例
> **sample input**
> 5
> 4 10 3 13 7
> 8
> 1 2 4
> 2 1 3 3
> 1 2 4
> 1 3 3
> 2 2 5 5
> 1 1 5
> 2 1 2 10
> 1 2 3
>
> **sample output**
> 26
> 22
> 0
> 34
> 11
## 题意
> 给你n个数,q个操作:
> 查询:返回l到r的和
> 更新:l到r的每个数都异或上x。
题解
对没个数字按二进制展开,存放在20颗线段树里面。
然后就转化成了一个经典的区间更新区间查询的线段树问题了。
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#define lson (o<<1)
#define rson ((o<<1)|1)
#define M (l+(r-l)/2)
using namespace std;
const int maxn=1e5+10;
const int maxm=22;
typedef __int64 LL;
int n;
int sumv[maxm][maxn<<2];
int setv[maxn<<2];
void maintain(int o) {
for(int i=0; i<maxm; i++) {
sumv[i][o]=sumv[i][lson]+sumv[i][rson];
}
}
void pushdown(int o,int l,int r) {
for(int i=0; i<maxm; i++) {
if(setv[o]&(1<<i)) {
sumv[i][lson]=M-l+1-sumv[i][lson];
sumv[i][rson]=r-M-sumv[i][rson];
}
}
setv[lson]^=setv[o];
setv[rson]^=setv[o];
setv[o]=0;
}
void build(int o,int l,int r) {
if(l==r) {
int x;
scanf("%d",&x);
for(int i=0; i<maxm; i++) {
if(x&(1<<i)) sumv[i][o]=1;
else sumv[i][o]=0;
}
} else {
build(lson,l,M);
build(rson,M+1,r);
maintain(o);
}
}
int ql,qr,_v;
void update(int o,int l,int r) {
if(ql<=l&&r<=qr) {
for(int i=0; i<maxm; i++) {
if(_v&(1<<i)) {
sumv[i][o]=r-l+1-sumv[i][o];
}
}
setv[o]^=_v;
} else {
pushdown(o,l,r);
if(ql<=M) update(lson,l,M);
if(qr>M) update(rson,M+1,r);
maintain(o);
}
}
LL _sum;
void query(int o,int l,int r){
if(ql<=l&&r<=qr){
for(int i=0;i<maxm;i++){
_sum+=sumv[i][o]*((1LL)<<i);
}
}else{
pushdown(o,l,r);
if(ql<=M) query(lson,l,M);
if(qr>M) query(rson,M+1,r);
maintain(o);
}
}
int main() {
scanf("%d",&n);
build(1,1,n);
int q;
scanf("%d",&q);
while(q--) {
int cmd;
scanf("%d",&cmd);
scanf("%d%d",&ql,&qr);
if(cmd==1) {
_sum=0;
query(1,1,n);
printf("%I64d\n",_sum);
} else {
scanf("%d",&_v);
update(1,1,n);
}
}
return 0;
}
Notes
其实和XOR有关的题目大部分都是套路!!!!二进制展开,你会发现一个新的世界!!!!
codeforces 22E XOR on Segment 线段树的更多相关文章
- codeforces 242E. XOR on Segment 线段树
题目链接 给n个数, 两种操作, 一种是求区间内的数的和, 一种是将区间内的数异或x. 异或x没有什么思路, 单个异或肯定超时, 区间异或也没有办法做....后来才知道可以按位建线段树, 这样建20棵 ...
- 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. 这题 ...
- XOR on segment(线段树区间异或更新)
原题传送门 本题大意:给定n个数字和m个操作,操作共有两种,第一种是求解区间l到r上元素的和,第二种是将区间l到r的元素都异或一个x,作为某个位置的新值. 很容易想到线段树维护区间和,但是我们发现,在 ...
- codeforces Good bye 2016 E 线段树维护dp区间合并
codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...
- luogu P2574 XOR的艺术 (线段树)
luogu P2574 XOR的艺术 (线段树) 算是比较简单的线段树. 当区间修改时.\(1 xor 1 = 0,0 xor 1 = 1\)所以就是区间元素个数减去以前的\(1\)的个数就是现在\( ...
- codeforces 242E - XOR on Segment (线段树 按位数建树)
E. XOR on Segment time limit per test 4 seconds memory limit per test 256 megabytes input standard i ...
- CodeForces 242E - XOR on Segment 二维线段树?
今天练习赛的题....又是线段树的变换..拿到题我就敲了个点更新区间查询的..果断超时...然后想到了可以将每个数与合表示成不进位的二进制数..这样就可以区间进行更新了..比赛的时候写搓了..刚重写了 ...
- 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 516C Drazil and Park 线段树
原文链接http://www.cnblogs.com/zhouzhendong/p/8990745.html 题目传送门 - CodeForces 516C 题意 在一个环上,有$n$棵树. 给出每一 ...
随机推荐
- WIN8 下 Hyper-V和Vmware Workstation
1 管理员身份运行命令提示符 cmd bcdedit /copy {current} /d “Windows Without Hyper-V 2 记下 { } 中的代码 bcdedit /set {X ...
- 使用虚拟机在ubuntu下搭建mongoDB开发环境和简单增删改查操作
最近在折腾mongodb和nodejs,在imooc上找了一个mongodb的入门教程,跟着里面一步一步的走,下面记录下我操作的步骤和遇到的问题. 课程地址:http://www.imooc.com/ ...
- 新浪SAE URLRewrite(伪静态、重定向)详解
SAE全称Sina App Engine,真是一个好东西,他有很多优秀的特性,简单来说SAE就是一个简单高效的分布式Web服务开发.运行平台.支持现在常用的 PHP+Mysql 环境,在开发中难免会碰 ...
- ASP.NET MVC4学习笔记之Controller的激活
一. 高层相关类说明 当路由系统根据请求Url收集路由信息后,下一步就要将路由信息传给Controller激活系统,Controller激活系统负责实现了IController接口的Controlle ...
- delphi xe6 打开andoridGPS设置
Androidapi.JNI.JavaTypes, Androidapi.JNI.GraphicsContentViewText, Androidapi.JNI.Location, ...
- SQL Server 锁表、查询被锁表、解锁相关语句
SQL Server 锁表.查询被锁表.解锁相关语句,供参考. --锁表(其它事务不能读.更新.删除) BEGIN TRAN SELECT * FROM <表名> WITH(TABLOCK ...
- jquery mobile最棘手的一个问题
大多数jquery mobile开发的妹子们都碰到过这个问题: 如何调用loading效果 这里给出一段代码,赶紧练手吧. //显示loading function showLoading(){ ...
- stm32f103 SPI单线TX发数据来驱动LCD
有一黑白LCD,有CS/SI/SCK三线,时序满足SPI时序,但STM32的SPI有四线NSS/MOSI/SCK/MISO,这里MISO没有用到.因此可以使用SPI的单线发送模式进行驱动LCD. 关键 ...
- WPF.UIShell UIFramework之自定义窗口的深度技术 - 模态闪动(Blink)、窗口四边拖拽支持(WmNCHitTest)、自定义最大化位置和大小(WmGetMinMaxInfo)
无论是在工作和学习中使用WPF时,我们通常都会接触到CustomControl,今天我们就CustomWindow之后的一些边角技术进行探讨和剖析. 窗口(对话框)模态闪动(Blink) 自定义窗口的 ...
- poj 3259 Wormholes
题目连接 http://poj.org/problem?id=3259 Wormholes Description While exploring his many farms, Farmer Joh ...