您的位置:首页 > 其它

poj2759(单点更新)

2015-09-30 15:49 351 查看
思路:题目说的是有一个h*w的空板子,然后在上面要粘贴1*w的广告。

广告之间是不能重叠的,然后就是如果没有这个广告的位置了,就输出

-1,有的话就输出贴在第几行。

然后就是线段树维护[L,R]之间的最大宽度,初始宽度w。如果这一

行贴了广告的len[L] -= w[i]。这样分析下来就是线段树的单点更新了QWQ。

/*****************************************
Author      :Crazy_AC(JamesQi)
Time        :2015
File Name   :
*****************************************/
// #pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <sstream>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <map>
#include <set>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <limits.h>
using namespace std;
#define MEM(a,b) memset(a,b,sizeof a)
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> ii;
const int inf = 1 << 30;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int maxn = 2e5 + 20;
#define lson rt << 1
#define rson rt << 1 | 1
int h,w,n;
int len[maxn << 2];
inline void pushup(int rt){
len[rt] = max(len[lson],len[rson]);
}
void Build(int L,int R,int rt){
len[rt] = w;
if (L == R) return;
int mid = (L + R) >> 1;
Build(L,mid,lson);
Build(mid + 1,R,rson);
}
int Query(int L,int R,int rt,int x){
if (L == R) {
len[rt] -= x;
return L;
}
int mid = (L + R) >> 1;
int ret = (len[lson] >= x?Query(L,mid,lson,x):Query(mid + 1,R,rson,x));
pushup(rt);
return ret;
}
int main()
{
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
int x;
while(~scanf("%d %d %d",&h,&w,&n)){
h = min(h,n);
Build(1,h,1);
for (int i = 1;i <= n;i++){
scanf("%d",&x);
if (x > len[1]){
puts("-1");
continue;
}
printf("%d\n",Query(1,h,1,x));
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: