POJ 1754 Segment Tree

Source: Internet
Author: User

E, it should be a water problem in the line tree. Line segment Tree single point update. The most value between Sany.

Code Template PS: Templates Some places don't quite understand.

#include <stdio.h>
#include <iostream>
#include <string.h>
#define MAXN 200010
using namespace Std;

int VAL[MAXN];

struct Node
{
int Max; We need to find the maximum value here.
int left, right;
}TREE[MAXN*4]; Said. The segment tree node needs to be an array of three to five times larger

int max (int a, int b)
{
Return a > B? A:B;
}

int build (int root, int left, int right)//root node left and right array-length tree
{
Tree[root].left = left;
Tree[root].right = right;
if (left = = right)
return Tree[root].max = Val[left]; assigning values to leaf nodes

int mid = (left + right)/2;
int a = Build (2*root, left, mid); Establishment of Saozi right sub-tree
int B = Build (2*root+1, mid+1, right);
return Tree[root].max = Max (A, b);
}

int update (int root, int pos, int val)//single-point update update the POS position element to Val
{
if (Tree[root].left = = pos && tree[root].right = = pos)//If the node corresponding to the location is found
return Tree[root].max = val;
if (Tree[root].left > pos | | tree[root].right < POS)//If not. These are two kinds of recursive breach. E. But I'm not quite sure what the return value of these two cases is.
return Tree[root].max;
int A, B;
A = Update (2*root, POS, Val); Update Saozi Right subtree
b = Update (2*root+1, POS, Val);
return Tree[root].max = Max (A, b);
}

int calculate (int root, int left, int right)//find the maximum value of the interval left to right
{
if (Tree[root].left > right | | tree[root].right < left)//If there is no intersection between the current interval and the range you are seeking
return 0;
if (left <= tree[root].left && tree[root].right <= right)//If the current interval is fully contained within the desired interval. I do not understand why we should return the maximum value of the current interval.
return Tree[root].max;
int A, B;
A = Calculate (2*root, left, right); Both of these cases are wood. We need to decompose the interval to find the maximum value of the sub-interval.
b = Calculate (2*root+1, left, right);
Return Max (A, b);
}

int main ()
{
int n, m;
while (CIN >> n >> m)
{
for (int i=1; i<=n; ++i)
scanf ("%d", &val[i]);
Build (1, 1, N);
while (m--)
{
char temp;
int A, B;
CIN >> Temp >> a >> b;
if (temp = = ' Q ')
cout << Calculate (1, a, b) << Endl;
else update (1, a, b);
}
}
return 0;
}




POJ 1754 Segment Tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.