ZOJ2011-Dynamic Rankings 树套树?

好久没有写博文了,这题是刚才被lrj的题虐了之后写的,这题的题意是告诉你N个数的序列,每次修改一个位置的值,动态查询区间第k个元素


做法是维护一个线段树,这样我们就可以得到区间的信息,但是这时候我们并不能维护区间有序的序列,所以我们要二分答案,查询l到r区间内比这个数小的有多少,和k做比较,然后最后得到答案,写起来就是一个线段树和一个平衡树,对于平时写数据结构写多的来说代码量一般,我的代码一向都很长,写了200行左右。


/*
 * Author: Fish@UESTC_Obsidian
 * Created Time:  2011/09/30 18:40:59
 * Project: ZOJ2112-Dynamic_Rankings
 *    Type: ds
 */
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>

using namespace std;

typedef long long ll;
const int MAX = 50005;
const int oo = 0x3f3f3f3f;

struct Node
{
    int key, size;
    Node* c[2];
} memo[MAX * 20], *cur, *nil;
int a[MAX], n, q;

struct Seg
{
    int l, r;
    Node* root;
} seg[MAX << 2];

inline Node* New(int v)
{
    cur->key = v;
    cur->size = 1;
    cur->c[0] = cur->c[1] = nil;
    return cur++;
}

inline void rotate(Node*& t, int f)
{
    Node* k = t->c[f ^ 1];
    t->c[f ^ 1] = k->c[f];
    k->c[f] = t;
    k->size = t->size;
    t->size = t->c[0]->size + t->c[1]->size + 1;
    t = k;
}

inline void keep(Node*& t, int f)
{
    if (t == nil) return;
    else if (t->c[f]->c[f]->size > t->c[f ^ 1]->size) rotate(t, f ^ 1);
    else if (t->c[f]->c[f ^ 1]->size > t->c[f ^ 1]->size) rotate(t->c[f], f), rotate(t, f ^ 1);
    else return;
    for (int i = 0; i < 2; i++) keep(t->c[i], i);
    for (int i = 0; i < 2; i++) keep(t, i);
}

inline void insert(Node*& t, int v)
{
    if (t == nil)
    {
        t = New(v);
    }
    else
    {
        t->size++;
        insert(t->c[v >= t->key], v);
        keep(t, v >= t->key);
    }
}

inline Node* del(Node*& t, int v)
{
    Node* p;
    if (t == nil) return nil;
    t->size--;
    if (v == t->key || t->c[v > t->key] == nil)
    {
        if (t->c[0] != nil && t->c[1] != nil) p = del(t->c[0], v + 1), t->key = p->key;
        else p = t, t = t->c[t->c[0] == nil];
        return p;
    }
    else return del(t->c[v > t->key], v);
}

inline int getRank(Node* t, int v)
{
    int ret = 0;
    while (t != nil)
        if (t->key < v) ret += t->c[0]->size + 1, t = t->c[1];
        else t = t->c[0];
    return ret;
}

inline void init()
{
    nil = cur = memo;
    nil = New(0);
    nil->size = 0;
}

inline void init(int k, int l, int r)
{
    seg[k].l = l;
    seg[k].r = r;
    seg[k].root = nil;
    for (int i = l; i <= r; i++)
    {
        insert(seg[k].root, a[i]);
    }
    if (l == r) return;
    int mid = l + r >> 1;
    init(k << 1, l, mid);
    init(k << 1 | 1, mid + 1, r);
}

inline void set(int k, int idx, int v)
{
    del(seg[k].root, a[idx]);
    insert(seg[k].root, v);
    if (seg[k].l == seg[k].r) return;
    int mid =  seg[k].l + seg[k].r >> 1;
    if (idx <= mid) set(k << 1, idx, v);
    else set(k << 1 | 1, idx, v);
}

inline int read(int k, int l, int r, int v)
{
    if (l > r || seg[k].l > r || seg[k].r < l) return 0;
    if (seg[k].l >= l && seg[k].r <= r)
    {
        return getRank(seg[k].root, v);
    }
    return read(k << 1, l, r, v) + read(k << 1 | 1, l, r, v);
}

inline int doit(int lb, int rb, int k)
{
    int l = -1000000005, r = 1000000005, mid;
    int ret;
    
    while (l < r)
    {
        mid = l + r + 1 >> 1;
        ret = read(1, lb, rb, mid);
        if (ret < k) l = mid;
        else r = mid - 1;
    }
    
    return l;
}

inline void doit()
{
    char op[5];
    int l, r, k;
    
    init();
    init(1, 1, n);
    while (q--)
    {
        scanf("%s", op);
        if (op[0] == 'Q')
        {
            scanf("%d%d%d", &l, &r, &k);
            printf("%d\n", doit(l, r, k));
        }
        else
        {
            scanf("%d%d", &l, &r);
            set(1, l, r);
            a[l] = r;
        }
    }
}

int main()
{
    int T;
    
    scanf("%d", &T);
    while (T--)
    {
        scanf("%d%d", &n, &q);
        for (int i = 1; i <= n; i++)
            scanf("%d", &a[i]);
        doit();
    }
    
    return 0;
}



你可能感兴趣的:(数据结构,c,OO,insert)