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

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点处掉下一个馅饼,或吃掉一个离当前距离最小的馅饼,如果距离相同则不转向优先(上次是向右移动,这次也是为不转向)
          若没有馅饼可吃,则呆在原地不动,问最后走的距离是多少。
解题思路:
         线段树维护区间最小最大值即可。
*/
#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<<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==-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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值