hdu3577 Fast Arrangement
One train can just take k passangers. And each passanger can just buy one ticket from station a to station b. Each train cannot take more passangers any time. The one who buy the ticket earlier which can be sold will always get the ticket.
The first line contains just one number k( 1 ≤ k ≤ 1000 ) and Q( 1 ≤ Q ≤ 100000 )
The following lines, each line contains two integers a and b, ( 1 ≤ a < b ≤ 1000000 ), indicate a query.
Huge Input, scanf recommanded.
Output the case number in the first line.
If the ith query can be satisfied, output i. i starting from 1. output an blank-space after each number.
Output a blank line after each test case.
3 6
1 6
1 6
3 4
1 5
1 2
2 4
1 2 3 5
这一题属于成段更新,需要用到lazy思想,题意是每个人按时间顺序从a站坐到b站,然后每趟车不能超过k个人,输出能够坐车的人。这里要注意从a站坐到b站,区间更新的是[a,b-1].这里先用线段树维护4个变量,l,r,max,cnt.l,r表示区间的左右端点,max表示这一段区间的最大人数,cnt表示这段的增量,也是lazy标志。
关于cnt和max怎么进行更新,这是个难的地方,想了很久。每次如果询问区间比当前整段区间小,那么把这整段区间的增量加到左右子树的增量上,然后再把增量加到左右子树的最大值上,最后这段区间的增量变为0.
<pre name="code" class="cpp">#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
#define maxn 1000005
int num,k,a[100005];
int max(int a,int b){
return a>b?a:b;
}
struct node{
int l,r,max,cnt;
}b[4*maxn];
void build(int l,int r,int i)
{
int mid;
b[i].l=l;b[i].r=r;b[i].max=b[i].cnt=0;
if(l==r)return;
mid=(l+r)/2;
build(l,mid,i*2);
build(mid+1,r,i*2+1);
}
int question(int l,int r,int i)
{
int mid;
if(b[i].l==l && b[i].r==r){
return b[i].max;
}
if(b[i].cnt){
b[i*2].cnt+=b[i].cnt;b[i*2+1].cnt+=b[i].cnt;
b[i*2].max+=b[i].cnt;b[i*2+1].max+=b[i].cnt;
b[i].cnt=0;
}
mid=(b[i].l+b[i].r)/2;
if(r<=mid)return question(l,r,i*2);
else if(l>mid)return question(l,r,i*2+1);
else return max(question(l,mid,i*2),question(mid+1,r,i*2+1));
}
void update(int l,int r,int i)
{
int mid;
if(b[i].l==l && b[i].r==r){
b[i].cnt++;b[i].max++;return;
}
if(b[i].cnt){
b[i*2].cnt+=b[i].cnt;b[i*2+1].cnt+=b[i].cnt;
b[i*2].max+=b[i].cnt;b[i*2+1].max+=b[i].cnt;
b[i].cnt=0;
}
mid=(b[i].l+b[i].r)/2;
if(r<=mid)update(l,r,i*2);
else if(l>mid)update(l,r,i*2+1);
else {
update(l,mid,i*2);
update(mid+1,r,i*2+1);
}
b[i].max=max(b[i*2].max,b[i*2+1].max);
}
int main()
{
int m,i,j,T,num1=0,c,d,t;
scanf("%d",&T);
while(T--)
{
num1++;t=0;
printf("Case %d:\n",num1);
scanf("%d%d",&k,&m);
build(1,1000005,1);
memset(a,0,sizeof(a));
for(i=1;i<=m;i++){
scanf("%d%d",&c,&d);
d--;
if(question(c,d,1)<k){
a[++t]=i;
update(c,d,1);
}
}
for(i=1;i<=t;i++){
printf("%d ",a[i]);
}
printf("\n\n");
}
return 0;
}
hdu3577 Fast Arrangement的更多相关文章
- HDU - 3577 Fast Arrangement 线段树
Fast Arrangement Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- HDU 3577 Fast Arrangement (线段树区间更新)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3577 题意不好理解,给你数字k表示这里车最多同时坐k个人,然后有q个询问,每个询问是每个人的上车和下车 ...
- Fast Arrangement (线段树,延迟标志)
个人心得:线段树的延迟标志确实是减少了很多时间,思想比较简单,但是实现得时候和建立延迟的时候比较麻烦. 按照我的一些理解,就是更新时找到完全覆盖的区间时,更新延迟标志,不再往下更新,但此时父节点啥的都 ...
- HDU 3577 Fast Arrangement ( 线段树 成段更新 区间最值 区间最大覆盖次数 )
线段树成段更新+区间最值. 注意某人的乘车区间是[a, b-1],因为他在b站就下车了. #include <cstdio> #include <cstring> #inclu ...
- hdu 3577 Fast Arrangement(线段树区间修改,求区间最小值)
Problem Description Chinese always have the railway tickets problem because of its' huge amount of p ...
- HDU 3577Fast Arrangement(线段树模板之区间增减更新 区间求和查询)
Fast Arrangement Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- opencv中的SIFT,SURF,ORB,FAST 特征描叙算子比较
opencv中的SIFT,SURF,ORB,FAST 特征描叙算子比较 参考: http://wenku.baidu.com/link?url=1aDYAJBCrrK-uk2w3sSNai7h52x_ ...
- 基于Fast Bilateral Filtering 算法的 High-Dynamic Range(HDR) 图像显示技术。
一.引言 本人初次接触HDR方面的知识,有描述不正确的地方烦请见谅. 为方便文章描述,引用部分百度中的文章对HDR图像进行简单的描述. 高动态范围图像(High-Dynamic Range,简称HDR ...
- Fast RCNN 训练自己的数据集(3训练和检测)
转载请注明出处,楼燚(yì)航的blog,http://www.cnblogs.com/louyihang-loves-baiyan/ https://github.com/YihangLou/fas ...
随机推荐
- 详解 TCP的三次握手四次挥手
本文转载来自https://blog.csdn.net/qzcsu/article/details/72861891 背景描述 通过上一篇中网络模型中的IP层的介绍,我们知道网络层,可以实现两个主机之 ...
- C语言实现蛇形矩阵
今天大一考试C语言的时候看见了这道题,下面是我转载的一个大佬的博客,自认为分析的很清楚,特来分享一下. **原文地址: https://blog.csdn.net/jack22333/article/ ...
- MySQL 设置保留几天的binlog
1 ) 查看默认的日志保存天数: mysql> show variables like 'expire_logs_days'; +------------------+-------+ | Va ...
- Pandas数据分析练手题(十题)
数据集下载地址:https://github.com/Rango-2017/Pandas_exercises --------------------------------------------- ...
- pidof
pidof 服务名称,就可以查看到服务占用的进程号
- 【数据结构与算法】Java制作一个简单数组类
bobo老师的玩转算法系列–玩转数据结构 简单记录 文章目录 不要小瞧数组 - 制作一个数组类 1 .使用Java中的数组 数组基础 简单使用 2.二次封装属于我们自己的数组 数组基础 制作属于我们自 ...
- 【Oracle】userenv()函数介绍分析
说到这个userenv()使用起来还是很有用的 参数 功能 CLINET_INFO 返回最高可达64个字节存储的用户会话信 ...
- SSRF - Pikachu
概述: SSRF(Server-Side Request Forgery:服务器端请求伪造) 其形成的原因大都是由于服务端提供了从其他服务器应用获取数据的功能,但又没有对目标地址做严格过滤与限制 导致 ...
- C语言变量
C语言变量 任何一种编程语言都离不开变量,特别是数据处理型程序,变量的使用非常频繁,没有变量参与程序甚至无法编制,即使编制运行后的意义也不大.变量之所以重要,是因为变量是编程语言中数据的符号标识和载体 ...
- .NET 项目中的单元测试
.NET 项目中的单元测试 Intro "不会写单元测试的程序员不是合格的程序员,不写单元测试的程序员不是优秀的工程师." -- 一只想要成为一个优秀程序员的渣逼程序猿. 那么问题 ...