题意:有n个数,每个数进来时求出与它与它最接近的数的编号(多个答案选数小的)
模板测试
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <functional>
#include <cstdlib>
#include <queue>
#include <stack>
#include <set>
using namespace std;
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
typedef long long ll;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+llabs(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
#define
int n;
struct treap{
ll rnd[MAXN],v[MAXN];
int size[MAXN],l[MAXN],r[MAXN],cnt;
void mem() {
MEM(size) MEM(rnd) MEM(l) MEM(r) MEM(v)
cnt=0;
}
void update(int x)
{
size[x]=size[l[x]]+size[r[x]]+1;
}
void rturn(int &k)
{
int t=l[k];l[k]=r[t];r[t]=k;update(k);update(t);k=t;
}
void lturn(int &k)
{
int t=r[k];r[k]=l[t];l[t]=k;update(k);update(t);k=t;
}
int union2(int x,int y) { //new node = unoin2(x,y)
if (!x) return y;if (!y) return x;
if (size[x]<size[y]) swap(x,y);
union2(x,l[y]);union2(x,r[y]);
insert(x,v[y],y);
return x;
}
void insert(int &x,ll rank,int y=0) //insert (x,newnode(rank),0) or insert(x,node[y])
{
if(!x)
{
if (!y) x=++cnt;else {
x=y;
}
v[x]=rank; l[x]=r[x]=0;
rnd[x]=rand();size[x]=1;
return ;
}
size[x]++;
if(v[x]<rank)
{
insert(r[x],rank,y);
if(rnd[r[x]]<rnd[x])lturn(x);
}
else if (v[x]>rank)
{
insert(l[x],rank,y);
if(rnd[l[x]]<rnd[x]) rturn(x);
}return ;
}
// return the pointer
int lower_bound(int x,ll rank) {
int ans=-1;
if (!x) return ans;
if (v[x]<=rank) {
ans=lower_bound(r[x],rank);
if (ans==-1) ans=x;
} else ans=lower_bound(l[x],rank);
return ans;
}
int upper_bound(int x,ll rank) {
int ans=-1;
if (!x) return ans;
if (v[x]>rank) {
ans=upper_bound(l[x],rank);
if (ans==-1) ans=x;
} else ans=upper_bound(r[x],rank);
return ans;
}
void pri(int x){
if (l[x]) pri(l[x]);
cout<<v[x]<<' ';
if (r[x]) pri(r[x]);
}
}T;
int id[MAXN];
int main() {
// freopen("hdu4585.in","r",stdin);
while(scanf("%d",&n)==1&&n) {
T.mem();
int rot=0;
T.insert(rot,1e9);id[1]=1;
Fork(i,2,n+1) {
ll ans=-1;
ll a=read(),b=read();
id[i]=a;
int p1=T.lower_bound(rot,b);
int p2=T.upper_bound(rot,b);
if (p1==-1||p2==-1) ans=max(p1,p2);
else if (b-T.v[p1]<=T.v[p2]-b) ans=p1;
else ans=p2;
printf("%lld %d\n",a,id[ans]);
T.insert(rot,b);
}
}
return 0;
}