b树范围查找

The following question/problem is asked on http://www.spoj.com/problems/GSS1/

在http://www.spoj.com/problems/GSS1/上询问以下问题/问题

Problem:

问题:

A sequence is given: A[1], A[2], ..., A[N] .( |A[i]| ≤ 15007 , 1 ≤ N ≤ 50000 ). Query defined as follows:

给出了一个序列: A [1],A [2],...,A [N](| A [i] |≤15007,1≤N≤50000) 。 查询定义如下:

Query(x,y) = Max { a[i]+a[i+1]+...+a[j] ; x ≤ i ≤ j ≤ y }.

Query(x,y)= Max {a [i] + a [i + 1] + ... + a [j]; x≤i≤j≤y}。

Given M queries, your program must output the results of the queries.

给定M个查询,您的程序必须输出查询结果。

Input

输入项

  • First line will have input N

    第一行将输入N

  • Second line will have N numbers of the sequence

    第二行将有N个序列

  • Third line will input M

    第三行将输入M

  • Fourth line will have those M queries of two numbers

    第四行将有两个数字的M个查询

Output

输出量

Your program must output the results of the M queries, one query per line.

您的程序必须输出M个查询的结果,每行一个查询。

Example:

例:

    Input:
3
-1 2 3
1
1 2
Output:
2

Solution:

解:

In this question, we need to use segment trees. But what data to store in each node, such that it is easy to compute the data associated with a given node If we already know the data associated with its two child nodes.

在这个问题中,我们需要使用段树。 但是要在每个节点中存储什么数据,以便轻松计算与给定节点关联的数据(如果我们已经知道与其两个子节点关联的数据)。

We need to find maximum sum subarray in a given range.

我们需要找到给定范围内的最大和子数组。

Let’s say we have 'a' as a parent node and p and q as its child nodes. Now we need to build data for a from p and q such that node a can give the maximum sum subinterval for its range when queried.

假设我们以“ a”作为父节点, pq作为其子节点。 现在,我们需要为pq中的 a建立数据,以使节点a在查询时可以给出其范围的最大和子间隔。

So for this do we need?

那么我们需要这个吗?

Maximum sum subarray in 'a' can be equal to:

“ a”中的最大和子数组可以等于:

  1. Max subarray in p

    p中的最大子数组

  2. Max subarray in q

    q中的最大子数组

  3. Elements including both p and q

    包含pq的元素

So for each node we need to store:

因此,对于每个节点,我们需要存储:

  1. Maximum prefix sum

    最大前缀和

  2. Maximum suffix sum

    最大后缀和

  3. Total Sumtr

    总和

  4. Best Sum

    最佳总和

Max Suffix sum can be calculated by:

最大后缀总和可以通过以下方式计算:

a.suffix = Max(q.suffix,q.total+p.suffix)

a。后缀=最大值(q.suffix,q.total + p.suffix)

Similarly Max prefix sum can be calculated by:

同样,可以通过以下方式计算最大前缀和:

a.prefix = Max(p.prefix,p.total+q.prefix)

a.prefix = Max(p.prefix,p.total + q.prefix)

Total = p.total + q.total

总数= p。总计+ q。总计

Best Sum: Max(p.suffix+q.prefix,max(p.best,q.best)).

最佳总和:最大值(p.suffix + q.prefix,max(p.best,q.best))。

.minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } } .minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } }

Program:

程序:

#include<bits/stdc++.h>
using namespace std;
#define INT_BITS 32
int maxSubarrayXOR(int set[], int n)
{
// Initialize index of
// chosen elements
int index = 0;
// Traverse through all
// bits of integer
// starting from the most
// significant bit (MSB)
for (int i = INT_BITS-1; i >= 0; i--)
{
// Initialize index of
// maximum element and
// the maximum element
int maxInd = index;
int maxEle = INT_MIN;
for (int j = index; j < n; j++)
{
// If i'th bit of set[j]
// is set and set[j] is
// greater than max so far.
if ( (set[j] & (1 << i)) != 0
&& set[j] > maxEle )
maxEle = set[j], maxInd = j;
}
// If there was no
// element with i'th
// bit set, move to
// smaller i
if (maxEle == INT_MIN)
continue;
// Put maximum element
// with i'th bit set
// at index 'index'
swap(set[index], set[maxInd]);
// Update maxInd and
// increment index
maxInd = index;
// Do XOR of set[maxIndex]
// with all numbers having
// i'th bit as set.
for (int j=0; j<n; j++)
{
// XOR set[maxInd] those
// numbers which have the
// i'th bit set
if (j != maxInd &&
(set[j] & (1 << i)) != 0)
set[j] = set[j] ^ set[maxInd];
}
// Increment index of
// chosen elements
index++;
}
// Final result is
// XOR of all elements
int res = 0;
for (int i = 0; i < n; i++)
res ^= set[i];
return res;
}
struct uni{long parent;
long size;
};
int main(){long N,M;
cin>>N>>M;
uni U[N+1];
long size[N+1];
for(long i =0;i<N;i++){U[i].parent = i;
U[i].size = 1;
}
size[1] = N;
for(long i =0;i<M;i++){long u1,u2;
cin>>u1>>u2;
size[U[u1].size]--;
size[U[u2].size]--;
U[u1].size = U[u1].size + U[u2].size;
U[u2].size = U[u1].size;
size[U[u1].size]++;
cout<<"before loop\n";
while(U[u2].parent!=u2){u2 = U[u2].parent;
}
cout<<"After loop\n";
U[u1].parent = u2;
}
int xorInput[N];
int count = 0;
for(int i =0;i<N;i++){if(size[i]>0){xorInput[count] = i;
count++;
}
}
cout<<maxSubarrayXOR(xorInput,count);
return 0;
}

翻译自: https://www.includehelp.com/data-structure-tutorial/find-maximum-range-of-query-using-segment-trees.aspx

b树范围查找

b树范围查找_使用段树查找最大查询范围相关推荐

  1. python智慧树判断题_知到智慧树_Python程序设计基础_判断题答案

    知到智慧树_Python程序设计基础_判断题答案 更多相关问题 [问答题,简答题] 简述安全门升压试验的方法. [问答题,简答题] 简述热力除氧的基本条件. [问答题,简答题] 简答汽轮机组停机后造成 ...

  2. 实现树状结构_组合模式 - 树状结构的优雅实现

    在编程实践中,经常会遇到树状结构的场景,比如我们的浏览器窗口,比如文件系统. 那么,在处理树状结构的时候有什么较好的方式呢? 现在,我们就来学习一种利用组合模式的方法. 如上图所示,我们通过程序输出的 ...

  3. lookup无序查找_学习LOOKUP 函数实现无序查询

    [释疑]简要地说,从逻辑推理来看: 1 . 首先, 条件是一组逻辑判断的值或逻辑运算得到的由 TRUE 和 FALSE 组成或者 0 与非 0 组成的数组,因而: 1/( 条件 ) 的作用是用于构建一 ...

  4. lookup无序查找_使用LOOKUP函数实现无序查询

    LOOKUP函数有一个经典的条件查找解法,通用公式基本可以写为: LOOKUP(2,1/(条件),查找数组或区域) 或 LOOKUP(1,0/(条件),查找数组或区域) 很多初学者对此感觉非常诧异就, ...

  5. mysql实现二分法查找_算法之二分法查找

    二分法检索(binary search)又称折半检索,二分法检索的基本思想是设字典中的元素从小到大有序地存放在数组(array)中, 首先将给定值key与字典中间位置上元素的关键码(key)比较,如果 ...

  6. python特征匹配 查找_特征匹配+单纯形查找对象

    我尝试使用opencv获取一个查询图像并在一个基本图像中进行匹配.我看了一下在线教程,你看,他们有示例代码来做这件事.所以我复制并粘贴了代码,并尝试用一些试用图像来运行它.下面是代码和一组图像示例.在 ...

  7. 树的结构 数据结构_段树| 数据结构

    树的结构 数据结构 What is a segment tree? 什么是段树? A segment tree is a full binary tree where each node repres ...

  8. b树与b+树的区别_一篇文章理清B树、B-树、B+树、B*树索引之间的区别与联系

    概述 相信对于B树.B-树.B+树.B*树索引这几个大家都很容易混淆,下面单独对这几个索引做下分类总结. B树 即二叉搜索树: 1.所有非叶子结点至多拥有两个儿子(Left和Right): 2.所有结 ...

  9. 二叉树第i层中的所有结点_讲透学烂二叉树(二):图中树的定义amp;各类型树的特征分析...

    日常中我们见到的二叉树应用有,Java集合中的TreeSet和TreeMap,C++ STL中的set.map,以及Linux虚拟内存的管理,以及B-Tree,B+-Tree在文件系统,都是通过红黑树 ...

最新文章

  1. python的类型 变量 数值和字符串
  2. html 运用正则表达式,HTML5 使用正则表达式
  3. 浅谈缓存最终一致性的解决方案
  4. RabbitMQ是如何运转的?
  5. 如何在几分钟内安装Red Hat Container Development Kit(CDK)
  6. 非线性动力学_非线性动力学特辑 低维到高维的联通者
  7. 学python的正确方法_学习Python最正确的步骤(0基础必备)
  8. vue项目安装axios - cmd篇
  9. 年薪 50w+ 的程序员,是这样写代码的?
  10. Js屏蔽键盘输入的某些字符,用以部分代替正则表达式
  11. PHP博客导入导出,Thinkphp5.0导入导出详解
  12. 动态生成圈形+文字的图片
  13. 【翻译】CryEngine3下的Hair Shader
  14. flex实现水平垂直居中
  15. 2021-06-26数组详解
  16. eclipse汉化-设置语言包
  17. 微信小程序码无法解析到scene参数问题
  18. 利用Python中的requests+wget批量下载微信页面上的音频
  19. 利用vlan交换机(网管交换机)打trunk实现单线复用
  20. 陈建文资料介绍:与众不同的交易机遇

热门文章

  1. @SpringBootApplication揭秘
  2. Java并发篇_volatile
  3. web项目上之深入理解Java国际化
  4. Unable to resolve dependency问题解决
  5. HDU 4923 Room and Moor(瞎搞题)
  6. 国家可持续发展议程创新示范区创建工作推进会在北京召开
  7. zhilizhili-ui 2016始动 开始做个样例站吧 (一)
  8. 定位position详解:relative与absolute
  9. eclipse的tomcat运行mave web项目
  10. RDataMining系列:Chapter 4 Decision Trees --决策树实现,未完待续