hdu2795 Billboard(单点更新)

来源:互联网 发布:如何检查网络是否丢包 编辑:程序博客网 时间:2024/05/24 06:19


这里h是宽,w是长,刚开始老是翻不过来。。

然后要想清楚,这里处理的是宽度,所以以宽度作为线段建树。注意优化,因为最坏情况也就每行贴一张告示(告示宽度为1),所以树的最长宽度也就n,不处理这一步,这个树就建不起来,所以运行错误。


#include <stdio.h>#include <algorithm>#include <stdlib.h>#include <string.h>#include <iostream>#include <math.h>using namespace std;typedef long long LL;const int N = 200010;const int INF = 1e8;struct line{    int l;    int r;    int value;}tree[4 * N];int ans;void build(int i, int l, int r, int val){    tree[i].l = l;    tree[i].r = r;    tree[i].value = val;    if(l == r)    {        return;    }    int mid = (l + r) >> 1;    build(i*2, l, mid, val);    build(i*2+1, mid+1, r, val);}void update(int i, int x){    if(tree[i].l == tree[i].r)    {        tree[i].value -= x;        ans = tree[i].l;        return;    }    if(tree[2*i].value >= x) update(i*2, x);//判断左子树是否可以走    else update(i*2+1, x);    tree[i].value = max(tree[i * 2].value, tree[i * 2 + 1].value);}int main(){   // freopen("in.txt", "r", stdin);    int h, w, n, x;    while(~scanf("%d%d%d", &h, &w, &n))    {        if(h > n) h = n;        build(1, 1, h, w);        ans = -1;        for(int i = 0; i < n; i++)        {            scanf("%d", &x);            if(tree[1].value >= x) update(1, x);            printf("%d\n", ans);            ans = -1;        }    }    return 0;}


0 0