权值线段树板子题

 

 

https://www.luogu.org/problem/P1168

#include <bits/stdc++.h>
using namespace std;
const int maxn=1e5+5;
int tree[maxn*4];
int dtc[maxn];
int a[maxn];
void pushup(int root)
{
    tree[root]=tree[root<<1]+tree[root<<1|1];
}
void update(int root,int l,int r,int index) // 将权值线段树上值为index(已离散化)的位置值+1 
{
    if(l==r)
    {
        tree[root]++;
        return;
    }
    int mid=(l+r)/2;
    if(mid>=index)
        update(root<<1,l,mid,index);
    else
        update(root<<1|1,mid+1,r,index);
    pushup(root);
}
int querry(int root,int l,int r,int k) // 查询第k大的数字 
{
    if(l==r)
        return l;
    int mid=(l+r)/2;
    if(tree[root<<1]>=k)
        return querry(root<<1,l,mid,k);
    return querry(root<<1|1,mid+1,r,k-tree[root<<1]);
}
int main()
{
    int n;
    scanf("%d",&n);
    for(int i = 1; i <= n; ++i)
    {
        scanf("%d",&a[i]);
        dtc[i]=a[i];
    }
    sort(a+1,a+1+n);
    int cnt=unique(a+1,a+1+n)-a-1;
    for(int i = 1; i <= n; ++i)
        dtc[i]=lower_bound(a+1,a+1+cnt,dtc[i])-a;
    for(int i = 1; i <= n; ++i)
    {
        update(1,1,n,dtc[i]);
        if(i&1)
        {
            int ans=querry(1,1,n,(i+1)/2);
            printf("%d\n",a[ans]);
        }
    }
    return 0;
}

 

posted @ 2019-09-28 21:35  paranoid。  阅读(220)  评论(0编辑  收藏  举报