HDU 4302 Segment Tree single point update, maintenance interval Maximum minimum value

Source: Internet
Author: User

http://acm.hdu.edu.cn/showproblem.php?pid=4302

Problem Descriptionholedox is a small animal which can being considered as one point. It lives in a straight pipe whose length is L. Holedox can only move along the pipe. Cakes may appear anywhere on the pipe, from time to time. When Holedox wants to eat cakes, it always goes to the nearest one and eats it. If There is many pieces of cake in different directions holedox can choose, Holedox'll choose one in the direction whic H is the direction of it last movement. If There is no cakes present, holedox just stays where it is.

Inputthe input consists of several test cases. The first line of the input contains a single integer t (1 <= t <=), the number of test cases, followed by the Put data for each test case. The first line of each case contains the integers l,n (1<=l,n<=100000), representing the length of the pipe, and the Number of events.
The next n lines, each of the describes an event. 0 x (0<=x<=l, x is a integer) represents a piece of cake appears in the X position; 1 represent Holedox wants to eat a cake.
Holedox always starts off at the position 0.

Outputoutput the total distance holedox'll move. Holedox don ' t need to return to the position 0.
Sample Input
310 80 10 510 20 0111 10 70 10 510 20 01110 80 10 10 510 20 011

Sample Output
Case 1:9case 2:4case 3:2
/**hdu 4032 Segment Tree Single-point update maintenance interval minimum maximum; the main idea: there are some points on the x-axis, which will be done at the following time: Drop a pie at the X-point, or eat a pie that is the smallest from the current distance, and if the distance is the same, do not turn to priority (the last move is right, This is also to not turn) if there is no pie to eat, then stay in place, ask the last distance is how much. Problem-Solving ideas: Segment tree maintenance interval Minimum maximum value can be. * * #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm>using    namespace Std;const int Maxn=100010;const int inf=0x3f3f3f3f;struct node{int l,r;    int t; int Minn,maxx;}    tree[maxn*3];void Build (int i,int l,int r) {tree[i].l=l;    Tree[i].r=r;    tree[i].t=0;        if (l==r) {tree[i].minn=inf;        Tree[i].maxx=-1;    Return    } int mid= (L+R) >>1;    Build (I<<1,l,mid);    Build (I<<1|1,mid+1,r);    Tree[i].maxx=max (Tree[i<<1].maxx,tree[i<<1|1].maxx); Tree[i].minn=min (Tree[i<<1].minn,tree[i<<1|1].minn);}        void Add (int i,int t) {if (tree[i].l==t&&tree[i].r==t) {tree[i].maxx=tree[i].minn=t;        tree[i].t++;    Return    } int mid= (TREE[I].L+TREE[I].R) >>1; if (t<=mid) Add (i<<1,t);    else Add (i<<1|1,t);    Tree[i].maxx=max (Tree[i<<1].maxx,tree[i<<1|1].maxx); Tree[i].minn=min (Tree[i<<1].minn,tree[i<<1|1].minn);}        void del (int i,int t) {if (tree[i].l==t&&tree[i].r==t) {tree[i].t--;            if (tree[i].t==0) {tree[i].minn=inf;        Tree[i].maxx=-1;    } return;    } int mid= (TREE[I].L+TREE[I].R) >>1;    if (T<=mid) del (i<<1,t);    Else del (i<<1|1,t);    Tree[i].maxx=max (Tree[i<<1].maxx,tree[i<<1|1].maxx); Tree[i].minn=min (Tree[i<<1].minn,tree[i<<1|1].minn);}    int query1 (int i,int l,int R) {if (tree[i].l==l&&tree[i].r==r) return tree[i].maxx;    int mid= (TREE[I].L+TREE[I].R) >>1;    if (R<=mid) return Query1 (I&LT;&LT;1,L,R);    else if (L>mid) return Query1 (I&LT;&LT;1|1,L,R); else return Max (Query1 (I<<1,l,mid), Query1 (I&LT;&Lt;1|1,mid+1,r));}    int query2 (int i,int l,int R) {if (tree[i].l==l&&tree[i].r==r) return tree[i].minn;    int mid= (TREE[I].L+TREE[I].R) >>1;    if (R<=mid) return Query2 (I&LT;&LT;1,L,R);    else if (L>mid) return Query2 (I&LT;&LT;1|1,L,R); else return min (Query2 (i<<1,l,mid), Query2 (I<<1|1,mid+1,r));}    int main () {int t,tt=0;    scanf ("%d", &t);        while (t--) {int n,m;        scanf ("%d%d", &n,&m);        Build (1,0,n);        int pre=1,x=0,ans=0;            while (m--) {int A, B;            scanf ("%d", &a);                if (a==0) {scanf ("%d", &b);            Add (1,b);                } else {int t1=query1 (1,0,X);                int T2=query2 (1,X,N);                    if (t1==-1&&t2!=inf) {ans+=t2-x;                    X=t2;                    Del (1,T2);              pre=1;  } else if (T1!=-1&&t2==inf) {ans+=x-t1;                    X=T1;                    Del (1,T1);                Pre=-1;                    } else if (T1!=-1&&t2!=inf) {if (x-t1>t2-x)                        {ans+=t2-x;                        X=t2;                        Del (1,T2);                    pre=1;                        } else if (x-t1<t2-x) {ans+=x-t1;                        X=T1;                        Del (1,T1);                    Pre=-1;                            } else {if (pre==1) {                            Ans+=t2-x;                            X=t2;                            Del (1,T2);                        pre=1;                      } else  {ans+=x-t1;                            X=T1;                            Del (1,T1);                        Pre=-1;    }}}}} printf ("Case%d:%d\n", ++tt,ans); } return 0;}


HDU 4302 Segment Tree single point update, maintenance interval Maximum minimum value

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.