POJ 1818——Buy Tickets【线段树 & 单点更新】

题目传送门


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.
在这里插入图片描述


题意

有N个人排队,每一个人都有一个val来对应,每一个后来人都会插入当前队伍的某一个位置pos。要求把队伍最后的状态输出。


题解

  • 正向思考的话,这道题就是最简单的模拟了,明显不能这样做。那就逆向思考

  • 现在我们知道总共有 n 个人,且最后一个人要排在这 n 个人中的位置是 p[n],我们可以直接把队列的第 p[n] 个位子安排给第 n 个人

    现在来考虑第 n-1 个人,他要排在只有 n-1 个人的第 p[n-1] 个位置上,怎么做呢?我们肯定是从队列的第 1 个开始数,遇到没有被人占领的位置就加 1,直到数到了 p[n-1],那么这个空位就安排给第 n-1 个人

    对于第 n-2 个人,以及剩下的所有人都可以这样做

  • 当然,不可能真的一个一个的数,但是可以用线段树动态的统计空余位置的数量更新空余位置的信息

    所以对每个人安排位置,最坏需要 log(n) 的时间,总的时间复杂度是 nlog(n) 的


AC-Code

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <fstream>
#include <utility>
using namespace std;
typedef long long ll;
typedef pair<int, int> Pii;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

const int INF = 0x7fffffff;
const int MOD = 1e4;
const int MAXN = 2e5 + 7;

struct node {
	int left;
	int right;
	int size;// 维护剩余空位置总数
}tree[MAXN << 2];
int n;
int ans[MAXN];
void PushUp(int rt) {
	tree[rt].size = tree[rt << 1].size + tree[rt << 1 | 1].size;
}
void build(int rt, int l, int r) {
	tree[rt].left = l;
	tree[rt].right = r;
	if (l == r) {
		tree[rt].size = 1;
		return;
	}
	int mid = (l + r) >> 1;
	build(rt << 1, l, mid);
	build(rt << 1 | 1, mid + 1, r);
	PushUp(rt);
}
int Query(int rt, int pos) {
	if (tree[rt].left == tree[rt].right) {
		tree[rt].size = 0;
		return tree[rt].left;
	}
	int s;
	int mid = (tree[rt].left + tree[rt].right) >> 1;
	if (pos <= tree[rt << 1].size)	s = Query(rt << 1, pos);
	else	s = Query(rt << 1 | 1, pos - tree[rt << 1].size);
	PushUp(rt);
	return s;
}

int a[MAXN], b[MAXN];
int main() {
	int n;
	while (scanf("%d", &n) == 1) {
		build(1, 1, n);
		for (int i = 1; i <= n; i++) {
			scanf("%d%d", &a[i], &b[i]);
		}
		for (int i = n; i > 0; i--)
			ans[Query(1, a[i] + 1)] = b[i];
		for (int i = 1; i <= n; i++) {
			printf("%d", ans[i]);
			if (i == n)
				printf("\n");
			else
				printf(" ");
		}
	}
	return 0;
}
发布了104 篇原创文章 · 获赞 60 · 访问量 5876

猜你喜欢

转载自blog.csdn.net/Q_1849805767/article/details/103153737
今日推荐