Codeforces Round #135 (Div. 2) E. Parking Lot 线段数区间合并
2 seconds
256 megabytes
standard input
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.
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 ti, idi (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.
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.
7 11
1 15
1 123123
1 3
1 5
2 123123
2 15
1 21
2 3
1 6
1 7
1 8
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 线段数区间合并的更多相关文章
- 树形DP Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland
题目传送门 /* 题意:求一个点为根节点,使得到其他所有点的距离最短,是有向边,反向的距离+1 树形DP:首先假设1为根节点,自下而上计算dp[1](根节点到其他点的距离),然后再从1开始,自上而下计 ...
- 构造 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 ...
- 贪心 Codeforces Round #135 (Div. 2) C. Color Stripe
题目传送门 /* 贪心:当m == 2时,结果肯定是ABABAB或BABABA,取最小改变量:当m > 2时,当与前一个相等时, 改变一个字母 同时不和下一个相等就是最优的解法 */ #incl ...
- Codeforces Round #603 (Div. 2) E. Editor(线段树)
链接: https://codeforces.com/contest/1263/problem/E 题意: The development of a text editor is a hard pro ...
- Codeforces Round #135 (Div. 2)
A. k-String 统计每个字母出现次数即可. B. Special Offer! Super Price 999 Bourles! 枚举末尾有几个9,注意不要爆掉\(long\ long\)的范 ...
- 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 ...
- 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 ...
- Codeforces Round #135 (Div. 2) D - Choosing Capital for Treeland(两种树形DP)
- 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 ...
随机推荐
- linux设备驱动归纳总结(十二):简单的数码相框【转】
本文转载自:http://blog.chinaunix.net/uid-25014876-id-116926.html linux设备驱动归纳总结(十二):简单的数码相框 xxxxxxxxxxxxxx ...
- 如何自动生成Facade 的EJB
1.jbuilder中连接数据库,注意:java:/DataSource 2.选择数据表,右健选择"create cmp 2.x..." 3.添加"findAll&quo ...
- Understanding the RelationshipType Enumeration [AX 2012]
Understanding the RelationshipType Enumeration [AX 2012] 3 out of 3 rated this helpful - Rate this t ...
- HDU 3076:ssworld VS DDD(概率DP)
http://acm.split.hdu.edu.cn/showproblem.php?pid=3076 ssworld VS DDD Problem Description One day, s ...
- SQLServer学习笔记<>sql的范围内查找,sql数据类型,字符串处理函数
sql的范围内查找 (1)between.....and用法 通常情况下我们查找一个在某固定区域内的所有记录,可以采用>=,<=来写sql语句,例如:查找订单价格在1000到2000之间的 ...
- StringUtils中 isEmpty 和isBlank的区别
StringUtils方法的操作对象是java.lang.String类型的对象,是JDK提供的String类型操作方法的补充,并且是null安全的(即如果输入参数String为null则不会抛出Nu ...
- c# TCPclient
服务端 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; ...
- JDBC批量Insert深度优化(有事务)
环境: MySQL 5.1 RedHat Linux AS 5 JavaSE 1.5 DbConnectionBroker 微型数据库连接池 测试的方案: 执行10万次Insert语句,使用不同方 ...
- The Blacksmith学习的相关资源
1.Unity官网的Blacksmith主页 https://unity3d.com/pages/the-blacksmith 2.WRINKLE MAPS IN THE BLACKSMITH 褶皱贴 ...
- 2016 Al-Baath University Training Camp Contest-1 B
Description A group of junior programmers are attending an advanced programming camp, where they lea ...