POJ 2828 Buy Tickets(单点更新)
来源:互联网 发布:2015中国汽车出口数据 编辑:程序博客网 时间:2024/06/11 07:28
题目链接:http://poj.org/problem?id=2828
对于第i个人的pos,可以看成在第i个人之前需要留的空位置数。
利用线段树可以确定每个人的最终位置。
那么对于每个人做一次查询,但需要 从后往前 查询。
代码如下。
#include <iostream>#include <iomanip>#include <cstdio>#include <algorithm>#include <cmath>#include <queue>#include <vector>#include <stack>#include <string>#include <cstring>#include <cassert>using namespace std;typedef long long ll;const int maxn=222222;const int INF=0x7fffffff;const int mod=1e7+7;#define LSON l, m, rt<<1#define RSON m+1, r, rt<<1|1#define ESP 1e-7struct people { int pos, val;}peo[maxn];int arr[maxn<<2], put[maxn];void pushup(int rt) { arr[rt]=arr[rt<<1]+arr[rt<<1|1];//每个节点存该区间剩余位置数}void build(int l, int r, int rt) { if(l==r) { arr[rt]=1; return ; } int m=(l+r)>>1; build(LSON); build(RSON); pushup(rt);}int query(int pos, int l, int r, int rt) { if(l==r) { arr[rt]--; return l; } int m=(l+r)>>1; int temp=pos<=arr[rt<<1]?query(pos, LSON):query(pos-arr[rt<<1], RSON); pushup(rt);//查询同时更新 return temp;}int main() { int n; while(~scanf("%d", &n)) { build(1, n, 1); for(int i=0;i<n;i++) { scanf("%d%d", &peo[i].pos, &peo[i].val); peo[i].pos++; } for(int i=n-1;i>=0;i--)//逆序 put[query(peo[i].pos, 1, n, 1)]=peo[i].val; for(int i=1;i<n;i++) printf("%d ", put[i]); printf("%d\n", put[n]); } return 0;}
0 0
- POJ 2828 Buy Tickets(单点更新)
- POJ 2828 Buy Tickets(单点更新)
- POJ 2828 Buy Tickets (单点更新)
- poj 2828 Buy Tickets(线段树单点更新)
- poj 2828 Buy Tickets (线段树---单点更新)
- POJ - 2828 Buy Tickets(线段树单点更新)
- POJ 题目2828 Buy Tickets(线段树单点更新)
- POJ 2828 Buy Tickets(线段树--单点更新)
- POJ 2828 Buy Tickets (线段树:单点更新)
- POJ 2828 Buy Tickets(线段树-单点更新)
- POJ 2828-Buy Tickets(线段树单点更新-插队)
- POJ 2828 Buy Tickets(线段树的单点更新)
- POJ 2828 Buy Tickets 线段树 单点更新
- POJ 2828 Buy Tickets(单点更新 区间求和)
- Buy Tickets---poj 2828 线段树单点更新练习
- POJ 2828 Buy Tickets ( 线段树—单点更新 )
- POJ 2828 Buy Tickets (线段树单点更新)
- POJ 2828 Buy Tickets (线段树,单点更新)
- 并发编程——线程安全性
- Servlet小记
- n的k次方,使用递归的两种实现方法
- 搭建Hadoop-2.6.0集群
- Android几个面试题的解答
- POJ 2828 Buy Tickets(单点更新)
- linux学习笔记--基本命令的学习--文件搜索命令
- Python中怎样使用shape计算矩阵的行和列
- 李红霞p-27-6题
- 常见OJ评判结果对照表,作为ACMer你懂得!
- JVMTI 中的JNI系列函数,线程安全及调试技巧
- HDU 1166 敌兵布阵(单点更新)
- SGU - 302 - BHTML 1.0 (栈的应用)
- WdatePicker日历控件使用方法