POJ 2828 单点更新

简介:

题意:给出N个人要查入队伍中的位置,按照输入的顺序,求最后队伍的先后顺序。

先把输入存起来,从后往前插入,前一个序号就代表前面有几个人,线段树内记录当前位置是否被占了,找低Num+1个空位坐下就行,查询函数返回空总和为num+1的位置。

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int maxn=200005;
int node[maxn<<2];
int yl[maxn][2],q[maxn];
void build(int l,int r,int rt)
{
    node[rt]=r-l+1;
    if(l==r)
        return;
    int m=(l+r)>>1;
    build(lson);
    build(rson);
}
int query(int pos,int l,int r,int rt)
{
    node[rt]--;
    if(l==r)
    {
        return l;
    }
    int m=(l+r)>>1;
    int ret=0;
    if(pos<=node[rt<<1])
        ret=query(pos,lson);
    else
        ret=query(pos-node[rt<<1],rson);
    return ret;
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        build(1,n,1);
        for(int i=0; i<n; i++)
            scanf("%d%d",&yl[i][0],&yl[i][1]);
        for(int i=n-1; i>=0; i--)
            q[query(yl[i][0]+1,1,n,1)]=yl[i][1];
        for(int i=1; i<=n; i++)
            printf("%d%c",q[i],i!=n?' ':'\n');
    }
    return 0;
}


目录
相关文章
|
11月前
poj 1990 MooFest 树状数组
题意就是有N头牛,每头牛都有一个坐标和声调值(x, v),两头牛之间通讯要花费的能量是他们的距离乘以最大的一个音调值,现在要任意两头牛之间都相互通讯一次,求总共需要花费多少能量?
40 0
|
人工智能 Java
HDU-敌兵布阵(线段树 || 树状数组)
HDU-敌兵布阵(线段树 || 树状数组)
85 0
|
人机交互
POJ-2524,Ubiquitous Religions(并查集模板题)
POJ-2524,Ubiquitous Religions(并查集模板题)
|
人工智能 网络架构
|
Java
HDU 1754 I Hate It(线段树之单点更新,区间最值)
I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 70863    Accepted Submission(s): 27424 Problem Description 很多学校流行一种比较的习惯。
1093 0
线段树-poj-2823
Sliding Window Description An array of size n ≤ 106 is given to you. There is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k
1074 0
Hdu1754-线段树-单点更新
#include&lt;iostream&gt; #include&lt;cstdio&gt; #include&lt;algorithm&gt; using namespace std; #define lson l,m,rt&lt;&lt;1 #define rson m+1,r,rt&lt;&lt;1|1 const int maxn=200005; int MAX[maxn&l
1125 0