poj_2828 Buy Tickets(线段树)
来源:互联网 发布:网络监控服务器搭建 编辑:程序博客网 时间:2024/05/19 16:32
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
40 771 511 332 6940 205231 192431 38900 31492
Sample Output
77 33 69 5131492 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 <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <stack>#include <bitset>#include <queue>#include <set>#include <map>#include <string>#include <algorithm>#define FOP freopen("data.txt","r",stdin)#define FOP2 freopen("data1.txt","w",stdout)#define inf 0x3f3f3f3f#define maxn 200010#define mod 1000000007#define PI acos(-1.0)#define LL long long#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1using namespace std;struct Node{ int l, r; int mid(){ return l+r>>1; }}tree[maxn<<4];int cot[maxn<<4];int n;int val[maxn], pos[maxn];int ans[maxn];void build(int pos, int val, int l, int r, int rt){ tree[rt].l = l, tree[rt].r = r; if(l <= pos && pos <= r) cot[rt] = 1; else cot[rt] = 0; if(l == r) { if(l == pos) ans[pos] = val; return ; } int m = l+r>>1; build(pos, val, lson); build(pos, val, rson);}void update(int pos, int val, int rt){ if(tree[rt].l == tree[rt].r) { if(!cot[rt]) ans[pos] = val, cot[rt]++; return ; } cot[rt]++; int m = tree[rt].mid(); if(cot[rt<<1] + pos <= m) update(pos, val, rt<<1); else update(pos+cot[rt<<1], val, rt<<1|1);}int main(){ while(~scanf("%d", &n)) { for(int i = 0; i < n; i++) scanf("%d%d", &pos[i], &val[i]); build(pos[n-1], val[n-1], 0, n-1, 1); for(int i = n-2; i >= 0; i--) update(pos[i], val[i], 1); for(int i = 0; i < n-1; i++) printf("%d ", ans[i]); printf("%d\n", ans[n-1]); } return 0;}
- poj_2828 Buy Tickets(线段树)
- Buy Tickets----POJ_2828----线段树之单点更新
- Buy Tickets (线段树)
- Buy Tickets(线段树)
- Buy Tickets 线段树
- pku 2828 Buy Tickets(线段树)
- poj 2828 Buy Tickets(线段树)
- poj Buy Tickets(线段树)
- POJ 2828 Buy Tickets(线段树)
- poj 2828 Buy Tickets(线段树)
- POJ 2828 Buy Tickets(线段树)
- poj 2828 Buy Tickets (线段树)
- 【POJ】Buy Tickets(思路 + 线段树)
- POJ 2828--Buy Tickets(线段树)
- POJ 2828 Buy Tickets(线段树)
- POJ 2828 Buy Tickets(线段树)
- POJ 2828 Buy Tickets(线段树)
- POJ 2828 Buy Tickets(线段树)
- 在Servlet使用getServletContext()获取ServletContext对象出现java.lang.NullPointerException(空指针)异常的解决办法
- Spring-mybatis事务配置
- 【Android 基础】RecyclerView 概述以及使用步骤
- 58-System V 消息队列
- 微信小程序使用font-awesome图标库
- poj_2828 Buy Tickets(线段树)
- Program "g++" not found in PATH
- Leetcode 282, Expression Add Operators
- Java考试题之三
- JPEG系列二 JPEG文件中的EXIF(下)
- jQuery遮罩效果
- JavaWeb总结二十七、jsp简单标签开发案例和打包
- 分词效果的评测算法(原创)
- React+Webpack+ES6从环境搭建到HelloWorld