Buy Tickets POJ - 2828 + segment tree thinking
The meaning of problems
N is said to have individual tickets, but then it will go to n individual jump the queue, the queue is finally asked what was happening. Jump the queue input two numbers, the first one is in front of how many people, the second is the person's number, the final output numbers just fine.
Problem-solving ideas
This problem backwards to deal with, because after the last person to jump the queue to complete, others will not affect him. There are n people in front of him, then he is n + 1 position, so to come, we need to know that position, there are n people in front of him on the line. No one default for each position.
Detailed look at the code.
Code
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#define ls (rt<<1)
#define rs (rt<<1|1)
#define mid ( (t[rt].l+t[rt].r)>>1 )
using namespace std;
const int maxn=2e5+7;
struct node{
int l, r;
int sum, val;//sum记录这个区间内有多少空位置,val记录这个人的编号
}t[maxn<<2];
struct note{//记录这n个人的插队的顺序
int pos, val;
}a[maxn];
int n;
void up(int rt)
{
t[rt].sum=t[ls].sum+t[rs].sum;
}
void build(int rt, int l, int r)
{
t[rt].l=l;
t[rt].r=r;
t[rt].sum=r-l+1;
if(l==r) return ;
build(ls, l, mid);
build(rs, mid+1, r);
}
void update(int rt, int pos, int val)
{
if(t[rt].l==t[rt].r)
{
t[rt].sum--;
t[rt].val=val;
return ;
}
if(t[ls].sum>=pos)
update(ls, pos, val);
else
update(rs, pos-t[ls].sum, val);
up(rt);
}
void query(int rt)
{
if(t[rt].l==t[rt].r)
{
printf("%d ", t[rt].val);
return ;
}
query(ls);
query(rs);
}
int main()
{
while(scanf("%d", &n)!=EOF)
{
for(int i=1; i<=n; i++)
{
scanf("%d%d", &a[i].pos, &a[i].val);
}
build(1, 1, n);
for(int i=n; i>=1; i--)
{
update(1, a[i].pos+1, a[i].val);
}
query(1);
printf("\n");
}
return 0;
}