zoukankan      html  css  js  c++  java
  • Codeforces Round #135 (Div. 2) E. Parking Lot 线段数区间合并

    E. Parking Lot
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    A parking lot in the City consists of n parking spaces, standing in a line. The parking spaces are numbered from 1 to n from left to right.

    When a car arrives at the lot, the operator determines an empty parking space for it. For the safety's sake the chosen place should be located as far from the already occupied places as possible. That is, the closest occupied parking space must be as far away as possible. If there are several such places, then the operator chooses the place with the minimum index from them. If all parking lot places are empty, then the car gets place number 1.

    We consider the distance between the i-th and the j-th parking spaces equal to 4·|i - j| meters.

    You are given the parking lot records of arriving and departing cars in the chronological order. For each record of an arriving car print the number of the parking lot that was given to this car.

    Input

    The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 2·105) — the number of parking places and the number of records correspondingly.

    Next m lines contain the descriptions of the records, one per line. The i-th line contains numbers tiidi (1 ≤ ti ≤ 2; 1 ≤ idi ≤ 106). If tiequals 1, then the corresponding record says that the car number idi arrived at the parking lot. If ti equals 2, then the corresponding record says that the car number idi departed from the parking lot.

    Records about arriving to the parking lot and departing from the parking lot are given chronologically. All events occurred consecutively, no two events occurred simultaneously.

    It is guaranteed that all entries are correct:

    • each car arrived at the parking lot at most once and departed from the parking lot at most once,
    • there is no record of a departing car if it didn't arrive at the parking lot earlier,
    • there are no more than n cars on the parking lot at any moment.

    You can consider the cars arbitrarily numbered from 1 to 106, all numbers are distinct. Initially all places in the parking lot are empty.

    Output

    For each entry of an arriving car print the number of its parking space. Print the numbers of the spaces in the order, in which the cars arrive to the parking lot.

    Examples
    input
    7 11
    1 15
    1 123123
    1 3
    1 5
    2 123123
    2 15
    1 21
    2 3
    1 6
    1 7
    1 8
    output
    1
    7
    4
    2
    7
    4
    1
    3

     思路:区间合并;搓代码;

    #include<bits/stdc++.h>
    using namespace std;
    #define ll long long
    #define pi (4*atan(1.0))
    #define eps 1e-14
    const int N=2e5+10,M=1e6+10,inf=1e9+10,mod=1e9+7;
    const ll INF=1e18+10;
    map<int,int>mp;
    int n,m;
    struct is
    {
        int llen;
        int rlen;
        int l,r,ans;
    }tree[N<<2];
    int getmaxx(int l,int r)
    {
        if(l==1)
            return r;
        if(r==n)
            return r-l+1;
        return (r+l)/2-l+1;
    }
    void build(int l,int r,int pos)
    {
        tree[pos].l=l;
        tree[pos].r=r;
        tree[pos].llen=tree[pos].rlen=(r-l+1);
        tree[pos].ans=getmaxx(l,r);
        if(l==r)return;
        int mid=(l+r)>>1;
        build(l,mid,pos<<1);
        build(mid+1,r,pos<<1|1);
    }
    void pushup(int pos,int l,int r)
    {
        int mid=(l+r)>>1;
        tree[pos].llen=tree[pos<<1].llen;
        tree[pos].rlen=tree[pos<<1|1].rlen;
        if(tree[pos<<1].ans==0&&tree[pos<<1|1].ans==0)
        {
            tree[pos].ans=0;
        }
        else if(tree[pos<<1].ans>=tree[pos<<1|1].ans&&tree[pos<<1].ans>=getmaxx(mid-tree[pos<<1].rlen+1,mid+tree[pos<<1|1].llen))
        {
            tree[pos].l=tree[pos<<1].l;
            tree[pos].r=tree[pos<<1].r;
            tree[pos].ans=getmaxx(tree[pos].l,tree[pos].r);
            if(tree[pos<<1].ans==getmaxx(mid-tree[pos<<1].rlen+1,mid+tree[pos<<1|1].llen)&&tree[pos<<1].l>=mid-tree[pos<<1].rlen+1)
            {
                tree[pos].l=mid-tree[pos<<1].rlen+1;
                tree[pos].r=mid+tree[pos<<1|1].llen;
                tree[pos].ans=getmaxx(tree[pos].l,tree[pos].r);
            }
        }
        else if(getmaxx(mid-tree[pos<<1].rlen+1,mid+tree[pos<<1|1].llen)>=tree[pos<<1|1].ans)
        {
            tree[pos].l=mid-tree[pos<<1].rlen+1;
            tree[pos].r=mid+1+tree[pos<<1|1].llen-1;
            tree[pos].ans=getmaxx(tree[pos].l,tree[pos].r);
        }
        else
        {
            tree[pos].l=tree[pos<<1|1].l;
            tree[pos].r=tree[pos<<1|1].r;
            tree[pos].ans=getmaxx(tree[pos].l,tree[pos].r);
        }
        if(tree[pos<<1].llen==mid-l+1)
            tree[pos].llen+=tree[pos<<1|1].llen;
        if(tree[pos<<1|1].rlen==r-(mid+1)+1)
            tree[pos].rlen+=tree[pos<<1].rlen;
    }
    void update(int p,int c,int l,int r,int pos)
    {
        if(l==p&&r==p)
        {
            if(!c)
            {
                tree[pos].llen=tree[pos].rlen=tree[pos].ans=0;
            }
            else
            {
                tree[pos].llen=tree[pos].rlen=tree[pos].ans=1;
                tree[pos].l=p;
                tree[pos].r=p;
            }
            return;
        }
        int mid=(l+r)>>1;
        if(p<=mid)
        update(p,c,l,mid,pos<<1);
        else
            update(p,c,mid+1,r,pos<<1|1);
        pushup(pos,l,r);
    }
    int main()
    {
        scanf("%d%d",&n,&m);
        build(1,n,1);
        while(m--)
        {
            int flag,x;
            scanf("%d%d",&flag,&x);
            if(flag==1)
            {
                int pos=(tree[1].l+tree[1].r)>>1;
                if(tree[1].l==1)
                    pos=1;
                else if(tree[1].r==n)
                    pos=n;
                update(pos,0,1,n,1);
                printf("%d
    ",pos);
                mp[x]=pos;
            }
            else
            {
                update(mp[x],1,1,n,1);
                mp[x]=0;
            }
        }
        return 0;
    }
  • 相关阅读:
    boost::asio 连接管理11 如何关闭连接
    boost::asio async_write也不能保证一次发完所有数据 二
    boost::asio async_write也不能保证一次发完所有数据 一
    boost参考博客
    C++ 多线程编程总结
    Boost::asio io_service 实现分析
    使用boost io_service时,需要注意的东西
    Boost::Thread 多线程的基础知识
    boost::thread类
    Boost::thread库的使用
  • 原文地址:https://www.cnblogs.com/jhz033/p/5988197.html
Copyright © 2011-2022 走看看