b树范围查找_使用段树查找最大查询范围
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個查詢,您的程序必須輸出查詢結(jié)果。
Input
輸入項(xiàng)
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
第四行將有兩個數(shù)字的M個查詢
Output
輸出量
Your program must output the results of the M queries, one query per line.
您的程序必須輸出M個查詢的結(jié)果,每行一個查詢。
Example:
例:
Input:3 -1 2 311 2Output:2Solution:
解:
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.
在這個問題中,我們需要使用段樹。 但是要在每個節(jié)點(diǎn)中存儲什么數(shù)據(jù),以便輕松計(jì)算與給定節(jié)點(diǎn)關(guān)聯(lián)的數(shù)據(jù)(如果我們已經(jīng)知道與其兩個子節(jié)點(diǎn)關(guān)聯(lián)的數(shù)據(jù))。
We need to find maximum sum subarray in a given range.
我們需要找到給定范圍內(nèi)的最大和子數(shù)組。
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.
假設(shè)我們以“ a”作為父節(jié)點(diǎn), p和q作為其子節(jié)點(diǎn)。 現(xiàn)在,我們需要為p和q中的 a建立數(shù)據(jù),以使節(jié)點(diǎn)a在查詢時可以給出其范圍的最大和子間隔。
So for this do we need?
那么我們需要這個嗎?
Maximum sum subarray in 'a' can be equal to:
“ a”中的最大和子數(shù)組可以等于:
Max subarray in p
p中的最大子數(shù)組
Max subarray in q
q中的最大子數(shù)組
Elements including both p and q
包含p和q的元素
So for each node we need to store:
因此,對于每個節(jié)點(diǎn),我們需要存儲:
Maximum prefix sum
最大前綴和
Maximum suffix sum
最大后綴和
Total Sumtr
總和
Best Sum
最佳總和
Max Suffix sum can be calculated by:
最大后綴總和可以通過以下方式計(jì)算:
a.suffix = Max(q.suffix,q.total+p.suffix)
a。后綴=最大值(q.suffix,q.total + p.suffix)
Similarly Max prefix sum can be calculated by:
同樣,可以通過以下方式計(jì)算最大前綴和:
a.prefix = Max(p.prefix,p.total+q.prefix)
a.prefix = Max(p.prefix,p.total + q.prefix)
Total = p.total + q.total
總數(shù)= p。總計(jì)+ q。總計(jì)
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 32int 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樹范圍查找
總結(jié)
以上是生活随笔為你收集整理的b树范围查找_使用段树查找最大查询范围的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ai怎么约束每个字的大小_人工智能的约束
- 下一篇: 单位矩阵属性(I ^ k = I)| 使