Codevs3304 Fruit Street Shopping Ⅰ

Source: Internet
Author: User

http://codevs.cn/problem/3304/

  • Solving
    The topic is an obvious interval query problem, you can quickly think of a line tree and other data structures. (I do not know why the interval DP, the pretreatment of at least O( n 2 ) , is DP really going to make it? )

  • The first is achievement. By test instructions, it is obvious that each node should include the interval left endpoint , the right endpoint , the maximum value , the minimum value , due to the direction of the one-way route, so there should be a range from left to right to walk the maximum difference and The maximum difference from right to left . where Max and Min values are easy to maintain, then the remaining two is the maximum difference.

  • Consider the leaf node first, the maximum difference between the two directions is obviously 0;
    The core of the segment tree is the merging of two intervals, that is known to two sub-ranges, how to put the maximum difference and what?
    Take the maximum difference from left to right for example, which is divided into three cases:
    1. The difference is obtained in the interval of the left subtree, and all the cases of the left subtree are known;
    2, the difference in the right sub-tree to obtain the interval, and all the circumstances of the right subtree are known;
    3. The difference is the maximum value of the right subtree minus the maximum value of the left subtree, and all the cases of the Saozi right subtree are known;
    These three cases take maximum.
    the maximum difference from right to left is the same, just change the 3rd case to the maximum of the left subtree minus the minimum value of the right subtree.

  • The tree has been built, and the problem is basically solved.
    For the x==y situation, the answer must be 0, direct output, save time.
    For this section of the query interval [x,y] , starting from the root, if the segment tree node corresponding to the interval is included in the query interval, directly return the answer; If the right subtree is not involved, only the left subtree is queried, and if the left subtree is not involved, only the right subtree is queried: This is the basic query method of the line segment tree.
    When both left and right subtrees are involved, it is only necessary to query the maximal difference and the maximum value separately in the Saozi tree, and the answer is the maximum value between the maximum and the maximum difference.

  • Code

#include <cstdio> #include <algorithm> #define TLC (k) (K <<1) #define TRC (k) (K <<1|1) usingnamespaceStd;constintMAXN =800100, root =1, nil =0;intTMAX[MAXN], TMIN[MAXN], ANSL[MAXN], ANSR[MAXN], LC[MAXN], RC[MAXN];intN, M, A[maxn >>2];void Update (intk) {Tmax[k] =Max(TMAX[TLC (k)], TMAX[TRC (k)]); TMIN[K] =min(TMIN[TLC (k)], TMIN[TRC (k)]); ANSL[K] =Max(ANSL[TLC (k)], ANSL[TRC (k)]); ANSL[K] =Max(Ansl[k], TMAX[TRC (k)]-TMIN[TLC (k)]); ANSR[K] =Max(ANSR[TLC (k)], ANSR[TRC (k)]); ANSR[K] =Max(Ansr[k], TMAX[TLC (k)]-tmin[trc (k)]); void Build (int Rot,intLintR) {if(L = = r) {tmax[Rot] = tmin[Rot] = A[l]; ansl[Rot] = ansr[Rot] =0; lc[Rot] = rc[Rot] = l;return; }intMid = ((L + R) >>1); lc[Rot] = l; rc[Rot] = R; Build (TLC (Rot), L, mid); Build (TRC (Rot), Mid +1, R); UpdateRot);}intQuerymax (int Rot,intLintR) {if(L <= lc[Rot] && R >= rc[Rot])returntmax[Rot];intMid = ((lc[Rot] + rc[Rot]) >>1);if(R <= Mid)returnQuerymax (TLC (Rot), L, R);Else if(L > Mid)returnQuerymax (TRC (Rot), L, R);Else return Max(TLC (Querymax) (Rot), L, R), Querymax (TRC (Rot), L, R);}intQuerymin (int Rot,intLintR) {if(L <= lc[Rot] && R >= rc[Rot])returntmin[Rot];intMid = ((lc[Rot] + rc[Rot]) >>1);if(R <= Mid)returnQuerymin (TLC (Rot), L, R);Else if(L > Mid)returnQuerymin (TRC (Rot), L, R);Else return min(TLC (Querymin) (Rot), L, R), Querymin (TRC (Rot), L, R);}intQueryl (int Rot,intLintR) {if(L <= lc[Rot] && R >= rc[Rot])returnansl[Rot];intMid = ((lc[Rot] + rc[Rot]) >>1);if(R <= Mid)returnQueryl (TLC (Rot), L, R);Else if(L > Mid)returnQueryl (TRC (Rot), L, R);Else{intTMP =Max(TLC (Queryl) (Rot), L, mid), Queryl (TRC (Rot), Mid +1, R)); TMP =Max(TMP, Querymax (TRC (Rot), Mid +1, R)-Querymin (TLC (Rot), L, mid));returntmp }   }intQueryr (int Rot,intLintR) {if(L <= lc[Rot] && R >= rc[Rot])returnansr[Rot];intMid = ((lc[Rot] + rc[Rot]) >>1);if(R <= Mid)returnQueryr (TLC (Rot), L, R);Else if(L > Mid)returnQueryr (TRC (Rot), L, R);Else{intTMP =Max(TLC (Queryr) (Rot), L, mid), Queryr (TRC (Rot), Mid +1, R)); TMP =Max(TMP, Querymax (TLC) (Rot), L, mid)-Querymin (TRC (Rot), Mid +1, R));returntmp }}void init () {scanf ("%d", &n); for(inti =1; I <= N; ++i) scanf ("%d", &a[i]); Build (Root,1, n); scanf"%d", &m);} void work () {intx, y; while(m--) {scanf ("%d%d", &x, &y);if(x < y) printf ("%d\n", Queryl (root, x, y));Else if(x > Y) printf ("%d\n", Queryr (root, y, x));ElsePuts"0"); }}intMain () {init (); Work ();return 0;}

Codevs3304 Fruit Street Shopping Ⅰ

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.