/*
RMQ
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
#include<math.h>
using namespace std;
const int maxn = ;
struct Node{
int ki,ai,id;
}node[ maxn ];
int dp[ maxn ][ ];
int bit[ ];
int ans[ maxn ][ ]; void init(){
memset( ans,,sizeof( ans ) );
bit[] = ;
for( int i=;i<;i++ )
bit[ i ] = bit[ i- ]*;
return ;
} int cmp( Node a,Node b ){
return a.ki<b.ki;
} void ST( int n ){
int K = (int)(log( 1.0*n )/log( 2.0 ));
for( int i=;i<=n;i++ )
dp[ i ][ ] = i;//存储着i,j之间ai值最小的位置
for( int j=;j<=K;j++ ){
for( int i=;i<=n-bit[j]+;i++ ){
if( node[ dp[i][j-] ].ai<node[ dp[i+bit[j-]][j-] ].ai )
dp[ i ][ j ] = dp[i][j-];
else
dp[ i ][ j ] = dp[i+bit[j-]][j-];
}
}
} int RMQ( int l,int r ){
int k = (int)(log((+r-l)*1.0)/log(2.0));
if( node[ dp[l][k] ].ai<node[ dp[r-bit[k]+][k] ].ai ) return dp[l][k];
else return dp[r-bit[k]+][k];
} int solve( int fa,int l,int r ){
if( l>r ) return ;
int pos = RMQ( l,r );
int id = node[pos].id;
ans[ id ][ ] = fa;
ans[ id ][ ] = solve( id,l,pos- );
ans[ id ][ ] = solve( id,pos+,r );
return id;
} int main(){
int n;
init();
while( scanf("%d",&n)!=EOF ){
for( int i=;i<=n;i++ ){
scanf("%d%d",&node[i].ki,&node[i].ai);
node[i].id = i;
}
sort( node+,node+n+,cmp );
ST( n );
solve( ,,n );
puts("YES");
for( int i=;i<=n;i++ ){
printf("%d %d %d\n",ans[i][],ans[i][],ans[i][]);
}
}
return ;
}
改变了dp中存储的内容
/*
RMQ
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
#include<math.h>
using namespace std;
const int maxn = ;
struct Node{
int ki,ai,id;
}node[ maxn ];
int dp[ maxn ][ ];
int bit[ ];
int ans[ maxn ][ ];
int Ai[ ];
const int MAX = ; void init(){
memset( ans,,sizeof( ans ) );
bit[] = ;
for( int i=;i<;i++ )
bit[ i ] = bit[ i- ]*;
return ;
} int cmp( Node a,Node b ){
return a.ki<b.ki;
} void ST( int n ){
int K = (int)(log( 1.0*n )/log( 2.0 ));
for( int i=;i<=n;i++ )
dp[ i ][ ] = node[i].ai;//存储着i,j之间ai值最小的位置
for( int j=;j<=K;j++ ){
for( int i=;i<=n-bit[j]+;i++ ){
dp[ i ][ j ] = min( dp[i][j-],dp[i+bit[j-]][j-]);
}
}
} int RMQ( int l,int r ){
int k = (int)(log((+r-l)*1.0)/log(2.0));
int value = min( dp[l][k],dp[r-bit[k]+][k] );
return Ai[ value+MAX ];
} int solve( int fa,int l,int r ){
if( l>r ) return ;
int pos = RMQ( l,r );
int id = node[pos].id;
ans[ id ][ ] = fa;
ans[ id ][ ] = solve( id,l,pos- );
ans[ id ][ ] = solve( id,pos+,r );
return id;
} int main(){
int n;
init();
while( scanf("%d",&n)!=EOF ){
for( int i=;i<=n;i++ ){
scanf("%d%d",&node[i].ki,&node[i].ai);
node[i].id = i;
//Ai[ node[i].ai ] = i;
}
sort( node+,node+n+,cmp );
for( int i=;i<=n;i++ )
Ai[ node[i].ai+MAX ] = i;
ST( n );
solve( ,,n );
puts("YES");
for( int i=;i<=n;i++ ){
printf("%d %d %d\n",ans[i][],ans[i][],ans[i][]);
}
}
return ;
}