poj 2182 || hdu 2711 Lost Cows

来源:互联网 发布:西门子plc300编程手册 编辑:程序博客网 时间:2024/05/03 19:30

同POJ 2828。都是从往前推,插空位即可。

#include <queue>#include <stack>#include <math.h>#include <stdio.h>#include <stdlib.h>#include <iostream>#include <limits.h>#include <string.h>#include <string>#include <algorithm>#define MID(x,y) ( ( x + y ) >> 1 )#define L(x) ( x << 1 )#define R(x) ( x << 1 | 1 )#define BUG puts("here!!!")using namespace std;const int MAX = 8010;struct Tnode{int l,r,rs,ls;};Tnode node[MAX << 2];int a[MAX];int b[MAX],POS;void init(){memset(a,0,sizeof(a));memset(b,-1,sizeof(b));memset(node,0,sizeof(node));}void Updata_sum(int t){node[t].ls = node[L(t)].ls + node[L(t)].rs;node[t].rs = node[R(t)].ls + node[R(t)].rs;}void Build(int t,int l,int r){node[t].l = l; node[t].r = r;if( node[t].l == node[t].r - 1 ){node[t].ls = 1;node[t].rs = 0;return ;}int mid = MID(l,r);Build(L(t),l,mid);Build(R(t),mid,r);Updata_sum(t);}void Updata(int t,int len){if( node[t].l == node[t].r - 1 ){node[t].ls = 0;POS = node[t].l;return ;}if( len <= node[t].ls )Updata(L(t),len);elseUpdata(R(t),len-node[t].ls);Updata_sum(t);}int main(){int n;while( ~scanf("%d",&n) ){init();for(int i=1; i<n; i++)scanf("%d",&a[i]);Build(1,0,n);for(int i=n-1; i>=0; i--){Updata(1,a[i]+1);b[i] = POS;}for(int i=0; i<n; i++)printf("%d\n",b[i]+1);}return 0;}