POJ2201+RMQ
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /* 2 RMQ 3 */ 4 #include<stdio.h> 5 #include<string.h> 6 #include<stdlib.h> 7 #include<algorithm> 8 #include<iostream> 9 #include<math.h> 10 using namespace std; 11 const int maxn = 50005; 12 struct Node{ 13 int ki,ai,id; 14 }node[ maxn ]; 15 int dp[ maxn ][ 24 ]; 16 int bit[ 32 ]; 17 int ans[ maxn ][ 3 ]; 18 19 void init(){ 20 memset( ans,0,sizeof( ans ) ); 21 bit[0] = 1; 22 for( int i=1;i<32;i++ ) 23 bit[ i ] = bit[ i-1 ]*2; 24 return ; 25 } 26 27 int cmp( Node a,Node b ){ 28 return a.ki<b.ki; 29 } 30 31 void ST( int n ){ 32 int K = (int)(log( 1.0*n )/log( 2.0 )); 33 for( int i=1;i<=n;i++ ) 34 dp[ i ][ 0 ] = i;//存储着i,j之间ai值最小的位置 35 for( int j=1;j<=K;j++ ){ 36 for( int i=1;i<=n-bit[j]+1;i++ ){ 37 if( node[ dp[i][j-1] ].ai<node[ dp[i+bit[j-1]][j-1] ].ai ) 38 dp[ i ][ j ] = dp[i][j-1]; 39 else 40 dp[ i ][ j ] = dp[i+bit[j-1]][j-1]; 41 } 42 } 43 } 44 45 int RMQ( int l,int r ){ 46 int k = (int)(log((1+r-l)*1.0)/log(2.0)); 47 if( node[ dp[l][k] ].ai<node[ dp[r-bit[k]+1][k] ].ai ) return dp[l][k]; 48 else return dp[r-bit[k]+1][k]; 49 } 50 51 int solve( int fa,int l,int r ){ 52 if( l>r ) return 0; 53 int pos = RMQ( l,r ); 54 int id = node[pos].id; 55 ans[ id ][ 0 ] = fa; 56 ans[ id ][ 1 ] = solve( id,l,pos-1 ); 57 ans[ id ][ 2 ] = solve( id,pos+1,r ); 58 return id; 59 } 60 61 int main(){ 62 int n; 63 init(); 64 while( scanf("%d",&n)!=EOF ){ 65 for( int i=1;i<=n;i++ ){ 66 scanf("%d%d",&node[i].ki,&node[i].ai); 67 node[i].id = i; 68 } 69 sort( node+1,node+n+1,cmp ); 70 ST( n ); 71 solve( 0,1,n ); 72 puts("YES"); 73 for( int i=1;i<=n;i++ ){ 74 printf("%d %d %d\n",ans[i][0],ans[i][1],ans[i][2]); 75 } 76 } 77 return 0; 78 }
改变了dp中存储的内容
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /* 2 RMQ 3 */ 4 #include<stdio.h> 5 #include<string.h> 6 #include<stdlib.h> 7 #include<algorithm> 8 #include<iostream> 9 #include<math.h> 10 using namespace std; 11 const int maxn = 50005; 12 struct Node{ 13 int ki,ai,id; 14 }node[ maxn ]; 15 int dp[ maxn ][ 24 ]; 16 int bit[ 32 ]; 17 int ans[ maxn ][ 3 ]; 18 int Ai[ 70001 ]; 19 const int MAX = 30000; 20 21 void init(){ 22 memset( ans,0,sizeof( ans ) ); 23 bit[0] = 1; 24 for( int i=1;i<32;i++ ) 25 bit[ i ] = bit[ i-1 ]*2; 26 return ; 27 } 28 29 int cmp( Node a,Node b ){ 30 return a.ki<b.ki; 31 } 32 33 void ST( int n ){ 34 int K = (int)(log( 1.0*n )/log( 2.0 )); 35 for( int i=1;i<=n;i++ ) 36 dp[ i ][ 0 ] = node[i].ai;//存储着i,j之间ai值最小的位置 37 for( int j=1;j<=K;j++ ){ 38 for( int i=1;i<=n-bit[j]+1;i++ ){ 39 dp[ i ][ j ] = min( dp[i][j-1],dp[i+bit[j-1]][j-1]); 40 } 41 } 42 } 43 44 int RMQ( int l,int r ){ 45 int k = (int)(log((1+r-l)*1.0)/log(2.0)); 46 int value = min( dp[l][k],dp[r-bit[k]+1][k] ); 47 return Ai[ value+MAX ]; 48 } 49 50 int solve( int fa,int l,int r ){ 51 if( l>r ) return 0; 52 int pos = RMQ( l,r ); 53 int id = node[pos].id; 54 ans[ id ][ 0 ] = fa; 55 ans[ id ][ 1 ] = solve( id,l,pos-1 ); 56 ans[ id ][ 2 ] = solve( id,pos+1,r ); 57 return id; 58 } 59 60 int main(){ 61 int n; 62 init(); 63 while( scanf("%d",&n)!=EOF ){ 64 for( int i=1;i<=n;i++ ){ 65 scanf("%d%d",&node[i].ki,&node[i].ai); 66 node[i].id = i; 67 //Ai[ node[i].ai ] = i; 68 } 69 sort( node+1,node+n+1,cmp ); 70 for( int i=1;i<=n;i++ ) 71 Ai[ node[i].ai+MAX ] = i; 72 ST( n ); 73 solve( 0,1,n ); 74 puts("YES"); 75 for( int i=1;i<=n;i++ ){ 76 printf("%d %d %d\n",ans[i][0],ans[i][1],ans[i][2]); 77 } 78 } 79 return 0; 80 }
keep moving...