国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > php开源 > php教程 > HDU 4302 线段树单点更新,维护区间最大最小值

HDU 4302 线段树单点更新,维护区间最大最小值

来源:程序员人生   发布时间:2015-02-11 08:39:28 阅读次数:3469次

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

Problem Description
Holedox is a small animal which can be 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 in the pipe, from time to time. When Holedox wants to eat cakes, it always goes to the nearest one and eats it. If there are many pieces of cake in different directions Holedox can choose, Holedox will choose one in the direction which is the direction of its last movement. If there are no cakes present, Holedox just stays where it is.
 

Input
The input consists of several test cases. The first line of the input contains a single integer T (1 <= T <= 10), the number of test cases, followed by the input data for each test case.The first line of each case contains two integers L,n(1<=L,n<=100000), representing the length of the pipe, and the number of events. 
The next n lines, each line 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.
In each case, Holedox always starts off at the position 0.
 

Output
Output the total distance Holedox will move. Holedox don’t need to return to the position 0.
 

Sample Input
3 10 8 0 1 0 5 1 0 2 0 0 1 1 1 10 7 0 1 0 5 1 0 2 0 0 1 1 10 8 0 1 0 1 0 5 1 0 2 0 0 1 1
 

Sample Output
Case 1: 9 Case 2: 4 Case 3: 2
/**
HDU 4032 线段树单点更新保护区间最小最大值;
题目大意:在x轴上有些点,在接下来的时刻会进行以下操作:在x点处掉下1个馅饼,或吃掉1个离当前距离最小的馅饼,如果距离相同则不转向优先(上次是向右移动,这次也是为不转向)
          若没有馅饼可吃,则呆在原地不动,问最后走的距离是多少。
解题思路:
         线段树保护区间最小最大值便可。
*/
#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=⑴;
        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=⑴;
        }
        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<<1,l,r);
    else if(l>mid)
        return query1(i<<1|1,l,r);
    else
        return max(query1(i<<1,l,mid),query1(i<<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<<1,l,r);
    else if(l>mid)
        return query2(i<<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==⑴&&t2!=INF)
                {
                    ans+=t2-x;
                    x=t2;
                    del(1,t2);
                    pre=1;
                }
                else if(t1!=⑴&&t2==INF)
                {
                    ans+=x-t1;
                    x=t1;
                    del(1,t1);
                    pre=⑴;
                }
                else if(t1!=⑴&&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=⑴;
                    }
                    else
                    {
                        if(pre==1)
                        {
                            ans+=t2-x;
                            x=t2;
                            del(1,t2);
                            pre=1;
                        }
                        else
                        {
                            ans+=x-t1;
                            x=t1;
                            del(1,t1);
                            pre=⑴;
                        }
                    }
                }
            }
        }
        printf("Case %d: %d
",++tt,ans);
    }
    return 0;
}


生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生