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 ...
随机推荐
- kettle表输入条件参数设置
@ 获取系统信息:设置命令参数 @ 表输入:
- selenium python自动化测试 ddt数据驱动
安装ddt pip install ddt 直接上代码: # coding:utf-8 import ddt import time import excelunit import unittest ...
- HAproxy+varnish动静分离部署wordpress
author:JevonWei 版权声明:原创作品 实验背景:将wordpress应用部署在后端服务器上,使用HAProxy做代理服务器,Varnish做缓存服务器,后端有四台web服务器,web1和 ...
- Info模式下的隐形杀手(SpringMVC同时使用<mvc:resources.../>和FormattingConversionServiceFactoryBean时出现的问题)
天气一天比一天变的凉快了,而我一天天踩的坑更加贱了,首先在北京向各位问好,也给身边献身教育事业的亲朋好友们补上一句节日快乐! 今天早上手贱把项目误删了,不得不去SVN上去乞求了.我个人习惯项目运行的时 ...
- RMI,RPC,SOAP对比分析
详见: http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp60 1.RMI 使用java的程序员,对于RMI(Remot ...
- poj1236强连通缩点
题意:给出每个学校的list 代表该学校能链接的其他学校,问1:至少给几个学校资源使所有学校都得到:2:至少加多少个边能让所有学校相互连通: 思路:1:找出缩点后入度为零的点个数 2:找出缩点后入度 ...
- URL.createObjectURL() 与 URL.revokeObjectURL()
.URL.createObjectURL URL.createObjectURL()方法会根据传入的参数创建一个指向该参数对象的URL. 这个URL的生命仅存在于它被创建的这个文档里. 新的对象URL ...
- 201521123001《Java程序设计》第6周学习总结
1. 本周学习总结 1.1 面向对象学习暂告一段落,请使用思维导图,以封装.继承.多态为核心概念画一张思维导图,对面向对象思想进行一个总结. 注1:关键词与内容不求多,但概念之间的联系要清晰,内容覆盖 ...
- 201521123022 《Java程序设计》第三周学习总结
1.本周学习总结 2.书面作业 Q1.代码阅读 public class Test1 { private int i = 1;//这行不能修改 private static int j = 2; pu ...
- Python[小甲鱼007了不起的分支和循环]
加载背景音乐播放背景音乐(设置单曲循环)我方飞机诞生interval = 0while True:if 用户是否点击关闭按钮退出程序breakinterval += 1if interval = 50 ...