题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2795

 #include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <queue>
#include <vector> #define maxn 222222
#define lson l,mid,u<<1
#define rson mid+1,r,u<<1|1
using namespace std; const int INF = 0x3f3f3f;
int h,w,n;
int seg[maxn<<];
int row; int Push_UP(int u){
seg[u] = max(seg[u<<],seg[u<<|]); //****这个地方 u<<1|1 不能换为 u << 1 +1
}
void build(int l,int r,int u){
if(l == r){
seg[u] = w;
return;
}
int mid = (l + r)>>;
build(lson);
build(rson);
Push_UP(u);
}
void Update(int loc,int num,int l,int r,int u){
if(l == r){ // 这个地方要注意!!
seg[u] -= num;
return;
}
int mid = (l + r)/;
if(loc <= mid) Update(loc,num,lson);
else Update(loc,num,rson);
Push_UP(u);
} int Query(int num,int l,int r,int u){
if(seg[u] < num) return -; if(l == r && seg[u] >= num){
return l;
}
int ret = ;
int mid = (l + r)/;
ret = Query(num,lson);
if(ret != -) return ret;
else return Query(num,rson);
}
int main()
{
//if(freopen("input.txt","r",stdin)== NULL) {printf("Error\n"); exit(0);}
while(cin>>h>>w>>n){
if(h > n) h = n;
build(,h,);
while(n--){
int a;
scanf("%d\n",&a);
row = Query(a,,h,);
if(row == -) printf("-1\n");
else{
Update(row,a,,h,);
printf("%d\n",row);
}
}
}
}

参考别人的改进版:

 #include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <queue>
#include <vector> #define maxn 222222
#define lson l,mid,u<<1
#define rson mid+1,r,u<<1|1
using namespace std; const int INF = 0x3f3f3f;
int h,w,n;
int seg[maxn<<]; int Push_UP(int u){
seg[u] = max(seg[u<<],seg[u<<|]); //****这个地方 u<<1|1 不能换为 u << 1 +1
}
void build(int l,int r,int u){
if(l == r){
seg[u] = w;
return;
}
int mid = (l + r)>>;
build(lson);
build(rson);
Push_UP(u);
}
int Query(int num,int l,int r,int u){
if(l == r ){
seg[u] -= num;
return l;
}
int ret = ;
int mid = (l + r)/;
if(seg[u<<] >= num) ret = Query(num,lson);
else ret = Query(num,rson);
Push_UP(u);
return ret;
}
int main()
{
//if(freopen("input.txt","r",stdin)== NULL) {printf("Error\n"); exit(0);}
while(cin>>h>>w>>n){
if(h > n) h = n;
build(,h,);
while(n--){
int num;
scanf("%d\n",&num);
if(seg[]<num) printf("-1\n");
else printf("%d\n",Query(num,,h,));
}
}
}

hdu 2795 线段树的更多相关文章

  1. hdu 2795 线段树(纵向)

    注意h的范围和n的范围,纵向建立线段树 题意:h*w的木板,放进一些1*L的物品,求每次放空间能容纳且最上边的位子思路:每次找到最大值的位子,然后减去L线段树功能:query:区间求最大值的位子(直接 ...

  2. hdu 2795 线段树(二维问题一维化)

    Billboard Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  3. HDU 2795 线段树单点更新

    Billboard Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  4. HDU 2795 线段树区间最大值,单点更新+二分

    Billboard Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  5. HDU 2795 (线段树 单点更新) Billboard

    h*w的木板,放进一些1*L的物品,求每次放空间能容纳且最上边的位子. 每次找能放纸条而且是最上面的位置,询问完以后可以同时更新,所以可以把update和query写在同一个函数里. #include ...

  6. hdu 2795线段树

    #include<stdio.h> #define N 200005 int h,w,n; struct node { int x,y,max; }a]; int mmax(int e,i ...

  7. hdu 5877 线段树(2016 ACM/ICPC Asia Regional Dalian Online)

    Weak Pair Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

  8. hdu 3974 线段树 将树弄到区间上

    Assign the task Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  9. hdu 3436 线段树 一顿操作

    Queue-jumpers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

随机推荐

  1. Java排序8大算法实现

    概述 排序有内部排序和外部排序,内部排序是数据记录在内存中进行排序,而外部排序是因排序的数据很大,一次不能容纳全部的排序记录,在排序过程中需要访问外存. 我们这里说说八大排序就是内部排序. 当n较大, ...

  2. PrintWriter 和 BufferedWriter 写入文件.

    Ref: should I use PrintWriter to wrap BufferedWriter? The main reason for using PrintWriter is the w ...

  3. EntityFramework在root目录web.config中的配置设置

    未找到具有固定名称“System.Data.SqlClient”的 ADO.NET 提供程序的实体框架提供程序.请确保在应用程序配置文件的“entityFramework”节中注册了该提供程序.有关详 ...

  4. CentOS 7重装mysql编译过程报错解决方法

    错误记录: [ 82%] Building C object libmysql/CMakeFiles/clientlib.dir/__/sql-common/client.c.o/usr/local/ ...

  5. [中级] 有效删除URL中的index.php

    如果你刚接触CI不久又或者刚刚研读CI的使用手册的话,关于如何有效删除URL中index.php以使URL看起来更友好美观的问题,可能是你面对的第一个较为复杂的问题!本贴不是原创,而是一个各种意见的综 ...

  6. gulp-htmlmin可以压缩html的gulp插件

    通过一条命令用Npm安装gulp-htmlmin: npm install gulp-htmlmin --save-dev 安装完毕后,打开gulpfile.js文件,我们里面编写一个task用来专门 ...

  7. FileReader上传图片

    实现拖拽图片,在上传至服务器前,显示图片并操控大小 利用HTML5 dragenter dragover dragleave drop 在实现图片显示方面,用了FileReader这个类 var fi ...

  8. openerp 中如何方便对搜索时间段

    以前为了方便的搜索时间区间,经常用wizard对方式,设置开始  结束时间,需要做大量对代码工作, 今天看了search view对组成, 可以用2个filter_domain 来做, 这样用户需要输 ...

  9. asp.net web api long running task

    http://stackoverflow.com/questions/17577016/long-running-task-in-webapi http://blog.stephencleary.co ...

  10. 具有 Button 风格的 Panel(覆盖TCustomPanel的Paint函数,用到了ThemeServices)

    unit Unit2; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...