Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

路过飘过,贴代码

Posted by peijian at 2012-03-04 09:56:39 on Problem 2201
#include <iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=50005;
int a[maxn];
int c[maxn][20];
struct node{
    int x,y,pos,P,L,R;
    node(int a=0):pos(a),P(0),L(0),R(0){}
}p[maxn];
void RMQ(int n){
    for(int j=1,k=2;k<=n;j++,k<<=1){
        for(int i=1;i+k-1<=n;i++){
            c[i][j]=p[c[i][j-1]].y<p[c[i+(k>>1)][j-1]].y?c[i][j-1]:c[i+(k>>1)][j-1];
        }
    }
}
int query(int L,int R){
    if(L>R)return 0;
    int len=R-L+1;
    int j,k;
    for(j=0,k=1;k<=len;j++,k<<=1);
    j--,k>>=1;
    //cout<<c[L][j-1]<<" "<<c[R-k+1][j-1]<<endl;
    return p[c[L][j]].y<p[c[R-k+1][j]].y?c[L][j]:c[R-k+1][j];
}
int cmp(const void*a,const void*b){
    struct node*t1=(node*)a;
    struct node*t2=(node*)b;
    return t1->x-t2->x;
}
int cmp1(const void*a,const void*b){
    struct node*t1=(node*)a;
    struct node*t2=(node*)b;
    return t1->pos-t2->pos;
}
bool BuildTree(int L,int R){
    if(L<=R){
        int mindex=query(L,R);
        //cout<<p[mindex].pos<<" "<<p[mindex].x<<" "<<p[mindex].y<<endl;
        int mL=query(L,mindex-1);
        if(mL!=0){
            BuildTree(L,mindex-1);
            if(p[mL].x>p[mindex].x)return false;
            p[mindex].L=p[mL].pos;
            p[mL].P=p[mindex].pos;
        }
        int mR=query(mindex+1,R);
        if(mR!=0){
            BuildTree(mindex+1,R);
            if(p[mR].x<p[mindex].x)return false;
            p[mindex].R=p[mR].pos;
            p[mR].P=p[mindex].pos;
        }
        return true;
    }
    //return false;
}
int main()
{
   // freopen("in.txt","r",stdin);
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d%d",&p[i].x,&p[i].y);
        p[i].pos=i;
    }
    qsort(p+1,n,sizeof(p[0]),cmp);
    for(int i=1;i<=n;i++){
        c[i][0]=i;
    }
    RMQ(n);
    bool f=BuildTree(1,n);
    if(!f){
        printf("NO\n");
    }
    else {
        printf("YES\n");
        qsort(p+1,n,sizeof(p[0]),cmp1);
        for(int i=1;i<=n;i++){
            printf("%d %d %d\n",p[i].P,p[i].L,p[i].R);
        }
    }
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator