主席树板子(带单点更新&&不带更新)

//   带单点更新的
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;

const int MAXN = 60010;
const int M = 2500010;
int n,q,m,tot;
int a[MAXN], t[MAXN];
int T[MAXN], lson[M], rson[M],c[M];
int S[MAXN];

struct Query
{
    int kind;
    int l,r,k;
}query[10010];

void Init_hash(int k)
{
    sort(t,t+k);
    m = unique(t,t+k) - t;
}
int hash(int x)
{
    return lower_bound(t,t+m,x)-t;
}
int build(int l,int r)
{
    int root = tot++;
    c[root] = 0;
    if(l != r)
    {
        int mid = (l+r)/2;
        lson[root] = build(l,mid);
        rson[root] = build(mid+1,r);
    }
    return root;
}

int Insert(int root,int pos,int val)
{
    int newroot = tot++, tmp = newroot;
    int l = 0, r = m-1;
    c[newroot] = c[root] + val;
    while(l < r)
    {
        int mid = (l+r)>>1;
        if(pos <= mid)
        {
            lson[newroot] = tot++; rson[newroot] = rson[root];
            newroot = lson[newroot]; root = lson[root];
            r = mid;
        }
        else
        {
            rson[newroot] = tot++; lson[newroot] = lson[root];
            newroot = rson[newroot]; root = rson[root];
            l = mid+1;
        }
        c[newroot] = c[root] + val;
    }
    return tmp;
}

int lowbit(int x)
{
    return x&(-x);
}
int use[MAXN];
void add(int x,int pos,int val)
{
    while(x <= n)
    {
        S[x] = Insert(S[x],pos,val);
        x += lowbit(x);
    }
}
int sum(int x)
{
    int ret = 0;
    while(x > 0)
    {
        ret += c[lson[use[x]]];
        x -= lowbit(x);
    }
    return ret;
}
int Query(int left,int right,int k)
{
    int left_root = T[left-1];
    int right_root = T[right];
    int l = 0, r = m-1;
    for(int i = left-1;i;i -= lowbit(i)) use[i] = S[i];
    for(int i = right;i ;i -= lowbit(i)) use[i] = S[i];
    while(l < r)
    {
        int mid = (l+r)/2;
        int tmp = sum(right) - sum(left-1) + c[lson[right_root]] - c[lson[left_root]];
        if(tmp >= k)
        {
            r = mid;
            for(int i = left-1; i ;i -= lowbit(i))
                use[i] = lson[use[i]];
            for(int i = right; i; i -= lowbit(i))
                use[i] = lson[use[i]];
            left_root = lson[left_root];
            right_root = lson[right_root];
        }
        else
        {
            l = mid+1;
            k -= tmp;
            for(int i = left-1; i;i -= lowbit(i))
                use[i] = rson[use[i]];
            for(int i = right;i ;i -= lowbit(i))
                use[i] = rson[use[i]];
            left_root = rson[left_root];
            right_root = rson[right_root];
        }
    }
    return l;
}
void Modify(int x,int p,int d)
{
    while(x <= n)
    {
        S[x] = Insert(S[x],p,d);
        x += lowbit(x);
    }
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int Tcase;
    scanf("%d",&Tcase);
    while(Tcase--)
    {
        scanf("%d%d",&n,&q);
        tot = 0;
        m = 0;
        for(int i = 1;i <= n;i++)
        {
            scanf("%d",&a[i]);
            t[m++] = a[i];
        }
        char op[10];
        for(int i = 0;i < q;i++)
        {
            scanf("%s",op);
            if(op[0] == 'Q')
            {
                query[i].kind = 0;
                scanf("%d%d%d",&query[i].l,&query[i].r,&query[i].k);
            }
            else
            {
                query[i].kind = 1;
                scanf("%d%d",&query[i].l,&query[i].r);
                t[m++] = query[i].r;
            }
        }
        Init_hash(m);
        T[0] = build(0,m-1);
        for(int i = 1;i <= n;i++)
            T[i] = Insert(T[i-1],hash(a[i]),1);
        for(int i = 1;i <= n;i++)
            S[i] = T[0];
        for(int i = 0;i < q;i++)
        {
            if(query[i].kind == 0)
                printf("%d\n",t[Query(query[i].l,query[i].r,query[i].k)]);
            else
            {
                Modify(query[i].l,hash(a[query[i].l]),-1);
                Modify(query[i].l,hash(query[i].r),1);
                a[query[i].l] = query[i].r;
            }
        }
    }
    return 0;
}
//  不带更新的
#include<bits/stdc++.h>

using namespace std;

const int MAXN = 2 * 100000 + 10;

inline int read()
{
    int f = 1 ,x = 0;
    char ch;
    do
    {
        ch =getchar();
        if(ch=='-') f = -1;
    }while(ch<'0'||ch>'9');
    do
    {
        x=(x<<3)+(x<<1)+ch-'0';
        ch = getchar();
    }while(ch>='0'&&ch<='9');
    return f*x;
}

int n,m;

struct node
{
    int val;
    int id;
    friend bool operator < (node a1,node a2)
    {
        return a1.val<a2.val;    
    } 
};

node a[MAXN];

int c[MAXN];

struct Tree
{
    int lc;
    int rc;
    int sum;
    Tree()
    {
        sum = 0;    
    } 
};

Tree tree[MAXN*20];

int root[MAXN];

int cnt =0;

inline void init()
{
    root[0]=0;
    tree[0].lc=tree[0].rc=0;
    tree[0].sum=0;
    return;
}

inline void update(int num,int &rt ,int l,int r)
{
    tree[++cnt]=tree[rt];
    rt = cnt;
    tree[rt].sum++;
    if(l==r) return;
    int mid = (l+r)>>1;
    if(num<=mid) update(num,tree[rt].lc,l,mid);
    else update(num,tree[rt].rc,mid+1,r);
}

inline int query(int l,int r,int k,int x,int y)
{
    int now = tree[tree[r].lc].sum-tree[tree[l].lc].sum;
    if(x==y) return x;
    else
    {
        int mid = (x+y)>>1;
        if(now>=k)
        {
            return query(tree[l].lc,tree[r].lc,k,x,mid);    
        } 
        else return query(tree[l].rc,tree[r].rc,k-now,mid+1,y);
    } 
}

int main()
{
    n = read();
    m = read();
    
    for(int i=1;i<=n;i++) a[i].val=read(),a[i].id = i;
    
    sort(a+1,a+n+1);
    
    for(int i=1;i<=n;i++)
    {
        c[a[i].id]=i;
    }
    
    init();
    
    for(int i=1;i<=n;i++)
    {
        root[i]=root[i-1];
        update(c[i],root[i],1,n);
    }
    
    for(int i=1;i<=m;i++)
    {
        int L =read();
        int R = read();
        int k=read();
        printf("%d\n",a[query(root[L-1],root[R],k,1,n)].val);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值