LCIs segment Tree (interval update)

Source: Internet
Author: User

First, each node of the segment tree contains: [B,e],lmax,rmax,max; where Lmax represents the longest sequence length starting from the left endpoint, Rmax represents the longest descending sequence length starting from the e endpoint to the left, and Max represents the longest sequential length of successive increments of the current interval. Information about the insertion of a single node is maintained as follows:

(1) The value of the right endpoint of the If left son < The left end of the right son

lmax= Left son interval length = = left son's Lmax? Left son interval length + right son's Lmax: left son's Lmax.

rmax= Right son interval length = = Right son's rmax? Right son interval length + left son's Rmax: Right son's Rmax.

Max=max (Lmax,rmax, left son's rmax+ right son's Lmax);

(2) otherwise

Lmax= left son's Lmax;

rmax= right son's Rmax;

Max=max (Max of the left son, Max of the right son);

The search operations are as follows:

If current node range = = Query interval

Returns the current node's Max

Otherwise

              back to MAX (left, right, length across boundaries)

------------------------------------------------------------------//---Segment Tree interval update # define _crt_secure_no_ Deprecate#include<iostream> #include <algorithm>using namespace std;const int maxn = 1000000;struct Tnode{ int B, E;int lmax;int rmax;int max;int len () {return e-b + 1;}}; Tnode tree[4*maxn];int p[maxn];int N, m;int max (int A, int b, int c) {return Max (Max (A, B), c);} void Build (int v, int l, int r) {tree[v].b = L, tree[v].e = R;tree[v].lmax = Tree[v].rmax = Tree[v].max = 0;if (L < r) {I NT MID = (L + r) >> 1; Build (2 * v + 1, l, mid); Build (2 * v + 2, Mid + 1, r);}} void Insert (int v, int k, int val) {if (tree[v].b = = TREE[V].E) {//Find leaf node insert p[k] = Val;tree[v].lmax = Tree[v].rmax = Tree[v]. max = 1;return;} int mid = (tree[v].b + tree[v].e) >> 1;if (k <= mid) Insert (2 * v + 1, K, Val); Elseinsert (2 * v + 2, K, Val);//Next Update if (P[mid] < P[mid + 1]) {if (tree[2 * v + 1].lmax = = tree[2 * v + 1].len ()) Tree[v].lmax = tree[2 * v + 1].len () + tre E[2 * v + 2].LMAX;ELSETREE[V].Lmax = tree[2*v+1].lmax;if (tree[2 * v + 2].rmax = = tree[2 * v + 2].len ()) Tree[v].rmax = tree[2 * v + 2].len () + tree[2 * V + 1].rmax;elsetree[v].rmax = Tree[2*v+2].rmax;tree[v].max = Max (tree[2 * v + 1].max, tree[2 * v + 2].max,tree[2 * v + 1]. Rmax + tree[2 * v + 2].lmax);} Else{tree[v].lmax = Tree[2*v+1].lmax;tree[v].rmax = tree[2 * v + 2].rmax;tree[v].max = max (tree[2 * v + 1].max,tree[2 * V + 2].max);}} int Qurrey (int v,int l, int r) {if (L = = Tree[v].b&&r = = TREE[V].E) return tree[v].max;int mid = (tree[v].b + tree[v ].E) >> 1;if (R <= Mid) return Qurrey (2 * v + 1, L, R), else if (L > Mid) return Qurrey (2 * v + 2, L, R); else{if (P[mid] < P[mid + 1]) {int left = max (mid + 1-tree[2 * v + 1].rmax, l); int right = MIN (r, mid + tree[2 * v + 2].lmax); return Max (Qurrey (2 * V + 1, L, mid), Qurrey (2 * v + 2, Mid + 1, R), right-left+1);} Elsereturn Max (Qurrey (2 * v + 1, l, mid), Qurrey (2 * v + 2, Mid + 1, r));} }int Main () {int I, T,a,b;char s;scanf ("%d", &t), while (t--) {SCANF ("%d%d", &n, &m); memset (p, 0, sizeof (p)); Build (0, 0, n-1), for (i = 0; i < n; i++) {scanf ("%d", &p[i]), Insert (0, I, p[i]);} while (m--) {cin >> s;scanf ("%d%d", &a, &b), if (s = = ' U ') Insert (0, A, b); elseprintf ("%d\n", Qurrey (0, A, b));} }return 0;}

  

LCIs segment Tree (interval update)

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.