#include<bits/stdc++.h>
using namespace std;
const int N=5e4+10;
struct node1{
int a;
int k;
int id;
}t[N];
struct node2{
int id;
int w;
};
bool cmp(node1 x,node1 y){
return x.k<y.k;
}
int l[N],r[N],fa[N];
stack<node2>st;
int main(){
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d%d",&t[i].a,&t[i].k);
t[i].id=i;
}
sort(t+1,t+n+1,cmp);
/*先要按二叉搜索树的顺序查找*/
int head=1;
st.push({t[1].id,t[1].a});
for(int i=2;i<=n;i++){
int last=0;
while(!st.empty() && (st.top()).w>t[i].a){
last=(st.top()).id;
st.pop();
}
if(st.empty()){
l[t[i].id]=head;
fa[head]=t[i].id;
head=t[i].id;
fa[t[i].id]=0;
}
else{
r[(st.top()).id]=i;
fa[i]=(st.top().id);
l[t[i].id]=last;
fa[last]=t[i].id;
}
st.push({t[i].id,t[i].a});
}
printf("YES\n");
for(int i=1;i<=n;i++){
printf("%d %d %d\n",fa[i],l[i],r[i]);
}
return 0;
}
不是luogu的题目
样例
输入
7
5 4
2 2
3 9
0 5
1 3
6 6
4 11
输出
YES
2 3 6
0 5 1
1 0 7
5 0 0
2 4 0
1 0 0
3 0 0