hdu2795

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 200000 + 7;
struct node {
    int left, right;
    int val;
} str[maxn*10];
int w, h, n;
int ans = -1;

void BuildTree(int root, int left, int right) {
    int mid = (left + right) / 2;
    str[root].left = left;
    str[root].right = right;
    str[root].val = w;
    if(left == right) return ;
    BuildTree((root << 1), left, mid);
    BuildTree((root << 1) + 1, mid + 1, right);
}

bool Query(int root, int pos) {
    if(str[root].val < pos) return false;
    if(str[root].left == str[root].right) {
        if(str[root].val >= pos) {
            ans = str[root].left;
            str[root].val -= pos;
            return true;   //找到解就退出后面的都不用找了
        }
        return false;
    }
    if(Query(root * 2, pos)) return true; ///<span style="font-family: Arial, Helvetica, sans-serif;">找到解就退出后面的都不用找了</span>
    if(Query(root * 2 + 1, pos)) return true;
    if(str[root<<1].val > str[(root<<1)+1].val) str[root].val = str[root<<1].val;
    else str[root].val = str[(root<<1)+1].val;
    return false;
}

int main() {
    while(scanf("%d%d%d", &h, &w, &n) != EOF) {
        if(h > n) h = n + 7;
        BuildTree(1, 1, h);
        int wi;
        for(int i = 0; i < n; ++i) {
            ans = -1;
            scanf("%d", &wi);
            Query(1, wi);
            printf("%d\n", ans);
        }
    }
    return 0;
}//线段树
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值