hdu 2795 Billboard

    技术2022-05-19  19

    继续刷线段树,每个节点记录区间内的最大值,不断往左搜,搜不下去就往右搜,然后根据左右子树更新那个最大值

    #include<iostream> using namespace std; #define N 200005 struct node { int l,r,m; }; node st[N*4]; int result[N]; int mymax(int a,int b) { return a>b?a:b; } int mymin(int a,int b) { return a<b?a:b; } void build(int l,int r,int i,int w) { st[i].l=l,st[i].r=r,st[i].m=w; if(l==r) return ; int mid=(l+r)/2; build(l,mid,2*i,w); build(mid+1,r,2*i+1,w); return ; } int update(int i,int a) { if(st[i].m<a) return 0; if(st[i].l==st[i].r) { st[i].m-=a; return st[i].l; } int t=update(2*i,a); if(!t) t=update(2*i+1,a); st[i].m=mymax(st[2*i].m,st[i*2+1].m); return t; } int main() { int h,w,n,i,a; while(scanf("%d%d%d",&h,&w,&n)!=EOF) { build(1,mymin(n,h),1,w); for(i=1;i<=n;i++) { scanf("%d",&a); int t=update(1,a); if(t) result[i]=t; else result[i]=-1; } for(i=1;i<=n;i++) printf("%d/n",result[i]); } return 0; }


    最新回复(0)