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+,M=1e6+,inf=1e9+,mod=1e9+;
const ll INF=1e18+;
map<int,int>mp;
int n,m;
struct is
{
int llen;
int rlen;
int l,r,ans;
}tree[N<<];
int getmaxx(int l,int r)
{
if(l==)
return r;
if(r==n)
return r-l+;
return (r+l)/-l+;
}
void build(int l,int r,int pos)
{
tree[pos].l=l;
tree[pos].r=r;
tree[pos].llen=tree[pos].rlen=(r-l+);
tree[pos].ans=getmaxx(l,r);
if(l==r)return;
int mid=(l+r)>>;
build(l,mid,pos<<);
build(mid+,r,pos<<|);
}
void pushup(int pos,int l,int r)
{
int mid=(l+r)>>;
tree[pos].llen=tree[pos<<].llen;
tree[pos].rlen=tree[pos<<|].rlen;
if(tree[pos<<].ans==&&tree[pos<<|].ans==)
{
tree[pos].ans=;
}
else if(tree[pos<<].ans>=tree[pos<<|].ans&&tree[pos<<].ans>=getmaxx(mid-tree[pos<<].rlen+,mid+tree[pos<<|].llen))
{
tree[pos].l=tree[pos<<].l;
tree[pos].r=tree[pos<<].r;
tree[pos].ans=getmaxx(tree[pos].l,tree[pos].r);
if(tree[pos<<].ans==getmaxx(mid-tree[pos<<].rlen+,mid+tree[pos<<|].llen)&&tree[pos<<].l>=mid-tree[pos<<].rlen+)
{
tree[pos].l=mid-tree[pos<<].rlen+;
tree[pos].r=mid+tree[pos<<|].llen;
tree[pos].ans=getmaxx(tree[pos].l,tree[pos].r);
}
}
else if(getmaxx(mid-tree[pos<<].rlen+,mid+tree[pos<<|].llen)>=tree[pos<<|].ans)
{
tree[pos].l=mid-tree[pos<<].rlen+;
tree[pos].r=mid++tree[pos<<|].llen-;
tree[pos].ans=getmaxx(tree[pos].l,tree[pos].r);
}
else
{
tree[pos].l=tree[pos<<|].l;
tree[pos].r=tree[pos<<|].r;
tree[pos].ans=getmaxx(tree[pos].l,tree[pos].r);
}
if(tree[pos<<].llen==mid-l+)
tree[pos].llen+=tree[pos<<|].llen;
if(tree[pos<<|].rlen==r-(mid+)+)
tree[pos].rlen+=tree[pos<<].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=;
}
else
{
tree[pos].llen=tree[pos].rlen=tree[pos].ans=;
tree[pos].l=p;
tree[pos].r=p;
}
return;
}
int mid=(l+r)>>;
if(p<=mid)
update(p,c,l,mid,pos<<);
else
update(p,c,mid+,r,pos<<|);
pushup(pos,l,r);
}
int main()
{
scanf("%d%d",&n,&m);
build(,n,);
while(m--)
{
int flag,x;
scanf("%d%d",&flag,&x);
if(flag==)
{
int pos=(tree[].l+tree[].r)>>;
if(tree[].l==)
pos=;
else if(tree[].r==n)
pos=n;
update(pos,,,n,);
printf("%d\n",pos);
mp[x]=pos;
}
else
{
update(mp[x],,,n,);
mp[x]=;
}
}
return ;
}

Codeforces Round #135 (Div. 2) E. Parking Lot 线段数区间合并的更多相关文章

  1. 树形DP Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland

    题目传送门 /* 题意:求一个点为根节点,使得到其他所有点的距离最短,是有向边,反向的距离+1 树形DP:首先假设1为根节点,自下而上计算dp[1](根节点到其他点的距离),然后再从1开始,自上而下计 ...

  2. 构造 Codeforces Round #135 (Div. 2) B. Special Offer! Super Price 999 Bourles!

    题目传送门 /* 构造:从大到小构造,每一次都把最后不是9的变为9,p - p MOD 10^k - 1,直到小于最小值. 另外,最多len-1次循环 */ #include <cstdio&g ...

  3. 贪心 Codeforces Round #135 (Div. 2) C. Color Stripe

    题目传送门 /* 贪心:当m == 2时,结果肯定是ABABAB或BABABA,取最小改变量:当m > 2时,当与前一个相等时, 改变一个字母 同时不和下一个相等就是最优的解法 */ #incl ...

  4. Codeforces Round #603 (Div. 2) E. Editor(线段树)

    链接: https://codeforces.com/contest/1263/problem/E 题意: The development of a text editor is a hard pro ...

  5. Codeforces Round #135 (Div. 2)

    A. k-String 统计每个字母出现次数即可. B. Special Offer! Super Price 999 Bourles! 枚举末尾有几个9,注意不要爆掉\(long\ long\)的范 ...

  6. Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland dfs

    D. Choosing Capital for Treeland time limit per test 3 seconds memory limit per test 256 megabytes i ...

  7. Codeforces Round #135 (Div. 2)---A. k-String

    k-String time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...

  8. Codeforces Round #135 (Div. 2) D - Choosing Capital for Treeland(两种树形DP)

  9. Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland

    time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standa ...

随机推荐

  1. Servlet乱码

      request.setCharacterEncoding():是设置从request中取得的值或从数据库中取出的值 (只管post方式提交的问题///get需在server.xml中的: < ...

  2. loadruner报错:Step download timeout(120 seconds)的一个解决方法

    一个网友问了我一个问题如下: loadruner报错:Error -27728: Step download timeout (120 seconds) 如何解决 语 法检查通过,但是在并发执行一个查 ...

  3. linux,Mac下lu 一把

    习惯Terminal没有不知道ls命令的(等同于DOS的dir),经常只是需要查看目录的内容大小,但ls -h显示的只是目录的本身大小,而且很多项内容 ls 在这方面的两个诟病出现了: 小诟1. 显示 ...

  4. ORA-29339错误解决办法

    create tablespace TBS_JACK_16k blocksize 16k datafile '/u01/app/oracle/oradata/orcl/TBS_JACK_32K_01. ...

  5. Java学习网站

    黑马: http://bbs.itheima.com/forum.php 考试网站: http://www.nowcoder.com/ 牛客网 比较专业的学习技术网站: http://www.ibm. ...

  6. php header()函数设置页面Cache缓存

    header()函数在php的使用很大,下面我来介绍利用它实现页面缓存的一些方法,但使用header前必须注意,在它之前不能任何输出,包括空格. 手册上,我们对于cache都是写着如何设置,以便让代码 ...

  7. 对已有的2个一维数组,譬如说A[],B[],经过最少循环找出2个数组重复的元素。

    import java.util.Arrays; /** * Created by ccc on 16-4-27. */ public class Test { public static void ...

  8. hihoCoder 数论五·欧拉函数

    题目1 : 数论五·欧拉函数 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho有时候会用密码写信来互相联系,他们用了一个很大的数当做密钥.小Hi和小Ho约定 ...

  9. 网络编程中获取域名和id的方法

    package com.lanqiao.java.test; import java.net.InetAddress;import java.net.UnknownHostException; pub ...

  10. JQuery知识快览之二—事件

    事件是脚本语言的核心.本文将为大家介绍JQuery支持的一些事件和如何自定义事件 JQuery内置事件 1.Document加载事件 JQuery提供了ready,load,unload三个Docum ...