日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

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

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

來源:程序員人生   發(fā)布時間:2015-02-11 08:39:28 閱讀次數(shù):3676次

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 線段樹單點更新保護區(qū)間最小最大值;
題目大意:在x軸上有些點,在接下來的時刻會進行以下操作:在x點處掉下1個餡餅,或吃掉1個離當前距離最小的餡餅,如果距離相同則不轉(zhuǎn)向優(yōu)先(上次是向右移動,這次也是為不轉(zhuǎn)向)
          若沒有餡餅可吃,則呆在原地不動,問最后走的距離是多少。
解題思路:
         線段樹保護區(qū)間最小最大值便可。
*/
#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;
}


生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關(guān)閉
程序員人生
主站蜘蛛池模板: 欧美国产综合 | 午夜一区二区三区视频 | 一级毛片免费完整视频 | 香蕉视频成年人 | 尤物yw | 日韩av综合 | 久久精品一区二区国产 | 男女毛片 | 日韩综合 | 91高清免费看 | 成人18视频在线观看 | 欧美在线观看视频 | 久久精品成人 | 国产精品成人一区 | 久久久精品一区 | 免费看男女视频 | 欧美色亚洲 | 一区二区三区福利 | 视频一区二区在线 | 日本精品在线 | 99国产精品粉嫩初高生在线播放 | 久久桃色 | 国产午夜精品久久久久久久 | 81精品国产乱码久久久久久 | 国产高清一级毛片在线不卡 | 中文字幕一区二区三区乱码在线 | 91成人精品 | 欧洲中文字幕日韩精品成人 | 精品av天堂毛片久久久借种 | 欧美成人一区二区 | 亚色在线视频 | 亚洲欧美日韩在线播放 | 欧美在线视频二区 | 日韩有码一区二区三区 | 九九热在线免费视频 | 一区二区在线视频 | 久久男女视频 | 久久久国产一区二区三区 | 91蝌蚪色 | 97av在线视频免费播放 | 欧美日韩第一页 |