Test instructions: The solution of the minimum continuous maximum and starting position of the static interval of the Q inquiry and the lowest output dictionary order of the terminating position.
Idea: Rujia petition maintenance of three values per node
Pre, Sub, suf the largest prefix and, consecutive and, suffix and then this question also records the location of the solution, so also the sum of the interval sums
1#include <iostream>2#include <string>3#include <algorithm>4#include <cstdlib>5#include <cstdio>6#include <Set>7#include <map>8#include <vector>9#include <cstring>Ten#include <stack> One#include <cmath> A#include <queue> -#include <bits/stdc++.h> - using namespacestd; the #defineINF 0x3f3f3f3f - #definell Long Long - #defineCLC (A, B) memset (A,b,sizeof (a)) - + Const intMAXN =500000+Ten; - Const intMaxnode =1000000+Ten; +typedefLong LongLL; Atypedef pair<int,int>Interval; at - LL PREFIX_SUM[MAXN]; - -LL sum (intLintR) - { - returnPREFIX_SUM[R]-prefix_sum[l-1]; in } - to LL sum (Interval p) + { - returnsum (P.first, p.second); the } * $ Interval Better (Interval A, Interval b)Panax Notoginseng { - if(SUM (a)! = SUM (b))returnSUM (a) > sum (b)?a:b; the returnA < b? A:B;//using the dictionary sequence of the pair's own + } A the intQL, QR; + - structIntervaltree $ { $ intMax_prefix[maxnode]; - intMax_suffix[maxnode]; - Interval Max_sub[maxnode]; the - voidBuildintOintLintR)Wuyi { the if(L = =R) - { WuMax_prefix[o] = Max_suffix[o] =L; -Max_sub[o] =Make_pair (L, l); About } $ Else - { - intM = L + (r-l)/2; - //Recursive creation of sub-trees A intLC = o*2, rc = o*2+1; + Build (LC, L, M); theBuild (RC, m+1, R); - $ //recursive Max_prefix theLL V1 =sum (L, MAX_PREFIX[LC]); theLL v2 =sum (L, MAX_PREFIX[RC]); the if(V1 = = v2) Max_prefix[o] =min (MAX_PREFIX[LC], MAX_PREFIX[RC]); the ElseMax_prefix[o] = v1 > v2?MAX_PREFIX[LC]: MAX_PREFIX[RC]; - in //recursive Max_suffix theV1 =sum (MAX_SUFFIX[LC], R); theV2 =sum (MAX_SUFFIX[RC], R); About if(V1 = = v2) Max_suffix[o] =min (MAX_SUFFIX[LC], MAX_SUFFIX[RC]); the ElseMax_suffix[o] = v1 > v2?MAX_SUFFIX[LC]: MAX_SUFFIX[RC]; the the //recursive max_sub +Max_sub[o] = better (MAX_SUB[LC], MAX_SUB[RC]);//completely in the left subtree or right sub-tree -Max_sub[o] = better (Max_sub[o], Make_pair (MAX_SUFFIX[LC], MAX_PREFIX[RC]));//Cross midline the }Bayi } the theInterval Query_prefix (intOintLintR) - { - if(Max_prefix[o] <= QR)returnMake_pair (L, Max_prefix[o]); the intM = L + (r-l)/2; the intLC = o*2, rc = o*2+1; the if(QR <= M)returnQuery_prefix (LC, L, M); theInterval i = Query_prefix (RC, m+1, R); -I.first =L; the returnbetter (i, Make_pair (L, MAX_PREFIX[LC])); the } the 94Interval Query_suffix (intOintLintR) the { the if(Max_suffix[o] >= QL)returnMake_pair (Max_suffix[o], R); the intM = L + (r-l)/2;98 intLC = o*2, rc = o*2+1; About if(QL > M)returnQuery_suffix (RC, m+1, R); -Interval i =Query_suffix (LC, L, M);101I.second =R;102 returnbetter (i, Make_pair (MAX_SUFFIX[RC], R));103 }104 theInterval Query (intOintLintR)106 {107 if(QL <= L && R <= QR)returnMax_sub[o];108 intM = L + (r-l)/2;109 intLC = o*2, rc = o*2+1; the if(QR <= M)returnQuery (LC, L, M);111 if(QL > M)returnQuery (RC, m+1, R); theInterval i1 = Query_prefix (RC, m+1, R);//prefix to right half113Interval i2 = Query_suffix (LC, L, M);//suffix of the left half theInterval i3 = Better (query (LC, L, M), query (RC, m+1, R)); the returnBetter (Make_pair (I2.first, I1.second), i3); the }117 };118 119 Intervaltree Tree; - 121 intMain ()122 {123 intKase =0, N, a, Q;124 while(SCANF ("%d%d", &n, &q) = =2) the {126prefix_sum[0] =0;127 for(inti =0; I < n; i++) - {129scanf"%d", &a); theprefix_sum[i+1] = Prefix_sum[i] +A;131 } theTree.build (1,1, n);133printf"Case %d:\n", ++Kase);134 while(q--)135 {136 intL, R;137scanf"%d%d", &l, &R);138QL =L;139QR =R; $Interval ans = tree.query (1,1, n);141printf"%d%d\n", Ans.first, ans.second);142 }143 }144 return 0;145}
View Code
Uvalive 3938 "Ray, Pass me the dishes!" segment tree interval merge