codeforces 242E XOR on Segment
4 seconds
256 megabytes
standard input
standard output
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, coutstreams, or the %I64d specifier.
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
26 22 0 34 11
6 4 7 4 0 7 3 5 2 2 3 8 1 1 5 2 3 5 1 2 4 5 6 1 2 3
38 28
#include<cstdio>
using namespace std;
const int maxn = 100000+100;
typedef long long LL;
int a[maxn];
int T[22][maxn<<2];
bool lazy[22][maxn<<2];
void build(int k,int id,int l,int r){if(l==r){ T[k][id]=(a[l]>>k)&1; return ;}int m=(l+r)>>1;build(k,id<<1,l,m); build(k,id<<1|1,m+1,r);T[k][id]=T[k][id<<1]+T[k][id<<1|1];
}
void update(int k,int id,int l,int r,int L,int R){if(L==l&&R==r){T[k][id]=r-l+1-T[k][id];lazy[k][id]^=1;return ;}int m=(L+R)>>1;if(m>=r) update(k,id<<1,l,r,L,m);else if(l>m) update(k,id<<1|1,l,r,m+1,R);else{update(k,id<<1,l,m,L,m);update(k,id<<1|1,m+1,r,m+1,R);}T[k][id]=T[k][id<<1]+T[k][id<<1|1];if(lazy[k][id]) T[k][id]=R-L+1-T[k][id];
}
LL query(int k,int id,int l,int r,int L,int R){if(L==l&&R==r){return T[k][id];}int m=(L+R)>>1,s;if(m>=r) s=query(k,id<<1,l,r,L,m);else if(l>m) s=query(k,id<<1|1,l,r,m+1,R);else{s=query(k,id<<1,l,m,L,m)+query(k,id<<1|1,m+1,r,m+1,R);}if(lazy[k][id]) return r-l+1-s;else return s;
}
int main(){int n,m;scanf("%d",&n);for(int i=1;i<=n;i++) scanf("%d",&a[i]);for(int i=0;i<20;i++) build(i,1,1,n);scanf("%d",&m);int id,l,r,x;while(m--){scanf("%d",&id);if(id==1){scanf("%d%d",&l,&r);long long sum=0;for(int i=0;i<20;i++) sum+=query(i,1,l,r,1,n)<<i;printf("%I64d\n",sum);}else{scanf("%d%d%d",&l,&r,&x);for(int i=0;i<20;i++){if(x>>i&1)update(i,1,l,r,1,n);}}}return 0;}
codeforces 242E XOR on Segment相关推荐
- CF 242E XOR on Segment 【线段树】
两种操作: 1.求区间和 2.对区间上的每一个数进行异或(xor)运算 直接维护区间和的话区间更新无法进行,所以,要维护的信息是区间内按位和(即每个二进制位出现的次数),那么进行xor运算的时候,只需 ...
- CodeForces - 616D Longest k-Good Segment
CodeForces - 616D Longest k-Good Segment 题意: 有包含n个数的序列a,求能找到最长的区间包含不超过k个不同的元素. 题解: 尺取法,先固定L,然后移动R,R每 ...
- 【codeforces 242E】XOR on Segment
[原题题面]传送门 [题面翻译]传送门 [解题思路] 操作涉及到区间求和和区间异或,考虑到异或操作,我们对每个数二进制分解. 把每一位单独提出来做,异或要么取反要么变为不变,对于每一位建一颗线段树,那 ...
- Codeforces 617E XOR and Favorite Number
Discription Bob has a favorite number k and ai of length n. Now he asks you to answer m queries. Eac ...
- CodeForces - 617E XOR and Favorite Number (莫队+前缀和)
Bob has a favorite number k and ai of length n. Now he asks you to answer m queries. Each query is g ...
- codeforces B. The Fibonacci Segment 解题报告
题目链接:http://codeforces.com/problemset/problem/365/B 题目意思:简单来说,就是要找出最长的斐波纳契长度. 解决的方法不难,但是要注意更新左区间和右区间 ...
- codeforces 617E XOR and Favorite Number 莫队
https://vjudge.net/problem/CodeForces-617E 题目大意:给nnn个数,mmm个询问,以及一个数kkk,每次询问要输出[l,r][l,r][l,r]内满足a[i] ...
- CodeForces -617E XOR and Favorite Number(莫队)
题目链接:点击这里 题目大意: 给定一个长度为 nnn 的序列 a1,a2,...,ana_1,a_2,...,a_na1,a2,...,an ,再给出一个数字 kkk , mmm 组询问每组询 ...
- CodeForces - 1417E XOR Inverse(字典树求逆序对+分治)
题目链接:点击查看 题目大意:给出一个长度为 n 的数列 a,现在要求选出一个 x,将 a 中的每个元素都异或之后得到一个新的数列 b,要求数列 b 的逆序对最小,问最小的逆序对是多少,x 该如何选择 ...
最新文章
- Pycharm报错合集:在pycharm运行anaconda配置的Pytorch环境报错(Environment location diretory is not empty )
- 使用who.is查域名DNS信息以及用sameip.org查其他网站
- 千万不要一辈子靠技术生存
- 【集合论】卡氏积 ( 卡氏积概念 | 卡氏积示例 | 卡氏积性质 | 非交换性 | 非结合性 | 分配律 | 有序对为空 | n 维卡氏积 | n 维卡氏积个数 | n维卡氏积性质 )
- C 库函数 - vprintf()
- Linux命令之 DNF -- 新一代的 RPM 软件包管理器
- java面经_作为Java后台,这些都不会的话,就别去面试了
- 高质量C /C编程指南---序言
- angular1.x todolist 实现
- 银行软件业务开发分类杂谈-多年前的旧文
- Sublime Text 3安装、配置和快捷键
- 拓端tecdat|R和Python机器学习:广义线性回归glm,样条glm,梯度增强,随机森林和深度学习模型分析
- 190204每日一句
- Luarocks 安装艰难过程
- 【数据结构与算法分析】——java语言描述(1)
- 南京互联网IT公司推荐
- 你的程序要读入一系列正整数数据,输入-1表示输入结束,-1本身不是输入的数据,程序输出读到的数据中的奇数和偶数的个数。(Java经典编程案例)
- python中else是指什么意思_python中elif什么意思?
- 表格table中的td标签中的内容太多,影响整体外观,不美观。将一部分内容隐藏起来,用省略号代替
- 社会责任·价值共创,中关村网络安全与信息化产业联盟对话网信企业家海泰方圆董事长姜海舟