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. 修改Linux时间一般涉及到3个命令: date, clock, hwclock

    原贴:http://203.208.37.104/search?q=cache:p1vAAHvs9ikJ:www.goldthe.com /blog/%3Faction%3Dshowlog%26gid ...

  2. discuz搬家后报错SQL:SELECT value FROM [Table]vars WHERE name=’noteexists1′的解决办法

    基本上每个站长都会经历网站搬家,网站搬家中有时候就会遇到这次提到的这个错误数据库报错::SQL:SELECT value FROM [Table]vars WHERE name=\\\'noteexi ...

  3. ECshop中TemplateBeginEditable 和后台编辑讲解

    在ecshop的dwt文件里面经常发现有“<!-- TemplateBeginEditable name="doctitle" -->和<!-- #BeginLi ...

  4. 获取SqlServer2005表结构(字段,主键,外键,递增,描述)

    1.获取表的基本字段属性 --获取SqlServer中表结构 SELECT syscolumns.name,systypes.name,syscolumns.isnullable, syscolumn ...

  5. SlickGrid example 1: 最简单的例子和用法

    SlickGrid例子和用法 开始学习使用SlickGrid,确实挺好用,挺方便的. 官网地址: https://github.com/mleibman/SlickGrid 不多说,先上效果图. 上代 ...

  6. 在javaEE下学习web(在eclipse中开发动态的WEB工程,servlet的环境搭建,及servlet的一些方法)

    一个简便的方法实现javaee版的eclipse开发动态的WEB工程(javaWEB项目)1.把开发选项切换到javaEE2. 可以在window->shou view 中找到package e ...

  7. Wall(凸包POJ 1113)

    Wall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 32360 Accepted: 10969 Description On ...

  8. Epoll,Poll,Select模型比较

    http://blog.csdn.net/liangyuannao/article/details/7776057 先说Select: 1.Socket数量限制:该模式可操作的Socket数由FD_S ...

  9. firefox 最新版地址栏后没有生成二维码的工具

    下载火狐离线安装包50.0.2版本,安装后找不到这个图标了,搜索下载附加组件管理器cpmanager-1.2.13.xpi,提示没有签名无法安装,最后下载了一个较高版本的cpmanager-1.5.5 ...

  10. Git学习笔记03--git reset

    摘自<Git权威指南> Git reset 是Git最常用的命令之一,也是最危险最容易误用的命令. 用法一:git reset [-q] [<commit>] [--] < ...