日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

b树范围查找_使用段树查找最大查询范围

發布時間:2025/3/11 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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個查詢,您的程序必須輸出查詢結果。

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 311 2Output: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??傆?#43; 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 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樹范圍查找

    總結

    以上是生活随笔為你收集整理的b树范围查找_使用段树查找最大查询范围的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。