BZOJ 1901 Zju2112 Dynamic Rankings 树套"树"

此题很明显的树套树嘛…… 博主也是这样做的 , 唯一的区别就是平衡树不用自己写啦 , pbds 送了一个红黑树给我啦!

然后 , 这里主要记录一下那些难得记住的定义方式:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define mid (((l)+(r))/2)
#define ls ((o)*2)
#define rs ((o)*2+1)

using namespace std;
using namespace __gnu_pbds;
typedef pair<int, int> pi;

//typedef tree<pi , null_mapped_type , less<pi> , rb_tree_tag , tree_order_statistics_node_update> splay;
typedef tree<pi , null_type , less<pi> , rb_tree_tag , tree_order_statistics_node_update> splay;

const int maxn = 1e4+1e2;
const int INF = 0x3f3f3f3f;

int n , m;
int num[maxn];
splay a[100000];

void modify(int o , int l , int r , int p , int v , int add)
{
    if(add) a[o].insert(make_pair(v,p));
    else a[o].erase(make_pair(v,p));

    if(l!=r)
    {
        if(p <= mid) modify(ls, l, mid, p, v, add);
        else modify(rs, mid+1, r, p, v, add);
    }
}

int query(int o , int l , int r , int L , int R , int v)
{
    if(L <= l && r <= R) return a[o].order_of_key(make_pair(v, INF));
    else 
    {
        int res = 0;
        if(L <= mid)  res += query(ls, l, mid, L, R, v);
        if(R >  mid)  res += query(rs, mid+1, r, L, R, v);
        return res;
    }
}

int findK(int x , int y , int k)
{
    int l = -1 , r = INF , m;
    while(l+1<r)
    {
        m = (l+r)/2;
        if(query(1, 1, n, x, y, m) >= k) r = m;
        else l = m;
// cout<<m<<" "<<query(1, 1, n, x, y, m)<<endl;
    }

    return r;
}

int main(int argc, char *argv[]) {

    cin>>n>>m;
    for(int i=1;i<=n;i++) scanf("%d" , &num[i]) , modify(1, 1, n, i, num[i], 1);

    char s[2]; int a , b , c;
    while(m--)
    {
        scanf("%s" , s);
        if(s[0] == 'Q')
        {
            scanf("%d%d%d" , &a , &b , &c);
            printf("%d\n" , findK(a, b, c));
        }
        else 
        {
            scanf("%d%d" , &a , &b);
            modify(1, 1, n, a, num[a], 0);
            modify(1, 1, n, a, num[a] = b, 1);
        }
    }   

    return 0;
}

你可能感兴趣的:(线段树,PBDS)