Poj2201 Cartesian Tree 求调
  • 板块灌水区
  • 楼主cqsunny
  • 当前回复0
  • 已保存回复0
  • 发布时间2025/2/8 12:45
  • 上次更新2025/2/8 15:17:18
查看原帖
Poj2201 Cartesian Tree 求调
726775
cqsunny楼主2025/2/8 12:45
#include<bits/stdc++.h>
using namespace std;
int n;
pair<pair<int, int>, int> a[50010];
int len, st[50010];
int lson[50010], rson[50010], fa[50010];
int tlson[50010], trson[50010], tfa[50010];
inline void read(int &s){
	char x = getchar();
	while(x < '0' || x > '9') x = getchar();
	while(x >= '0' && x <= '9'){
		s = (s << 1) + (s << 3) + x - '0';
		x = getchar();
	}
}
int main(){
	read(n);
	for(int i = 1; i <= n; ++ i){
		read(a[i].first.first), read(a[i].second);
		a[i].first.second = i;
	}
	sort(a + 1, a + n + 1);
	for(int i = 1; i <= n; ++ i){
		while(a[st[len]].second > a[i].second && len){
			-- len;
		} 
		if(len){
			fa[i] = st[len];
			fa[rson[i]] = i;
			lson[i] = rson[st[len]];
			rson[st[len]] = i;
		}
		else{
			lson[i] = st[1];
			fa[st[1]] = i;
		}
		st[++ len] = i;
	}
	for(int i = 1; i <= n; ++ i){
		tfa[a[i].first.second] = a[fa[i]].first.second;
		tlson[a[i].first.second] = a[lson[i]].first.second;
		trson[a[i].first.second] = a[rson[i]].first.second;
	}
	printf("YES\n");
	for(int i = 1; i <= n; ++ i){
		printf("%d %d %d\n", tfa[i], tlson[i], trson[i]);
	}
	return 0;
} 
2025/2/8 12:45
加载中...