HDU 2795 线段树 水题

来源:互联网 发布:林非比淘宝模特 编辑:程序博客网 时间:2024/06/14 15:19

水题

从左至右找到位置就插入即可

 

#include "stdio.h"
#include "string.h"
#include "math.h"
#include "stdlib.h"
#include "algorithm"
#include "iostream"
using namespace std;

int w;
struct comp
{
 int l,r,mid,max;
} data[600010];

int max(int a,int b)
{
 if (a<b) return b;
 else return a;
}

void build(int l,int r,int k)
{
 data[k].l=l;
 data[k].r=r;
 data[k].mid=(l+r)/2;
 data[k].max=w;

 if (l==r) return ;

 build(l,data[k].mid,k*2);
 build(data[k].mid+1,r,k*2+1);
}

void search(int x,int k)
{
 if (data[k].l==data[k].r)
 {
  printf("%d\n",data[k].l);
  data[k].max-=x;
  return ;
 }

 if (data[k*2].max>=x) search(x,k*2);
 else
  search(x,k*2+1);

 data[k].max=max(data[k*2].max,data[k*2+1].max);
}

 

int main()
{
 int h,n,x,m;
 while (scanf("%d%d%d",&h,&w,&n)!=EOF)
 {
  if (h<n) m=h; else m=n; // 缩小树的范围
  build(1,m,1);
  while (n--)
  {
   scanf("%d",&x);
   if (data[1].max<x) printf("-1\n");
   else search(x,1);
  }
 }
 return 0;
}

原创粉丝点击