CodeForces 242E二维线段树
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.
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
一个用二维线段树操作的异或题,建立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二维线段树的更多相关文章
- CodeForces 242E - XOR on Segment 二维线段树?
今天练习赛的题....又是线段树的变换..拿到题我就敲了个点更新区间查询的..果断超时...然后想到了可以将每个数与合表示成不进位的二进制数..这样就可以区间进行更新了..比赛的时候写搓了..刚重写了 ...
- Codeforces 453E - Little Pony and Lord Tirek(二维线段树+ODT)
Codeforces 题目传送门 & 洛谷题目传送门 一道难度 *3100 的 DS,而且被我自己搞出来了! 不过我终究还是技不如人,因为这是一个 \(n\log^2n\) + 大常数的辣鸡做 ...
- codeforces 677D D. Vanya and Treasure(二维线段树)
题目链接: D. Vanya and Treasure time limit per test 1.5 seconds memory limit per test 256 megabytes inpu ...
- 【Codeforces Round #433 (Div. 1) C】Boredom(二维线段树)
[链接]我是链接 [题意] 接上一篇文章 [题解] 接(点我进入)上一篇文章. 这里讲一种用类似二维线段树的方法求矩形区域内点的个数的方法. 我们可以把n个正方形用n棵线段树来维护. 第i棵线段树维护 ...
- UVA 11297 线段树套线段树(二维线段树)
题目大意: 就是在二维的空间内进行单个的修改,或者进行整块矩形区域的最大最小值查询 二维线段树树,要注意的是第一维上不是叶子形成的第二维线段树和叶子形成的第二维线段树要 不同的处理方式,非叶子形成的 ...
- POJ2155 Matrix二维线段树经典题
题目链接 二维树状数组 #include<iostream> #include<math.h> #include<algorithm> #include<st ...
- HDU 1823 Luck and Love(二维线段树)
之前只知道这个东西的大概概念,没具体去写,最近呵呵,今补上. 二维线段树 -- 点更段查 #include <cstdio> #include <cstring> #inclu ...
- poj 2155:Matrix(二维线段树,矩阵取反,好题)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 17880 Accepted: 6709 Descripti ...
- poj 1195:Mobile phones(二维线段树,矩阵求和)
Mobile phones Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 14391 Accepted: 6685 De ...
随机推荐
- Python3调用企业微信用于告警
前段时间利用py爬虫抓取一些网页信息,然后通过wxpy发送到微信群,以用作日常告警,感觉还是很方便. 但好景不长,我的小号微信被腾讯封了(很常见咯), 显示无法登录网页版微信,至今已经有半个多月了. ...
- 如何快速的理解JavaScript闭包?
先看问题 1. 在js中的作用域是什么? 作用域就是作用范围,作用空间.作用域分为全局作用域和局部作用域.(这个东西大家都明白) 如果把局部作用域比作一个国家,那么全局作用域就是地球,地球上除了那个国 ...
- MVC导入命名空间
为什么要导入 一次性导入,避免每个页面都要导入,代码看起来更为清晰,不再带一个长长的命名空间,视图里面可以直接写类名了. 导入方法 在Views文件夹的web.config的namespaces里面配 ...
- 16汇编第十讲完结Call变为函数以及指令的最后讲解
16汇编完结Call变为函数以及指令的最后讲解 学了10天的16位汇编,这一讲就结束了,这里总结一下昨天的LOOP指令的缺陷,因为lOOP指令的缺陷,所以我们都改为下面的汇编代码使用了,自己去写,其中 ...
- .NET Core2.0 MVC中使用EF访问数据
使用环境:Win7+VS2017 一.新建一个.NET Core2.0的MVC项目 二.使用Nuget添加EF的依赖 输入命令:Install-Package Microsoft.EntityFram ...
- Mysql分页处理(PageHelper)
第一次做分页处理(完全不知道分页处理到底要做成什么样?) 理解:其实就是前台页面通过传递不同的参数{1.查询的条件.2.查询页数(pageNum),3每页展现的条数(pageSize)},之后我们(只 ...
- java开发网易电话面试 一面总结
晚上八点多自己在看视频的时候突然接到杭州来的一个电话,当时觉得很奇怪,突兀,接通之后被告知是杭州网易打来的,没有简单的自我介绍,没有多余的废话,直接入主题,吓得我心里怪紧张的,完全没有准备,但是也没有 ...
- 大道至简第一章观后感——java伪代码
一节: public class Yugongyishan_ { //定义一个名为Yugongyishan_的类 Public static void main(string args[]) // ...
- 关于C++中计时的方法
在C++的库函数中,我们可以使用clock()来计算程序的运行时间,主要使用一下三个函数类型及函数: 1.clock_t:数据类型,其实,当你打开time.h就知道了,就是个long型,用来记录一段时 ...
- spring的list ,set,map,properties注入(set,get注入)
①Message.java: package com.hts.entity; import java.io.Serializable; import java.util.List; import ja ...