HDU2795线段树入门 简单查询和修改

时间:2023-03-09 15:39:15
HDU2795线段树入门 简单查询和修改

http://acm.hdu.edu.cn/showproblem.php?pid=2795

#include<iostream>
using namespace std;
const int n=1e6+;
int h,w,x;
struct node
{
int r;
int l;
int res;
};
node tree[n*];
void build(int root,int l,int r)
{
tree[root].l=l;
tree[root].r=r;
tree[root].res=w;
if(l==r)
{
return ;
}
int mid=(l+r)>>;
build(root<<,l,mid);
build(root<<|,mid+,r);
}
int query(int root,int key)
{
if(tree[root].l==tree[root].r)
{
tree[root].res-=key;
return tree[root].r;
}
int c;
if(tree[root<<].res>=key) c=query(root<<,key);
else c=query(root<<|,key);
tree[root].res=max(tree[root<<].res,tree[root<<|].res);
// cout<<c<<endl;
return c;
}
int main(){
while(~scanf("%d%d%d",&h,&w,&x)){
if(h>x) h=x;
build(,,h);
while(x--)
{
int value;
scanf("%d",&value);
if(tree[].res<value) printf("-1\n");
else printf("%d\n",query(,value));
}
}
}