多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php開源 > php教程 > HDU 4302 線段樹單點更新,維護區間最大最小值

HDU 4302 線段樹單點更新,維護區間最大最小值

來源:程序員人生   發布時間:2015-02-11 08:39:28 閱讀次數:3674次

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;
}


生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 国产色视频一区二区三区 | 日本wwxx护士| 一区二区三区四区国产 | 大焦伊人 | 亚洲色图欧美色 | 春意影院午夜免费入口 | 91视频www| 国产高清免费视频 | 日本一级级特黄特色大片 | 免费看www| 国产精品久久久久久久久久98 | 91在线精品亚洲一区二区 | 国产精品99久久久久久夜夜嗨 | 自拍自偷 | 老司机成人午夜精品福利视频 | 国产精品v欧美精品v日韩精品 | 日韩亚洲欧美性感视频影片免费看 | 国产成人一区二区三区影院免费 | 日韩爱爱小视频 | 中文字幕在线观看网站 | 国产一级做a爰片... | 国产精品亚洲精品日韩动图 | 日本视频中文字幕一区二区 | 日本不卡免费高清一级视频 | 国产a一级 | 欧美最猛黑人xxxx黑人猛交98 | 欧美性videosex18 | 国产五月婷婷 | jzz欧美 | 最新国产一区二区精品久久 | 欧美日韩亚洲高清不卡一区二区三区 | 一二区 | 国产成人精品亚洲午夜麻豆 | 亚洲春色在线观看 | 欧洲乱码专区一区二区三区四区 | 国产精品第| 最近中文字幕最新在线视频 | 欧美一区二区在线免费观看 | 校园春色欧美日韩 | 成人国产一区二区三区 | 国产老妇女 |