hdu 2795 线段树
题目链接: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 线段树的更多相关文章
- hdu 2795 线段树(纵向)
注意h的范围和n的范围,纵向建立线段树 题意:h*w的木板,放进一些1*L的物品,求每次放空间能容纳且最上边的位子思路:每次找到最大值的位子,然后减去L线段树功能:query:区间求最大值的位子(直接 ...
- hdu 2795 线段树(二维问题一维化)
Billboard Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 2795 线段树单点更新
Billboard Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 2795 线段树区间最大值,单点更新+二分
Billboard Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 2795 (线段树 单点更新) Billboard
h*w的木板,放进一些1*L的物品,求每次放空间能容纳且最上边的位子. 每次找能放纸条而且是最上面的位置,询问完以后可以同时更新,所以可以把update和query写在同一个函数里. #include ...
- hdu 2795线段树
#include<stdio.h> #define N 200005 int h,w,n; struct node { int x,y,max; }a]; int mmax(int e,i ...
- 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 ...
- hdu 3974 线段树 将树弄到区间上
Assign the task Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 3436 线段树 一顿操作
Queue-jumpers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...
随机推荐
- C#winform程序自定义鼠标样式
public void SetCursor(Bitmap cursor, Point hotPoint) { int hotX = hotPoint.X; int hotY = hotPoint.Y; ...
- Linux命令:head命令详解
概述:head命令用于显示文件文字区块 1.格式 head [参数][文件] 2.参数 -q 隐藏文件名 -v 显示文件名 -c<字节> 显示字节数 -n<行数> 显示的行数 ...
- spl_autoload_register()和__autoload()
关于spl_autoload_register()和__autoload() 看两者的用法: //__autoload用法 function __autoload($classname) { ...
- 小心DriveInfo类IsReady属性的较大延迟问题
当某些驱动器调用IsReady属性来判断是否准备好时,会有性能问题,会非常慢,特别是网络驱动器断开的时候,这个属性会有30秒左右的延迟,这对程序执行是非常大的开销,请慎重调用
- C#DateTimePicker设置自定义格式
摘自Microsoft TechNet DateTimePicker.CustomFormat 属性 包含日期和时间分隔符的显示字符串文字或格式字符串,必须在子字符串中使用转义符. 例如,若要显示将日 ...
- 提供进销存、ERP系统快速开发框架源码 (C#+SQL)
C/S系统开发框架-企业版 V4.0 (Enterprise Edition) 简介: http://www.csframework.com/cs-framework-4.0.htm 视频下载: 百度 ...
- Python中几种数据结构的整理,列表、字典、元组、集合
列表:shoplist = ['apple', 'mango', 'carrot', 'banana']字典:di = {'a':123,'b':'something'}集合:jihe = {'app ...
- HDU 1069 Monkey and Banana(LIS最长上升子序列)
B - LIS Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Descripti ...
- JavaScript的组成—ECMAScript、BOM和DOM
JavaScript 是一种基于 ECMAScript 规范的脚本语言,并在此基础上进行了自己的封装.ECMAScript 不是一种编程语言,仅仅是一种脚本语言规范,由欧洲计算机协会制定和发布,任 ...
- 转:PHP非阻塞模式
你可以任意转摘“PHP非阻塞模式”,但请保留本文出处和版权信息.作者:尘缘,QQ:130775,来源:http://www.4wei.cn/archives/1002336 让PHP不再阻塞当PHP作 ...