poj 2201 笛卡尔树

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<sstream>
#include<string>
#include<climits>
#include<stack>
#include<set>
#include<bitset>
#include<cmath>
#include<deque>
#include<map>
#include<queue>
#define iinf 2000000000
#define linf 1000000000000000000LL
#define dinf 1e200
#define all(v) (v).begin(),(v).end()
#define sz(x) x.size()
#define pb push_back
#define mp make_pair
#define lng long long
#define sqr(a) ((a)*(a))
#define pii pair<int,int>
#define pll pair<lng,lng>
#define pss pair<string,string>
#define pdd pair<double,double>
#define X first
#define Y second
#define pi 3.14159265359
#define ff(i,xi,n) for(int i=xi;i<=(int)(n);++i)
#define ffd(i,xi,n) for(int i=xi;i>=(int)(n);--i)
#define ffl(i,r) for(int i=head[r];i!=-1;i=edge[i].next)
#define cc(i,j) memset(i,j,sizeof(i))
#define two(x)	 ((lng)1<<(x))
#define N 55555
#define M 105000
#define MAXN 100
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
using namespace std;
typedef vector<int> vi;
typedef vector<string> vs;
typedef unsigned int uint;
typedef unsigned lng ulng;
template<class T> inline void checkmax(T &x,T y){if(x<y) x=y;}
template<class T> inline void checkmin(T &x,T y){if(x>y) x=y;}
template<class T> inline T Min(T x,T y){return (x>y?y:x);}
template<class T> inline T Max(T x,T y){return (x<y?y:x);}
template<class T> T gcd(T a,T b){return (a%b)==0?b:gcd(b,a%b);}
template<class T> T lcm(T a,T b){return a*b/gcd(a,b);}
template<class T> T Abs(T a){return a>0?a:(-a);}
struct pp{int v,w,next;}edge[M];int tot=0,n,m,dis[N],ncase,head[N],euler[2*N],pos[N],d[N],dp[2*N][32],y,a[N];
inline void addedge(int u,int v,int w,int *h){edge[tot].v=v,edge[tot].w=w,edge[tot].next=h[u],h[u]=tot++;}
struct node
{
int key;
int value;
int id;
node(int _key=0,int _value=0,int _id=0):key(_key),value(_value),id(_id){}
}q[N];
struct knode
{
int l,r,fa;
knode(int _l=0,int _r=0,int _fa=0):l(_l),r(_r),fa(_fa){}
}tree[N];
bool cmp(node p1,node p2)
{
return p1.key<p2.key;
}
int Stack[N],top;
int main()
{
#ifdef DEBUG
// freopen("data.in","r",stdin);
// freopen("data.out","w",stdout);
#endif
while(scanf("%d",&n)==1)
{
ff(i,1,n)
{
scanf("%d%d",&q[i].key,&q[i].value);
q[i].id=i;
}
sort(q+1,q+n+1,cmp);
ff(i,1,n)
pos[q[i].id]=i;
top=0;
cc(tree,0);
ff(i,1,n)
{
int w=q[i].value,t=-1;
while(top>=1&&q[Stack[top]].value>w) t=Stack[top],top--;
if(top>=1) tree[Stack[top]].r=i,tree[i].fa=Stack[top];
if(q[t].value>w) tree[i].l=t,tree[t].fa=i;
Stack[++top]=i;
}
printf("YES\n");
// ff(i,1,n)
// printf("%d %d %d\n",q[tree[i].fa].key,q[tree[i].l].key,q[tree[i].r].key);
ff(i,1,n)
{
int id=pos[i];
printf("%d %d %d\n",q[tree[id].fa].id,q[tree[id].l].id,q[tree[id].r].id);
}
}
return 0;
}
/*
made by qinggege
*/

你可能感兴趣的:(poj 2201 笛卡尔树)