标签:
Description
Input
Output
Sample Input
Sample Output
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <set> #include <map> #include <queue> #include <string> #include <algorithm> #define LL long long using namespace std; int h, w, n; //线段树 //区间某点增值,求区间最大值 const int maxn = 200005; struct node { int lt, rt; int val; }tree[4*maxn]; //向上更新 void pushUp(int id) { tree[id].val = max(tree[id<<1].val, tree[id<<1|1].val); } //建立线段树 void build(int lt, int rt, int id) { tree[id].lt = lt; tree[id].rt = rt; tree[id].val = 0;//每段的初值,根据题目要求 if (lt == rt) { tree[id].val = w; return; } int mid = (lt + rt) >> 1; build(lt, mid, id<<1); build(mid + 1, rt, id<<1|1); pushUp(id); } //查询某段区间内的符合p的 int query(int lt, int rt, int id, int p) { if (tree[id].val < p) return -1; if (tree[id].lt == tree[id].rt) { tree[id].val -= p; return tree[id].lt; } int tmp; tmp = query(lt, rt, id<<1, p); if (tmp != -1) { pushUp(id); return tmp; } tmp = query(lt, rt, id<<1|1, p); pushUp(id); return tmp; } void work() { int k, ans; int len = min(h, n); build(1, len, 1); for (int i = 0; i < n; ++i) { scanf("%d", &k); ans = query(1, len, 1, k); printf("%d\n", ans); } } int main() { //freopen("test.in", "r", stdin); while (scanf("%d%d%d", &h, &w, &n) != EOF) { work(); } return 0; }
ACM学习历程—HDU 2795 Billboard(线段树)
标签:
原文地址:http://www.cnblogs.com/andyqsmart/p/4662759.html