POJ 2828 Buy Tickets(线段树)

题目链接:http://poj.org/problem?id=2828

题目大意:给定n对数字 p[i], v[i](数据量小于2*10^5)。p为插入时到排首的距离,v为插入的值,问最后的队伍的顺序是如何的。

解析:插入时倒序插入,那么直接可以确定其位置,为队首后p[i]个空位处,如果直接使用数列每次都遍历求得位置,会TLE。利用线段树,定义每个节点的数值的含义为:当前节点范围内的叶子节点有多少是空的,通过update函数找到符合当前所需要空位的位置。

知识点:线段树单点更新。


AC代码如下:

#include <iostream>
#pragma comment(linker, "/STACK:1024000000,1024000000") 
#include <stdio.h>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <string>
#include <string.h>
#include <sstream>
#include <cctype>
#include <climits>
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <vector>
#include <iterator>
#include <algorithm>
#include <stack>
#include <functional>
//cout << "OK" << endl;
#define _clr(x,y) memset(x,y,sizeof(x))
#define _inf(x) memset(x,0x3f,sizeof(x))
#define pb push_back
#define mp make_pair
#define FORD(i,a,b) for (int i=(a); i<=(b); i++)
#define FORP(i,a,b) for (int i=(a); i>=(b); i--)
#define REP(i,n) for (int i=0; i<(n); i++)
using namespace std;
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
const double EULER = 0.577215664901532860;
const double PI = 3.1415926535897932384626;
const double E = 2.71828182845904523536028;
typedef long long LL;
LL pow_mod(LL a,LL n,LL m)
{
    if(n == 0) return 1;
    LL x = pow_mod(a,n>>1,m);
    LL ans = x*x%m;
    if(n&1) ans = ans*a%m;
    return ans;
}
int gcd(int a,int b){return b == 0 ? a : gcd(b,a%b);}

int ans[2000010];
int arr[2000010<<2];
void pushup(int node){
    arr[node] = arr[node<<1] + arr[node<<1|1];
    //当前节点管理下,有多少空位
}
void build(int l, int r, int node){
    if(l == r){
        arr[node] = 1;
        //初始化将每个叶子节点定义为1
        return;
    }
    int m = (l+r)>>1;
    build(l,m,node<<1);
    build(m+1,r,node<<1|1);
    pushup(node);
}
void update(int p, int v, int l, int r, int node){
    if(l == r){
        arr[node] = 0;
        ans[l] = v;
        return;
    }
    //每次寻找到队首的第p[i]个空位处,将v[i]插入
    int m = (l+r)>>1;
    if(p <= arr[node<<1]){
        update(p,v,l,m,node<<1);
        //如果左节点管理的空位大于等于当前所需要的,那么更新左子树
    }
    else update(p-arr[node<<1],v,m+1,r,node<<1|1);
    //如果左边节点的管理的空位小于当前所需要的,那么将需要的空位p[i]减去左边节点代表的已经有的空位,更新右子树。
    pushup(node);   
}
int p[200020], v[200020];
int main(){
    int n;
    while(cin >> n){
        build(1,n,1);
        for(int i = 1; i <= n; i++){
            scanf("%d%d",&p[i],&v[i]);
            p[i]++;
        }
        for(int i = n; i > 0; i--){
            update(p[i],v[i],1,n,1);
        }
        for(int i = 1; i <= n; i++){
            printf("%d ",ans[i]); 
        }
        cout << endl;
    }
    return 0;
} 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值