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 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))。

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树范围查找

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/545881.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

两难!先更新数据库再删缓存?还是先删缓存再更新数据库?

前言当我们在做数据库与缓存数据同步时&#xff0c;究竟更新缓存&#xff0c;还是删除缓存&#xff0c;究竟是先操作数据库&#xff0c;还是先操作缓存&#xff1f;本文带大家深度分析数据库与缓存的双写问题&#xff0c;并且给出了所有方案的实现代码方便大家参考。本篇文章主…

c++ list sort

1. bool operator < (S & b) {return ID < b.ID;} struct S {std::string firstname;std::string secondname;int ID;bool operator < (S & b) {return ID < b.ID;}// 重新定义小于&#xff0c;因为默认的sort函数调用的操作符是<&#xff0c;所以我…

负数在计算机中如何表示?

取一个负数如-5&#xff0c;其原码就是其绝对值5的原码&#xff0c;但最高位为1&#xff0c;&#xff08;负数最高位为1&#xff0c;正数最高位为0&#xff09;&#xff0c;其反码就是对原码取反&#xff08;最高位不参与取反&#xff09;&#xff0c;补码就是对反码1&#xff…

String中删除空格的7种方法!

字符串&#xff0c;是Java中最常用的一个数据类型了。我们在日常开发时候会经常使用字符串做很多的操作。比如字符串的拼接、截断、替换等。本文我们介绍一个比较常见又容易被忽略的一个操作&#xff0c;那就是移除字符串中的空格。其实&#xff0c;在Java中从字符串中删除空格…

pictureBox1.Image的获得图片路径的三种方法

1.绝对路径: this.pictureBox2.ImageImage.FromFile("D://1.jpg"); 2.相对路径: Application.StartupPath; 可以得到程序根目录 this.pictureBox2.ImageImage.FromFile(Application.StartupPath "//1.gif"); 3.获得网络图片的路径 this.pictureBox2.Imag…

借助datetime和pyttsx3在Python中创建闹钟

Modules used: 使用的模块&#xff1a; To create this script, we will use 3 modules: 要创建此脚本&#xff0c;我们将使用3个模块&#xff1a; datetime 约会时间 time 时间 pyttsx3 pyttsx3 datetime module: datetime is an inbuilt python module which will help us …

ie6 javascript js 缺少标识符总结(转载)

转载http://blog.csdn.net/qingyundys/article/details/6218280 ie6 javascript js 缺少标识符总结1. ie6下&#xff0c;javascript代码中&#xff0c;不能出现保留字符。解决办法就是避免直接使用JS的保留字符。2. IE和Firefox不兼容的地方.属性之间是要用","分隔的…

Unsigned 陷阱

unsigned是整形的一种类型&#xff0c;表示无符号&#xff0c;一般用于unsigned int和unsigned char&#xff0c;如果没有理解unsigned的意义将会在做题中掉入陷阱&#xff0c;下面通过介绍几个例子来说明&#xff1a; 1、 int main() {unsigned int i;for (i 10;i > 0;…

URL 去重的 6 种方案!(附详细代码)

作者 | 王磊来源 | Java中文社群&#xff08;ID&#xff1a;javacn666&#xff09;转载请联系授权&#xff08;微信ID&#xff1a;GG_Stone&#xff09;URL 去重在我们日常工作中和面试中很常遇到&#xff0c;比如这些&#xff1a;可以看出&#xff0c;包括阿里&#xff0c;网易…

深入了解INF

大家都知道&#xff0c;在“我的电脑”里有数也数不清的INF文件&#xff0c;但是却很少有人知道&#xff0c;INF文件是干什么用的。充其量&#xff0c;也仅仅停留在INF文件能够解开锁定的注册表这一感性的认识上&#xff0c;那么到底什么是INF文件&#xff0c;INF文件又能干些什…

Java字符串indexOf(int ch,int fromIndex)方法,带示例

字符串indexOf(int ch&#xff0c;int fromIndex)方法 (String indexOf(int ch, int fromIndex) Method) indexOf(int ch, int fromIndex) is a String method in Java and it is used to get the index of a specified character in the string from given fromIndex. That me…

阿里巴巴为什么禁止使用Apache Beanutils进行属性复制?

作者 l Hollis来源 l Hollis&#xff08;ID&#xff1a;hollischuang&#xff09;在日常开发中&#xff0c;我们经常需要给对象进行赋值&#xff0c;通常会调用其set/get方法&#xff0c;有些时候&#xff0c;如果我们要转换的两个对象之间属性大致相同&#xff0c;会考虑使用属…

台达A2-M伺服

地址: P3.00 从站地址0x01~0x7F【1~127】 P3.01 通讯速度UZYX【0403】 X:0【4800】1【9600】2【19200】3【38400】4【57600】5【115200】Z:0【125Kbit/s】1【250】2【500】3【750】4【1Mbit/s】 P3.02 通讯格式6【8N2】7【8E1】8【8O1】 P3.03 1通讯错误刹停…

关于数组首地址a、a+1、a[0]、a[0]+1、*a、*a、a+0的解析

有一个数组&#xff1a; int a[4]{1,2,3,4};例如&#xff0c;sizeof(a),很明显它的结果是16&#xff0c;这个a就表示的是整个数组的大小&#xff0c;那么有 a1: 表示数组a的第二个元素即a[1]的地址 a0&#xff1a; a[0]的地址 &a[0]: a[0]的地址 &a[0…

Intent, Bundle, ListView的简单使用

Intent, Bundle, ListView的使用 无参数的Activity跳转&#xff1a; intent.setClass(MainActivity.this, InformationActivity.class); startActivity(intent);当前Activity A 向下一个Activity B跳转并传递数据&#xff1a; Bundle bundle new Bundle(); bundle.putString(…

vb随机显示图片

根目录下建一个文件夹PIC&#xff0c;在PIC下存放5张图片&#xff0c;名称分虽是1&#xff0c;2&#xff0c;3&#xff0c;4&#xff0c;5然后添加一个timer1控件&#xff0c;一个image1控件&#xff0c;它的interval设5000&#xff08;即5秒&#xff09;写如下代码Private Sub…

正则数字字母下划线至少两种_8085微处理器中至少两个8位数字

正则数字字母下划线至少两种Problem statement: 问题陈述&#xff1a; To find minimum of two 8bit numberusing 8085 microprocessor. 使用8085微处理器查找最少两个8位数字。 Algorithm: 算法&#xff1a; Load the accumulator with the first data. 向累加器加载第一个…

字符串操作的12个小技巧!

字符串可以说是 Java 中最具有代表性的类了&#xff0c;似乎没有之一哈&#xff0c;这就好像直播界的李佳琪&#xff0c;脱口秀中的李诞&#xff0c;一等一的大哥地位。不得不承认&#xff0c;最近吐槽大会刷多了&#xff0c;脑子里全是那些段子&#xff0c;写文章都有点不由自…

关于二维数组取地址加以或减一解引用问题

int main() { int aa[2][5] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int *ptr1 (int *)(&aa 1); int *ptr2 (int *)(*(aa 1)); printf("%d,%d", *(ptr1 - 1), *(ptr2 - 1));system("pause");return 0; }很显然aa是一个二维数组&#xff0c;很多…

rand和srand的用法

首先我们要对rand&#xff06;srand有个总体的看法:srand初始化随机种子,rand产生随机数&#xff0c;下面将详细说明。rand&#xff08;产生随机数&#xff09;表头文件: #include定义函数 :int rand(void)函数说明 :因为rand的内部实现是用线性同余法做的&#xff0c;他不是真…