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. ASP+Access UTF-8 网页乱码问题解决办法

    用ACCESS数据库和ASP做网站时用UTF-8编码有时会出现乱码,再者网页出错或者刷新页面后就是乱码,如果数据库取值乱码在开头加上<%@LANGUAGE="VBSCRIPT" ...

  2. Oracle 临时表

    一.临时表的介绍: Oracle的临时表只存在于某个会话或者事物的生命周期里,此时临时表中的数据只对当前这个会话可见. 临时表经常被用于存放一个操作的中间数据(数据处理的中间环节). 临时表由于不产生 ...

  3. libsqlite3.dylib找不到

    Xcode7中 Link Binary With Libraries 没有 .dylib库,只能找到对应的 .tbd,但不能代替使用,通过查找资料,尝试后得到以下两种解决方法. 方法1. (heqin ...

  4. hdwiki 编码规范

    编码规范         命名规范 1.1.主要的目录 control 里面是控制类 ,前台命名为something.php,则后台对应的是admin_+前台名称,名称应选有意义的英文单词,例如 前台 ...

  5. gridview 经典

    一.gridview 中弹出超链接 <%--好方法:超链接列中可以直接从数据库获取完整字段,然后选择在当前窗口打开 Target="mainframe" --%> &l ...

  6. HDU 1060 Leftmost Digit

    基本思路:(参考大神和加自己的思考) 考虑到此题需要输入这么大的数a,并且还的求aa,求出来会更大,更多位.当时考虑用大数方法求(数组实现),结果实现不行.看网上大神采用对数法,巧妙避开处理这么大的数 ...

  7. 转 python range 用法

    详细记录python的range()函数用法   使用python的人都知道range()函数很方便,今天再用到他的时候发现了很多以前看到过但是忘记的细节.这里记录一下range(),复习下list的 ...

  8. easyui datagrid 可过滤行的数据表格 导出

    //过滤栏表格导出数据                  /* xukf                 * id datagrid id                 * url Action 路 ...

  9. Ultra-QuickSort 分类: POJ 排序 2015-08-03 15:39 2人阅读 评论(0) 收藏

    Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 48111   Accepted: 17549 ...

  10. IP Address 分类: POJ 2015-06-12 19:34 12人阅读 评论(0) 收藏

    IP Address Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 19125   Accepted: 11053 Desc ...