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-02-19]如何移除响应头中的.net framework 版本信息 以及mvc版本信息?

    先来看一个简单mvc3网站的响应头 修改Global.asax文件 在Application_Start方法中添加如下代码 MvcHandler.DisableMvcResponseHeader = ...

  2. 计算机程序的思维逻辑 (95) - Java 8的日期和时间API

    ​本节继续探讨Java 8的新特性,主要是介绍Java 8对日期和时间API的增强,关于日期和时间,我们在之前已经介绍过两节了,32节介绍了Java 1.8以前的日期和时间API,主要的类是Date和 ...

  3. 四张图揭秘中国AI人才现状

    本文数据来源:领英<全球AI领域人才报告> 最近有非常多的同学看了之前我们的一些文章和直播之后,多对AI领域跃跃欲试,本文我们结合一份人才报告(我个人感觉这份报告还是比较靠谱的),为大家揭 ...

  4. informatica9 安装下载,安装教程 介质(文章和视频教程)(csdn讲师:Array)

    Informatica学习:  参考文献:视频参考地址:http://edu.csdn.net/course/detail/5034,ETL之informatica9通关班(第二期) 1.安装介质的获 ...

  5. Nginx keepalived实现高可用负载均衡详细配置步骤

    Keepalived是一个免费开源的,用C编写的类似于layer3, 4 & 7交换机制软件,具备我们平时说的第3层.第4层和第7层交换机的功能.主要提供loadbalancing(负载均衡) ...

  6. webmagic爬取渲染网站

    最近突然得知之后的工作有很多数据采集的任务,有朋友推荐webmagic这个项目,就上手玩了下.发现这个爬虫项目还是挺好用,爬取静态网站几乎不用自己写什么代码(当然是小型爬虫了~~|). 好了,废话少说 ...

  7. [js高手之路]深入浅出webpack教程系列9-打包图片(file-loader)用法

    我们还是接着上文继续,本文我们要讲的是图片资源的打包,图片在静态排版中,经常出现的两个位置是css通过background引入背景,还有一种就是在html模板文件中用img标签引入的方式,如果要在we ...

  8. 数据库学习任务二:数据库连接对象SqlConnection

    数据库应用程序的开发流程一般主要分为以下几个步骤: 创建数据库 使用Connection对象连接数据库 使用Command对象对数据源执行SQL命令并返回数据 使用DataReader和DataSet ...

  9. python __name__ 变量的含义

    python __name__ 变量的含义 if __name__ == '__main__': tf.app.run() 当python读入程序时,会初始化一些系统变量.如果当前程序是主程序,__n ...

  10. CSS3动画效果之transition

    CSS3中有两种方式实现动画,transition和animation+@keyframe. 两者的作用机制不一样:transition定义在可能要进行动画的元素上,对某些CSS属性进行监听,一旦CS ...