E. XOR on Segment

You've got an array a, consisting of n integers a1, a2, ..., an. You are allowed to perform two operations on this array:

  1. Calculate the sum of current array elements on the segment [l, r], that is, count value al + al + 1 + ... + ar.
  2. 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.

Input

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.

Output

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 test(s)
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
Output
26 
22
0
34
11
Input
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
Output
38 
28

一个用二维线段树操作的异或题,建立20棵线段数 tree[i]表示这n个数第i为在二进制下的情况 更新的时候将每个数 每个位更新 并用延迟符号 求和的时候按位加起来。

 #include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <math.h>
#include <vector>
#include <stack>
using namespace std;
#define ll long long int
ll a[][];
ll b[];
void build(int l,int r,int t)
{
int i;
if(l==r)
{
int x;
scanf("%d",&x);
i=;
while(x)
{
a[t][i++]=x%;
x/=;
}
return ;
}
int m=(l+r)>>;
build(l,m,t<<);
build(m+,r,t<<|);
for(i=; i<; i++)
a[t][i]=a[t<<][i]+a[t<<|][i];
}
void fun(int l,int r,int t)
{
int z=b[t];
int m=(l+r)>>;
b[t<<]^=b[t];
b[t<<|]^=b[t];
int i=;
while(z)
{
if(z%){
a[t<<][i]=m-l+-a[t<<][i];
a[t<<|][i]=r-m-a[t<<|][i];
}
i++;
z/=;
}
b[t]=;
}
void update(int l,int r,int t,int x,int y,int z)
{
int i;
if(l>=x&&r<=y)
{
i=;
b[t]^=z;
while(z)
{
if(z%)
a[t][i]=r-l+-a[t][i];
i++;
z/=;
}
return ;
}
if(b[t])
fun(l,r,t);
int m=(l+r)>>;
if(x<=m)update(l,m,t<<,x,y,z);
if(y>m)update(m+,r,t<<|,x,y,z);
for(i=; i<; i++)
a[t][i]=a[t<<][i]+a[t<<|][i];
}
ll query(int l,int r,int t,int x,int y)
{
if(l>=x&&r<=y)
{
ll sum=;
for(int i=;i<;i++)
sum+=a[t][i]*(<<i);
return sum;
}
if(b[t])
fun(l,r,t);
int m=(l+r)>>;
ll sum=;
if(x<=m)sum+=query(l,m,t<<,x,y);
if(y>m)sum+=query(m+,r,t<<|,x,y);
for(int i=; i<; i++)
a[t][i]=a[t<<][i]+a[t<<|][i];
return sum;
}
int main()
{
memset(a,,sizeof(a));
memset(b,,sizeof(b));
int n;
cin>>n;
build(,n,);
int m;
cin>>m;
int i,s,x,y,z;
for(i=; i<m; i++)
{
scanf("%d",&s);
if(s==)
{
scanf("%d%d",&x,&y);
cout<<query(,n,,x,y)<<endl;
}
else
{
scanf("%d%d%d",&x,&y,&z);
update(,n,,x,y,z);
}
}
}

CodeForces 242E二维线段树的更多相关文章

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

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

  2. Codeforces 453E - Little Pony and Lord Tirek(二维线段树+ODT)

    Codeforces 题目传送门 & 洛谷题目传送门 一道难度 *3100 的 DS,而且被我自己搞出来了! 不过我终究还是技不如人,因为这是一个 \(n\log^2n\) + 大常数的辣鸡做 ...

  3. codeforces 677D D. Vanya and Treasure(二维线段树)

    题目链接: D. Vanya and Treasure time limit per test 1.5 seconds memory limit per test 256 megabytes inpu ...

  4. 【Codeforces Round #433 (Div. 1) C】Boredom(二维线段树)

    [链接]我是链接 [题意] 接上一篇文章 [题解] 接(点我进入)上一篇文章. 这里讲一种用类似二维线段树的方法求矩形区域内点的个数的方法. 我们可以把n个正方形用n棵线段树来维护. 第i棵线段树维护 ...

  5. UVA 11297 线段树套线段树(二维线段树)

    题目大意: 就是在二维的空间内进行单个的修改,或者进行整块矩形区域的最大最小值查询 二维线段树树,要注意的是第一维上不是叶子形成的第二维线段树和叶子形成的第二维线段树要  不同的处理方式,非叶子形成的 ...

  6. POJ2155 Matrix二维线段树经典题

    题目链接 二维树状数组 #include<iostream> #include<math.h> #include<algorithm> #include<st ...

  7. HDU 1823 Luck and Love(二维线段树)

    之前只知道这个东西的大概概念,没具体去写,最近呵呵,今补上. 二维线段树 -- 点更段查 #include <cstdio> #include <cstring> #inclu ...

  8. poj 2155:Matrix(二维线段树,矩阵取反,好题)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 17880   Accepted: 6709 Descripti ...

  9. poj 1195:Mobile phones(二维线段树,矩阵求和)

    Mobile phones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 14391   Accepted: 6685 De ...

随机推荐

  1. [2014-09-18]Entity Framework 6 预热、启动优化

    好久没写博客了,终于憋出了一个大招,现在总结下. 虽然文章题目是针对EF的,但涉及的内容不仅仅是EF. 场景介绍 目前在做的一个项目,行业门户,项目部分站点按域名划分如下: user.xxx.com: ...

  2. 用PyCharm执行测试成功但无法生成HTMLTestRunner报告

    问题:代码写的没问题,执行也成功了,但就是无法生成HTMLTestRunner的报告 其实这是编辑器搞得鬼,编辑器为了方便用户执行测试,都有一项功能,可以用编辑器来调用unittest或者nose来执 ...

  3. 关于用VMware克隆linux系统后,无法联网找不到eth0网卡的问题

    当使用克隆后的虚拟机时发现系统中的网卡eth0没有了,使用ifconfig -a会发现只有eth1.因为系统是克隆过来的,原有的eth0以及ip地址都是原先网卡的,VMware发现已经被占用,就会创建 ...

  4. 汇编指令-MOV与ldr区别(7)

    MOV 1.可以寄存器与寄存器之间传递数据 2.可以常数传递到寄存器中(常数不能超过32位) LDR 1.可以地址与寄存器之间的数据传递 2.也可以常数传递到寄存器中 实例: 1.r1与r2之间传递就 ...

  5. web程序员标准环境之DreamWeaver【推荐】

    Adobe Dreamweaver,简称"DW",中文名称 "梦想编织者",是美国MACROMEDIA公司开发的集网页制作和管理网站于一身的所见即所得网页编辑器 ...

  6. CCIE-MPLS VPN-实验手册(上卷)

    看完了看完了看完了,豪爽豪爽豪爽,一个月了,写得挺棒.总共14个mpls vpn的实验,为留下学习的痕迹,原封不动献出. CCIE实验手册 (路由部分-MPLSVPN基础篇) [CCIE]  JUST ...

  7. Mac 下如何使用sed -i命令

    今天在学习Linux的过程中发现了sed这一项指令 首先,sed的全称是:Stream Editor 调用sed命令有两种形式: sed [options] 'command' file(s) sed ...

  8. 结对编程1——四则运算-GUI

    码市链接:https://coding.net/u/hmhhh/p/hmh-homework/git/tree/master/ 201421123003 黄建英 201421123004 黄美海 题目 ...

  9. 201521123023《java程序设计》第四周学习总结

    1. 本周学习总结 思维导图 常规: (1)抽象类:不能被直接实例化.只能作为其它类的父类,这一点与final类正好相反.用关键词abstract声明. (2)继承:只能有一个父类,即单继承,子类继承 ...

  10. 201521123045 《Java程序设计》第2周学习总结

    ---恢复内容开始--- #1. 本周学习总结 上课讲解了上次的实验题目,对其中题目的一些问题得到了解决.学会了java数组的使用,对如何使用码云上传代码有了更清晰的理解.pta还是有一些问题没有解决 ...