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. Flask01 路由控制(转换器)、反转、请求方法控制

    1 提出问题 如何实现前端传过去的路径时动态的(即:多个url对应一个url视图函数) 例如: 浏览器中输入 http://127.0.0.1:5000/test/good/ 或者 http://12 ...

  2. ADO.NET的学习

    ADO.NET的几个对象 Connection:管理数据库的连接 Command:对数据库执行命令 DataReader:数据流读取器,返回的数据都是快速的且只是"向前"的数据流. ...

  3. [ASP.NET MVC]笔记(四 UnobtruSive AJAX和客户端验证

    UnobtruSive AJAX和客户端验证 ASP.NET MVC 已经默认开启非侵入试js和客户端验证,在web.config可以看到如下配置: <configuration> < ...

  4. POJ 3463 最(次)短路条数

    Sightseeing Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9497   Accepted: 3340 Descr ...

  5. 转: 【Java并发编程】之十三:生产者—消费者模型(含代码)

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/17249321 生产者消费者问题是线程模型中的经典问题:生产者和消费者在同一时间段内共用同一 ...

  6. windows下使用密钥登录Linux及xshell代理转发

    1.密钥登录原理 一般我们使用xshell访问远程主机(Linux主机)时,都是先请管理员给我们开一个账户,即为我们设置一个一个用户名和对应的密码,然后我们就可以使用下面的方式登录到远程主机了: 在这 ...

  7. JavaSE(十)集合之Set

    今天这一篇把之前没有搞懂的TreeSet中的比较搞得非常的清楚,也懂得了它的底层实现.希望博友提意见! 一.Set接口 1.1.Set集合概述 Set集合:它类似于一个罐子,程序可以依次把多个对象 “ ...

  8. JAVA基础第一组(前5道题)

    1.[程序1] 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一 对兔子,假如兔子都不死,问每个月的兔子总数为多少?        1.程序分析: 兔 ...

  9. Java课程设计——象棋(201521123042 姚佳希)

    1. 团队课程设计博客链接 Java课程设计(团队版) 2 个人负责模块或任务说明 ChessBoard类创建棋盘及界面. ChessPoint类创建棋盘格点及界面. ChessPiece类创建棋子及 ...

  10. 语句的分号、逗号和函数中var的使用

    1语句 分号 语句的结尾的分号不是必须的,但建议不要省略它,避免错误. 2逗号 1同时定义多个变量用逗号分开 2数组的最后一位,建议不要加上逗号,在有的浏览器中会导致,在逗号后添加一个undefine ...