笛卡尔树模板求调!玄关
  • 板块学术版
  • 楼主xu_zhihao
  • 当前回复1
  • 已保存回复1
  • 发布时间2025/8/4 17:15
  • 上次更新2025/8/4 21:53:16
查看原帖
笛卡尔树模板求调!玄关
1063855
xu_zhihao楼主2025/8/4 17:15
#include<bits/stdc++.h>
using namespace std;
const int N=5e4+10;
struct node1{
	int a;
	int k;
}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);
	}
	sort(t+1,t+n+1,cmp);
	/*先要按二叉搜索树的顺序查找*/
	int head=1;
	st.push({1,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[i]=head;
			fa[head]=i;
			head=i;
			fa[i]=0;
		}
		else{
			r[(st.top()).id]=i;
			fa[i]=(st.top().id);
			l[i]=last;
			fa[last]=i;
		}
		st.push({i,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
2025/8/4 17:15
加载中...