POJ 2828
1 #include<iostream> 2 #define MAXN 200000 3 #include<stdio.h> 4 using namespace std; 5 6 struct line_tree 7 { 8 int begin; 9 int end; 10 int count; 11 line_tree * left; 12 line_tree * right; 13 }; 14 int index; 15 line_tree node[2*MAXN+1]; 16 int place[MAXN]; 17 int value[MAXN]; 18 int ans[MAXN]; 19 line_tree * make_node(int b,int e) 20 { 21 int mid; 22 line_tree * p; 23 p = &node[index++]; 24 p->count = e - b + 1; 25 p->begin = b; 26 p->end = e; 27 if(b == e) 28 { 29 p->left = NULL; 30 p->right = NULL; 31 return p; 32 } 33 else 34 { 35 mid = (b+e)>>1; 36 p->left = make_node(b,mid); 37 p->right = make_node(mid+1,e); 38 } 39 return p; 40 } 41 42 void insert(line_tree * root,int value,int place) 43 { 44 -- root->count; 45 int mid; 46 if(root->left == NULL && root->right == NULL) 47 { 48 ans[root->begin] = value; 49 } 50 else 51 { 52 if(place < root->left->count) 53 { 54 insert(root->left,value,place); 55 } 56 else 57 { 58 insert(root->right,value,place-root->left->count); 59 } 60 } 61 } 62 63 int main() 64 { 65 //freopen("acm.acm","r",stdin); 66 int num; 67 int i; 68 line_tree * p; 69 while(scanf("%d",&num) != EOF) 70 { 71 index = 0; 72 p = make_node(0,num-1); 73 for(i = 0; i < num; ++ i) 74 { 75 scanf("%d%d",&place[i],&value[i]); 76 //cin>>place[i]>>value[i]; 77 // insert(p,value[i],place[i]); 78 } 79 for(i = num-1; i >= 0; -- i) 80 { 81 insert(p,value[i],place[i]); 82 } 83 for(i = 0; i < num; ++ i) 84 { 85 cout<<ans[i]<<" "; 86 } 87 cout<<endl; 88 } 89 }