码迷,mamicode.com
首页 > 其他好文 > 详细

POJ - 2828 - Buy Tickets (线段树)

时间:2015-05-14 08:40:19      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:acm   poj   区间的维护和更新   线段树   


题目传送:Buy Tickets


思路:线段树,从后往前依次插入,插入一个更新一次


AC代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <deque>
#include <cctype>
#define LL long long
#define INF 0x7fffffff
using namespace std;

const int maxn = 200005;

struct node {
	int pos, v;
}a[maxn];

int x[maxn << 2], num[maxn];

void build(int l, int r, int rt) {
	x[rt] = r - l + 1;
	if(r == l) return;
	int mid = (r + l) >> 1;
	build(l, mid, rt << 1);
	build(mid + 1, r, rt << 1 | 1);
}

int query(int p, int l, int r, int rt) {
	x[rt] --;
	if(l == r) return l;
	int mid = (l + r) >> 1;
	if(x[rt << 1] >= p) return query(p, l, mid, rt << 1);
	else return query(p - x[rt << 1], mid + 1, r, rt << 1 | 1);
}

int main() {
	int n;
	while(scanf("%d", &n) != EOF) {
		for(int i = 1; i <= n; i ++) {
			scanf("%d %d", &a[i].pos, &a[i].v);
		}
		build(1, n, 1);
		for(int i = n; i >= 1; i --) {
			int p = query(a[i].pos + 1, 1, n, 1);
			num[p] = a[i].v;
		}
		for(int i = 1; i < n; i ++) {
			printf("%d ", num[i]);
		}
		printf("%d\n", num[n]);
	}
	return 0;
}






POJ - 2828 - Buy Tickets (线段树)

标签:acm   poj   区间的维护和更新   线段树   

原文地址:http://blog.csdn.net/u014355480/article/details/45704389

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!