Codeforces Round #135 (Div. 2) E. Parking Lot 线段数区间合并
2016-10-22 19:24
375 查看
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 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.
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
output
思路:区间合并;搓代码;
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 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.
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\n",pos); mp[x]=pos; } else { update(mp[x],1,1,n,1); mp[x]=0; } } return 0; }
相关文章推荐
- Codeforces Round #223 (Div. 2) E. Sereja and Brackets 线段树区间合并
- CF-46D-Parking Lot(线段树,区间合并,点表示线段)
- 【Codeforces】Codeforces Round #299 (Div. 1) E. Tavas on the Path 【树链剖分+区间合并】
- Codeforces Round #222 (Div. 1) D. Developing Game 线段树有效区间合并
- ZOJ 3324 Machine 线段数 区间合并
- 【Codeforces Round 370 (Div 2) E】【线段树 等比数列 区间合并】Memory and Casinos 赌场区间[l,r] l进r先出的概率
- hdu 1540 Tunnel Warfare 线段数区间合并
- HDU1540 Tunnel Warfare(线段树区间合并 | 线段树新姿势)
- Hdu 3397 Sequence operation 区间合并+区间更新+翻转操作
- HDU 2871 Memory Control 区间合并+区间更新
- 洛谷 P2894 [USACO08FEB]酒店Hotel(线段树区间合并)
- CF380C. Sereja and Brackets[线段树 区间合并]
- Codeforces Round #333 (Div. 2) B. Approximating a Constant Range (线段树区间最值)
- 线段树区间合并——POJ 3667
- HDOJ 3911 Black And White 线段树 区间合并 成段更新
- HDU3397 Sequence operation(线段树的区间合并)
- POJ -2892 Tunnel Warfare - 线段树区间合并
- 51nod 1562 玻璃切割(线段树区间合并)
- HDU 3308【线段树-query:区间最长单调上升序列,update:结点更新,区间合并】
- hdu 3397(区间合并)