HDU 3577Fast Arrangement(线段树模板之区间增减更新 区间求和查询)
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.
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.
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.
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
题目意思:
现在有一辆列车,可以坐k个人
先买到票的就有座位
现在给你q个买票问询:a站到b站
如果此票持有者路上车,则输出问询编号
线段树解决
如果此时a,b区间内最大值是小于k的,那么
说明此人可以上车
可以上车的话
将区间a,b内部的值加1
现在有一辆列车,可以坐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=1000005; int ans[max_v]; struct node { int l,r,v,lazy; }tree[max_v<<2]; void build(int l,int r,int root) { tree[root].l=l; tree[root].r=r; tree[root].v=0; tree[root].lazy=0; if(l==r) return ; int mid=(l+r)>>1; build(l,mid,root<<1); build(mid+1,r,root<<1|1); } void push_up(int root)//往上更新父节点数据 { tree[root].v=max(tree[root<<1].v,tree[root<<1|1].v);// 最值 } void push_down(int root)//向下往左右儿子方向更新数据 { tree[root<<1].lazy+=tree[root].lazy; tree[root<<1|1].lazy+=tree[root].lazy; tree[root<<1].v+=tree[root].lazy; tree[root<<1|1].v+=tree[root].lazy; tree[root].lazy=0;//更新之后lazy清零 } void update(int l,int r,int root) { if(tree[root].l==l&&tree[root].r==r)//如果区间完全重合,则不需要继续往下更新了 { tree[root].v+=1; tree[root].lazy+=1; return; } if(tree[root].lazy)//因为没有找到完全重合的区间,所以要先更新下一层区间; push_down(root); int mid=(tree[root].l+tree[root].r)>>1; if(l>mid) update(l,r,root<<1|1); else if(r<=mid) update(l,r,root<<1); else { update(l,mid,root<<1); update(mid+1,r,root<<1|1); } 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)>>1; if(l>mid) return getmax(l,r,root<<1|1); else if(r<=mid) return getmax(l,r,root<<1); else { return max(getmax(l,mid,root<<1),getmax(mid+1,r,root<<1|1)); } } int main() { int t; scanf("%d",&t); int c=1; while(t--) { memset(ans,0,sizeof(ans)); int k,q; scanf("%d %d",&k,&q); build(1,1000000,1); int m=0; for(int i=0;i<q;i++) { int a,b; scanf("%d %d",&a,&b); b--; if(getmax(a,b,1)<k) { ans[m++]=i+1; update(a,b,1); } } printf("Case %d:\n",c++); for(int i=0;i<m;i++) printf("%d ",ans[i]); printf("\n\n"); } return 0; } /* 题目意思: 现在有一辆列车,可以坐k个人 先买到票的就有座位 现在给你q个买票问询:a站到b站 如果此票持有者路上车,则输出问询编号 线段树解决 如果此时a,b区间内最大值是小于k的,那么 说明此人可以上车 可以上车的话 将区间a,b内部的值加1 完全符合线段树的基本操作 区间更新 区间增减 区间查询 找最值 */
心之所向,素履以往
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南