http://acm.hdu.edu.cn/showproblem.php?pid=2795
View Code
1 //2795
2 #include <stdio.h>
3 const int N=200010;
4 int st[N*4];
5 int h,w;
6 void newup(int rt)
7 {
8 st[rt]=st[rt*2]>st[rt*2+1]?st[rt*2]:st[rt*2+1];
9 }
10 void build(int l,int r,int rt)
11 {
12 st[rt]=w;
13 if (l==r) return;
14 int m=(l+r)/2;
15 build(l,m,rt*2);
16 build(m+1,r,rt*2+1);
17 }
18 int query(int a,int l,int r,int rt)
19 {
20 if (l==r) {st[rt]-=a; return l;}
21 int m=(l+r)/2,s;
22 if (st[rt*2]>=a) s=query(a,l,m,rt*2);
23 else s=query(a,m+1,r,rt*2+1);
24 newup(rt);
25 return s;
26 }
27 int main()
28 {
29 int n;
30 while (~scanf("%d%d%d",&h,&w,&n))
31 {
32 if (h>n) h=n;
33 build(1,h,1);
34 int x,ans;
35 while (n--)
36 {
37 scanf("%d",&x);
38 ans=st[1]>=x?query(x,1,h,1):-1;
39 printf("%d\n",ans);
40 }
41 }
42 return 0;
43 }