Fast Arrangement

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3995    Accepted Submission(s): 1141

Problem Description
Chinese always have the railway tickets problem because of its' huge amount of passangers and stations. Now goverment need you to develop a new tickets query system.
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.
 
Input
The input contains servel test cases. The first line is the case number. In each test case:
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
For each test case, output three lines:
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.
 
Sample Input
1
3 6
1 6
1 6
3 4
1 5
1 2
2 4
 
Sample Output
Case 1:
1 2 3 5
 
Author
Louty (Special Thanks Nick Gu)
 
Source
 
Recommend
zhouzeyong   |   We have carefully selected several similar problems for you:  3578 2795 3581 1166 1698 

题目意思:
现在有一辆列车,可以坐k个人
先买到票的就有座位
现在给你q个买票问询:a站到b站
如果此票持有者路上车,则输出问询编号
线段树解决
如果此时a,b区间内最大值是小于k的,那么
说明此人可以上车
可以上车的话
将区间a,b内部的值加1
完全符合线段树的基本操作
区间更新 区间增减
区间查询 找最值
#include<stdio.h>
#include<iostream>
#include<vector>
#include <cstring>
#include <stack>
#include <cstdio>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include<string>
#include<math.h>
using namespace std;
const int max_v=;
int ans[max_v];
struct node
{
int l,r,v,lazy;
}tree[max_v<<];
void build(int l,int r,int root)
{
tree[root].l=l;
tree[root].r=r;
tree[root].v=;
tree[root].lazy=; if(l==r)
return ; int mid=(l+r)>>;
build(l,mid,root<<);
build(mid+,r,root<<|);
}
void push_up(int root)//往上更新父节点数据
{
tree[root].v=max(tree[root<<].v,tree[root<<|].v);// 最值
}
void push_down(int root)//向下往左右儿子方向更新数据
{
tree[root<<].lazy+=tree[root].lazy;
tree[root<<|].lazy+=tree[root].lazy; tree[root<<].v+=tree[root].lazy;
tree[root<<|].v+=tree[root].lazy; tree[root].lazy=;//更新之后lazy清零
}
void update(int l,int r,int root)
{
if(tree[root].l==l&&tree[root].r==r)//如果区间完全重合,则不需要继续往下更新了
{
tree[root].v+=;
tree[root].lazy+=;
return;
}
if(tree[root].lazy)//因为没有找到完全重合的区间,所以要先更新下一层区间;
push_down(root); int mid=(tree[root].l+tree[root].r)>>;
if(l>mid)
update(l,r,root<<|);
else if(r<=mid)
update(l,r,root<<);
else
{
update(l,mid,root<<);
update(mid+,r,root<<|);
} push_up(root);//最后还得往上面更新父节点区间
}
int getmax(int l,int r,int root)//查询区间最值
{
if(tree[root].l==l&&tree[root].r==r)
return tree[root].v; if(tree[root].lazy)//因为没有找到完全重合的区间,所以要先更新下一层区间
push_down(root); int mid=(tree[root].l+tree[root].r)>>;
if(l>mid)
return getmax(l,r,root<<|);
else if(r<=mid)
return getmax(l,r,root<<);
else
{
return max(getmax(l,mid,root<<),getmax(mid+,r,root<<|));
}
}
int main()
{
int t;
scanf("%d",&t);
int c=;
while(t--)
{
memset(ans,,sizeof(ans));
int k,q;
scanf("%d %d",&k,&q);
build(,,);
int m=;
for(int i=;i<q;i++)
{
int a,b;
scanf("%d %d",&a,&b);
b--;
if(getmax(a,b,)<k)
{
ans[m++]=i+;
update(a,b,);
}
}
printf("Case %d:\n",c++);
for(int i=;i<m;i++)
printf("%d ",ans[i]);
printf("\n\n");
}
return ;
}
/*
题目意思:
现在有一辆列车,可以坐k个人
先买到票的就有座位
现在给你q个买票问询:a站到b站
如果此票持有者路上车,则输出问询编号
线段树解决
如果此时a,b区间内最大值是小于k的,那么
说明此人可以上车
可以上车的话
将区间a,b内部的值加1 完全符合线段树的基本操作
区间更新 区间增减
区间查询 找最值
*/

HDU 3577Fast Arrangement(线段树模板之区间增减更新 区间求和查询)的更多相关文章

  1. POJ 3468 A Simple Problem with Integers(线段树模板之区间增减更新 区间求和查询)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 140120 ...

  2. POJ 2155 Matrix (二维线段树入门,成段更新,单点查询 / 二维树状数组,区间更新,单点查询)

    题意: 有一个n*n的矩阵,初始化全部为0.有2中操作: 1.给一个子矩阵,将这个子矩阵里面所有的0变成1,1变成0:2.询问某点的值 方法一:二维线段树 参考链接: http://blog.csdn ...

  3. HDU(1166),线段树模板,单点更新,区间总和

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166 第一次做线段树,帆哥的一句话,我记下来了,其实,线段树就是一种处理数据查询和更新的手段. 然后, ...

  4. HDU 1698 Just a Hook (线段树模板题-区间求和)

    Just a Hook In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of t ...

  5. hdu 4819 二维线段树模板

    /* HDU 4819 Mosaic 题意:查询某个矩形内的最大最小值, 修改矩形内某点的值为该矩形(Mi+MA)/2; 二维线段树模板: 区间最值,单点更新. */ #include<bits ...

  6. hdu1754 I hate it线段树模板 区间最值查询

    题目链接:这道题是线段树,树状数组最基础的问题 两种分类方式:按照更新对象和查询对象 单点更新,区间查询; 区间更新,单点查询; 按照整体维护的对象: 维护前缀和; 维护区间最值. 线段树模板代码 # ...

  7. 线段树:Segment Tree(单点修改/区间修改模板) C++

    线段树是非常有效的数据结构,可以快速的维护单点修改,区域修改,查询最大值,最小值等功能. 同时,它也很重要.如果有一天比赛,你卡在了一道线段树模板题目上,这就真的尴尬了.不过,随着时代的进步,题目也越 ...

  8. hdu 4031 attack 线段树区间更新

    Attack Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)Total Subm ...

  9. HDU 1754:I Hate It(线段树模板)

    I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

随机推荐

  1. yii 修改模块使用的布局文件

    方法一:yii模块默认使用系统当前的主题布局文件,如果在主配置文件中配置了主题比如: 'theme'=>'mythm', 那么yii的模块就使用 protected/themes/mythm/v ...

  2. Thymeleaf模板表达式

    日期格式.组件提取等. ${#dates.format(date)}${#dates.arrayFormat(datesArray)}${#dates.listFormat(datesList)}${ ...

  3. bootstrap前端框架使用总结分享

    1.bootstrap 排版 全局样式style.css: 1.移除body的margin声明 2.设置body的背景色为白色 3.为排版设置了基本的字体.字号和行高 4.设置全局链接颜色,且当链接处 ...

  4. Pwn with File结构体(三)

    前言 本文由 本人 首发于 先知安全技术社区: https://xianzhi.aliyun.com/forum/user/5274 前面介绍了几种 File 结构体的攻击方式,其中包括修改 vtab ...

  5. Vue 框架-03-键盘事件、健值修饰符、双向数据绑定

    Vue 框架-03-键盘时间及健值修饰符 一.键盘事件,当按键盘时,在控制台输出提示 html 源码: <!DOCTYPE html> <html> <head> ...

  6. PyQt4(简单计算器)

    随便写写 import sys import calc from PyQt4 import QtCore, QtGui class MyWidget(QtGui.QWidget): num1 = &q ...

  7. linux之redis

    配置环境变量的命令: 修改环境变量: vim /root/.bash_profile 添加以下配置: export PATH=/server/tools/redis/src:$PATH 激活环境变量 ...

  8. Java对于表达式中的自动类型提升

    1 表达式中的自动类型提升: 表达式求值时,Java自动的隐含的将每个byte.short或char操作数提升为int类型,这些类型的包装类型也是可以的. 例如: short s1 = 1; s1 = ...

  9. SQLSERVER2012里的扩展事件初尝试(上)

    SQLSERVER2012里的扩展事件初尝试(上) SQLSERVER2012里的扩展事件初尝试(下) 周未看了这两篇文章: 扩展事件在Denali CTP3里的新UI(一) 扩展事件在Denali ...

  10. [翻译] NSImage+HHTint - Tints grayscale images using CoreImage

    NSImage+HHTint - Tints grayscale images using CoreImage https://github.com/gloubibou/NSImage-HHTint ...