poj 2828 线段树

来源:互联网 发布:yy网络用语什么意思 编辑:程序博客网 时间:2024/05/24 05:43

表示很有鸭梨~~~~~

题意清楚,应该倒着遍历~~:

参考了别人的代码,并没有完全看懂:

先留着待解决:

#include <iostream>
#define N 800000
using namespace std;
struct node
{
 int maxn,l,r;
}st[N];
int h,w,n;
void build(int v,int l,int r)
{
  st[v].l=l;
  st[v].r=r;
  st[v].maxn=w;
  if(l==r)return;
  int mid=(st[v].l+st[v].r)/2;
  build(2*v,l,mid);
  build(2*v+1,mid+1,r);
}
int find(int v,int d)
{
  if(st[v].maxn<d)return -1;
  if(st[v].l==st[v].r)
   {
    st[v].maxn=st[v].maxn-d;
    return st[v].l;
   }  
  int t=find(2*v,d);
  if(t==-1)t=find(2*v+1,d);
  if(t)
       st[v].maxn=max(st[v*2].maxn,st[v*2+1].maxn);
  return t;
}
int main()
{
 while(scanf("%d%d%d",&h,&w,&n)!=EOF)
 {
   if(h>n)h=n+100;
    build(1,1,h);  
  
   int temp;
   for(int i=0;i<n;i++)
   {
      scanf("%d",&temp);
      int s=find(1,temp); 
      printf("%d\n",s);
   }
   
 }
 
 return 0;  
}

0 0
原创粉丝点击