您的位置:首页 > 其它

POJ 2828 Buy Tickets(多校连萌,线段树模拟插入)

2016-03-29 16:09 417 查看
题目地址:http://poj.org/problem?id=2828

题意:给一个队列,第一个数是插在哪个位置上,第二个数这个人的标号,问最后的队列是什么样

思路:用线段树倒着插入即可

AC代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>
const int inf = 0x3f3f3f3f;//1061109567
typedef long long LL;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
const int maxn = 200010;
int pos[maxn],value[maxn],sum[maxn<<2],ans[maxn<<2];
void pushup(int rt)
{
sum[rt] = sum[rt<<1]+sum[rt<<1|1];
}
void build(int l,int r,int rt)
{
sum[rt] = r - l + 1;
if(l == r) return;
int m = (l + r)>>1;
build(lson);
build(rson);
}
void updata(int pos,int value,int l,int r,int rt)
{
if(l == r)
{
ans[rt] = value;
sum[rt]--;
return;
}
int m = (l + r)>>1;
if(pos <= sum[rt<<1]) updata(pos,value,lson);
else updata(pos-sum[rt<<1],value,rson);
pushup(rt);
}
void shuchu(int l,int r,int rt)
{
if(l == r)
{
if(l == 1)
printf("%d",ans[rt]);
else
printf(" %d",ans[rt]);
return;
}
int m = (l + r)>>1;
shuchu(lson);
shuchu(rson);
}
int main()
{
int n;
while(scanf("%d",&n) != EOF)
{
for(int i=0; i<n; i++)
scanf("%d%d",&pos[i],&value[i]);
build(1,n,1);
for(int i=n-1; i>=0; i--)
updata(pos[i]+1,value[i],1,n,1);
shuchu(1,n,1);
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: