洛谷 P3369(權值線段樹)

在這裏插入圖片描述

思路:權值線段樹模板

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#include<iostream>
#include<vector>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define SIS std::ios::sync_with_stdio(false)
#define space putchar(' ')
#define enter putchar('\n')
#define lson root<<1
#define rson root<<1|1
typedef pair<int,int> PII;
const int mod=1e4+7;
const int N=1e5+10;
const int inf=0x7f7f7f7f;


ll gcd(ll a,ll b)
{
    return b==0?a:gcd(b,a%b);
}

ll lcm(ll a,ll b)
{
    return a*(b/gcd(a,b));
}

template <class T>
void read(T &x)
{
    char c;
    bool op = 0;
    while(c = getchar(), c < '0' || c > '9')
        if(c == '-')
            op = 1;
    x = c - '0';
    while(c = getchar(), c >= '0' && c <= '9')
        x = x * 10 + c - '0';
    if(op)
        x = -x;
}
template <class T>
void write(T x)
{
    if(x < 0)
        x = -x, putchar('-');
    if(x >= 10)
        write(x / 10);
    putchar('0' + x % 10);
}
struct node
{
    int l,r,cnt=0;

} T[N<<2];

vector<int> vt;
int a[N],op[N];
int b[N];

int getid(int x)
{

   return lower_bound(vt.begin(),vt.end(),x)-vt.begin();

}
void build(int root,int l,int r)
{
    T[root].l=l,T[root].r=r;
    if(l==r)
    {
        T[root].cnt=0;return ;
    }
    int mid=l+r>>1;
    build(lson,l,mid);
    build(rson,mid+1,r);
    T[root].cnt=T[lson].cnt+T[rson].cnt;
}
void update(int p,int v,int root)
{

    if(T[root].l==T[root].r){
        T[root].cnt+=v;
        return ;
    }else
    {
        int m=T[root].l+T[root].r>>1;
    if(m>=p)update(p,v,lson);
    else update(p,v,rson);
    }

    T[root].cnt=T[lson].cnt+T[rson].cnt;

}
int Kth(int k,int root)
{
    if(T[root].l==T[root].r)return T[root].l;
    if(T[lson].cnt>=k)return Kth(k,lson);
    else return Kth(k-T[lson].cnt,rson);

}
int Rank(int p,int root)
{
    if(T[root].r<p)return T[root].cnt;
    int m=T[root].l+T[root].r>>1;
    int res=Rank(p,lson);
    if(p>m+1)res+=Rank(p,rson);
    return res;
}
int findright(int root)
{
    if(T[root].l==T[root].r)return T[root].l;
    if(T[rson].cnt)return findright(rson);
    else return findright(lson);
}
int pre(int p,int root)
{
    if(T[root].r<p)
    {
        if(T[root].cnt)return findright(root);
        return 0;
    }
    int m=T[root].l+T[root].r>>1;
    if(m<p-1 && T[rson].cnt)
    {
        int re=pre(p,rson);
        if(re)return re;
    }
    return pre(p,lson);
}
int findleft(int root)
{
    if(T[root].l==T[root].r)return T[root].l;
    if(T[lson].cnt)return findleft(lson);
    else return findleft(rson);
}
int nex(int p,int root)
{
    if(p<T[root].l)
    {
        if(T[root].cnt)return findleft(root);
        return 0;
    }
    int m=T[root].l+T[root].r>>1;
    if(p<m && T[lson].cnt)
    {
        int re=nex(p,lson);
        if(re)return re;
    }
    return nex(p,rson);
}


int main()
{
    SIS;
    int n;
    cin>>n;
    vt.push_back(-1e8);
    for(int i=1;i<=n;i++)
    {
        cin>>op[i]>>a[i];
        if(op[i]!=4)vt.push_back(a[i]);
    }
    sort(vt.begin(),vt.end());
    vt.erase(unique(vt.begin(),vt.end()),vt.end());
    int m=vt.size();
    build(1,1,m);
    for(int i=1;i<=n;i++)
    {
        if(op[i]==1)
            update(getid(a[i]),1,1);
        else if(op[i]==2)
            update(getid(a[i]),-1,1);
        else if(op[i]==3)
        {
            int ans=Rank(getid(a[i]),1)+1;
            cout<<ans<<endl;
        }
        else if(op[i]==4)
        {

            int ans=vt[Kth(a[i],1)];
            cout<<ans<<endl;
        }
        else if(op[i]==5)
        {

            int ans=vt[pre(getid(a[i]),1)];
            cout<<ans<<endl;
        }
        else if(op[i]==6)
        {

            int ans=vt[nex(getid(a[i]),1)];
            cout<<ans<<endl;
        }
    }

    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章