设为首页 加入收藏

TOP

poj 2828 线段树插孔处理
2015-11-21 01:02:30 来源: 作者: 【 】 浏览:3
Tags:poj 2828 线段 插孔 处理

给你一个数列出现的先后顺序num【i】和对应数值 输出最后排好序的对应数值,如

?

4
0 77
1 51
1 33
2 69
第一步 77

?

第二部 77 51

第三步 77 33 51

第四部77 33 69 51

后面先出现的位置是固定的 所以从后往前处理。 线段树每个节点存当前区间还有多少个空位;

?

?

#include
  
   
#include
   
     #include
    
      using namespace std; #define LL(x) (x<<1) #define RR(x) ((x<<1)|1) struct node { int cont; }tree[4*200000]; int num[200010][3],queue[200010]; int deal(int L,int R,int point) { tree[point].cont=R-L+1; if(L==R)return 0; int mid=(L+R)/2; deal(L,mid,LL(point)); deal(mid+1,R,RR(point)); return 0; } int update(int L,int R,int pos,int point,int value) { tree[point].cont--; if(L==R) { queue[L]=value; return 0; } int mid=(L+R)/2; if(pos<=tree[LL(point)].cont) update(L,mid,pos,LL(point),value); else update(mid+1,R,pos-tree[LL(point)].cont,RR(point),value); return 0; } int main() { int n,i,j; while(~scanf("%d",&n)) { deal(1,n,1); for(i=1;i<=n;i++) { scanf("%d%d",&num[i][1],&num[i][2]); num[i][1]++; } for(i=n;i>=1;i--) { update(1,n,num[i][1],1,num[i][2]); } for(i=1;i<=n;i++) { if(i!=1) printf(" "); printf("%d",queue[i]); } printf("\n"); } return 0; }
    
   
  

?

?

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇acdream 1726 A Math game (部分.. 下一篇解题报告 之 POJ2891 Strange Way..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: