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.
InputThe 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.OutputFor 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
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iomanip>
#include<algorithm>
using namespace std;
#define inf 1<<29
#define nu 4000005
#define maxnum 1000000
int n,k;
int maxelem;
int book[];
int flag=;
typedef struct
{
int left,right;
int sum;
int jf;
int mid(){
return (left+right)/;
} }Tree;
Tree tree[nu];
void buildtree(int root,int l,int r){
tree[root].left=l,tree[root].right=r;
tree[root].sum=tree[root].jf=;
if(l!=r){
buildtree(root*+,l,(l+r)/);
buildtree(root*+,(l+r)/+,r);
}
}
void updown(int root)
{ tree[root*+].jf+=tree[root].jf;
tree[root*+].jf+=tree[root].jf;
tree[root*+].sum+=tree[root].jf;
tree[root*+].sum+=tree[root].jf;
tree[root].jf=;
}
void upset(int root)
{
tree[root].sum=max(tree[root*+].sum,tree[root*+].sum);
}
int checktree(int root,int l,int r)
{
if(tree[root].left==l&&tree[root].right==r)
{
return tree[root].sum;
}
int mid=tree[root].mid();
if(tree[root].jf) updown(root);
if(r<=mid)
return checktree(root*+,l,r);
else if(l>mid)
return checktree(root*+,l,r);
else
{
return max(checktree(root*+,l,mid),checktree(root*+,mid+,r));
}
}
void inserttree(int root,int l,int r){
if(tree[root].left==l&&tree[root].right==r)
{
tree[root].jf+=;
tree[root].sum+=;
return ;
}
int mid=tree[root].mid();
if(tree[root].jf) updown(root);
if(r<=mid)
inserttree(root*+,l,r);
else if(l>mid)
inserttree(root*+,l,r);
else
{
inserttree(root*+,l,mid);
inserttree(root*+,mid+,r);
}
upset(root);
}
int main()
{
int t,j;
scanf("%d",&t);
for(j=;j<=t;j++){
scanf("%d%d",&k,&n);
buildtree(,,maxnum);
flag=;
memset(book,,sizeof(book));
for(int i=;i<=n;i++){
int a,b;
scanf("%d%d",&a,&b);
b--;
if(checktree(,a,b)<k){
book[flag++]=i;
inserttree(,a,b);
}
}
printf("Case %d:\n",j);
for(int p=;p<flag;p++)
{
printf("%d ",book[p]);
}
printf("\n\n");
}
return ;
}
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个询问,每个询问是每个人的上车和下车 ...
- 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 ...
- zoj 1610 Count the Colors(线段树延迟更新)
所谓的懒操作模板题. 学好acm,英语很重要.做题的时候看不明白题目的意思,我还拉着队友一块儿帮忙分析题意.最后确定了是线段树延迟更新果题.我就欣欣然上手敲了出来. 然后是漫长的段错误.... 第一次 ...
- hdu 5023 线段树延迟更新+状态压缩
/* 线段树延迟更新+状态压缩 */ #include<stdio.h> #define N 1100000 struct node { int x,y,yanchi,sum; }a[N* ...
- HDU 3468:A Simple Problem with Integers(线段树+延迟标记)
A Simple Problem with Integers Case Time Limit: 2000MS Description You have N integers, A1, A2, ... ...
- codevs 1690 开关灯 线段树+延迟标记
1690 开关灯 时间限制: 1 s 空间限制: 128000 KB 题目描述 Description YYX家门前的街上有N(2<=N<=100000)盏路灯,在晚上六点之前,这 ...
- Tree(树链剖分+线段树延迟标记)
Tree http://poj.org/problem?id=3237 Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 12 ...
- HDU4893--Wow! Such Sequence! (线段树 延迟标记)
Wow! Such Sequence! Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
随机推荐
- LCD控制器与帧率、刷新率的关系分析
源:LCD控制器与帧率.刷新率的关系分析 LCM之Fmark功能 && LCD控制器同LCD驱动器的差别 && 帧率与刷新率的关系 && OLED背光
- 通过自动回复机器人学Mybatis:代码重构(分层)
imooc视频学习笔记 ----> URL:http://www.imooc.com/learn/154 ListServlet.java package com.imooc.servlet; ...
- React Native实战系列教程之自定义原生UI组件和VideoView视频播放器开发
React Native实战系列教程之自定义原生UI组件和VideoView视频播放器开发 2016/09/23 | React Native技术文章 | Sky丶清| 4 条评论 | 1 ...
- 使用mysqldump迁移数据
1. 先停止业务,使用MySQLdump的数据导出工具,将您线下原有数据库数据导出为数据文件 mysqldump -hlocalhost -uroot --default-character-set= ...
- 抓jsoup_02_数据
1.测试网页:http://ajax.mianbao99.com/vod-showlist-id-8-order-time-c-3719-p-1.html ZC: 直接查看的话,使用这个链接:http ...
- Ajax基础(二)--获取服务器文件
获取服务器文件相关步骤: 1.创建文件: 2.创建XMLHttpRequest对象: 3.获取文件(注意事项:1)在服务器中运行测试:2)注意编码问题,编码要统一). 3.1 获取xml文件: HTM ...
- iframe标签用法详解
功能:iframe标签用于定义内联框架. 语法:<iframe></iframe> 内联框架是在一个页面中嵌入另一个页面. 有很多网页看上去是一个网页,但实际上它其中可能镶 ...
- python基础之继承原理,多态与封装
1.什么是继承? 继承是一种创建新的类的方式.class A: passclass B: pass2.如何继承---->如何寻找继承关系 现实生活中找继承关系是自下而上,在程序中写是自上而下继承 ...
- java中Arrays和Collections等工具类
java.util.Arrays类能方便地操作数组,它提供的所有方法都是静态的.具有以下功能: ² 给数组赋值:通过fill方法. ² 对数组排序:通过sort方法,按升序. ² 比较数组:通过equ ...
- DOM解析XML文件例子
DOM解析XML文件是一次性将目标文件中的所有节点都读入,然后再进行后续操作的方式. 一般分为以下几步: 1. 定义好目标XML文件路径path . 2. 实例化DOM解析工厂对象 ,Document ...