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”作为父节点, p和q作为其子节点。 现在,我们需要为p和q中的 a建立数据,以使节点a在查询时可以给出其范围的最大和子间隔。
So for this do we need?
那么我们需要这个吗?
Maximum sum subarray in 'a' can be equal to:
“ a”中的最大和子数组可以等于:
Max subarray in p
p中的最大子数组
Max subarray in q
q中的最大子数组
Elements including both p and q
包含p和q的元素
So for each node we need to store:
因此,对于每个节点,我们需要存储:
Maximum prefix sum
最大前缀和
Maximum suffix sum
最大后缀和
Total Sumtr
总和
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))。
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树范围查找