线段树 --- (区间维护+逆推)
Buy Tickets
Time Limit:4000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Description
Railway tickets were difficult to buy around the Lunar New Year in China, so we must get up early and join a long queue…
The Lunar New Year was approaching, but unluckily the Little Cat still had schedules going here and there. Now, he had to travel by train to Mianyang, Sichuan Province for the winter camp selection of the national team of Olympiad in Informatics.
It was one o’clock a.m. and dark outside. Chill wind from the northwest did not scare off the people in the queue. The cold night gave the Little Cat a shiver. Why not find a problem to think about? That was none the less better than freezing to death!
People kept jumping the queue. Since it was too dark around, such moves would not be discovered even by the people adjacent to the queue-jumpers. “If every person in the queue is assigned an integral value and all the information about those who have jumped the queue and where they stand after queue-jumping is given, can I find out the final order of people in the queue?” Thought the Little Cat.
Input
There will be several test cases in the input. Each test case consists of N + 1 lines where N (1 ≤ N ≤ 200,000) is given in the first line of the test case. The next N lines contain the pairs of values Posi and Vali in the increasing order of i (1 ≤ i ≤ N). For each i, the ranges and meanings of Posi and Vali are as follows:
- Posi ∈ [0, i − 1] — The i-th person came to the queue and stood right behind the Posi-th person in the queue. The booking office was considered the 0th person and the person at the front of the queue was considered the first person in the queue.
- Vali ∈ [0, 32767] — The i-th person was assigned the value Vali.
There no blank lines between test cases. Proceed to the end of input.
Output
For each test cases, output a single line of space-separated integers which are the values of people in the order they stand in the queue.
Sample Input
4 0 77 1 51 1 33 2 69 4 0 20523 1 19243 1 3890 0 31492
Sample Output
77 33 69 51 31492 20523 3890 19243
Hint
The figure below shows how the Little Cat found out the final order of people in the queue described in the first test case of the sample input.


#include<cstring> #include<cstdio> #include<cmath> #include<cstdlib> #include<algorithm> #include<iostream> #define MAX 200010 using namespace std; struct Node { int p,v; } node[MAX]; struct TNode { int l,r; int c; } tree[4*MAX]; int N; int ans[MAX]; void build(int l,int r,int x) { tree[x].c=r-l+1; //该区间的大小,开始写反了,wa了两次 tree[x].l=l; tree[x].r=r; if(l==r) //到底了,返回 return; int mid=(l+r)>>1; build(l,mid,2*x); build(mid+1,r,2*x+1); } void Insert(int x,int pos,int val) //下标 位置(position) 价值(value) { tree[x].c--; //该区间的空格数减少1 if(tree[x].l==tree[x].r) //直到找到了一个合适的空格来存放该位置 { ans[tree[x].l]=val; //将价值映射到ans中 return; } if(tree[2*x].c>=pos) //该区间的剩余空格数大于位置的坐标,往前探寻 Insert(2*x,pos,val); else //该区间剩余空格数小于位置的坐标,往后找空格 Insert(2*x+1,pos-tree[2*x].c,val); } int main() { while(~scanf("%d",&N)) { build(1,N,1); int i; for(i=1;i<=N;i++) { scanf("%d%d",&node[i].p,&node[i].v); node[i].p++; //输入是从0开始的,加1变为从1开始 } for(i=N;i>=1;i--) //重点:逆序更新 { Insert(1,node[i].p,node[i].v); } for(i=1;i<N;i++) printf("%d ",ans[i]); printf("%d\n",ans[N]); } return 0; }
作者:北岛知寒
出处:https://www.cnblogs.com/crazyacking/p/3705444.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?