bzoj 1901: Zju2112 Dynamic Rankings(带修改的区间第k大,树状数组+主席树)

             果然在zju上交是要超内存的。。。据说,可以把树拆开,这样就不会爆掉了。先对原数组建一颗主席树,然后修改的时候再新建一颗主席树,这样的话据说可过。。。太伤感了。

             带修改的主席树更加冷艳了,思想还是和原来差不多,不带修改的时候保存的是前缀和,这时只查询的话时间只需要logn,显然是可以接受的。但是对于带修改的话,如果还是之前那种建树的话,我们会发现,修改的话时间复杂度就是nlogn了,查询还是logn,因为我们需要修改所有包含这个节点的后缀(或前缀),怎么才能均衡一下,降低修改时间复杂度呢?

             对于一个数组,我们要是求这个数组的前缀和的话,如果事先先求出前缀和的话,那么如果修改的话,时间复杂度就是n,查询是1,然后对于这个问题,我们显然可以用树状数组去优化,这样就可以做到查询修改都是logn了。其实带修改的主席树和裸的数组这个情况是一样的,我们也可以通过树状数组去优化"前缀和"问题,这样就很愉快了。查询修改都是nlognlogn了。

            还是比着cxlove大神的写的,很清楚,但是只能过掉bzoj的。。。如果想若zoj的话,还是得建两棵树。有兴趣的话大家可以试一下。。。

cxlove大神这道题的blog,http://blog.csdn.net/acm_cxlove/article/details/8565309

//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<cmath>
#include<stack>
#include<set>
#include<map>
#define INF 0x3f3f3f3f3f3f3f3fll
#define CLR(a, b) memset(a, b, sizeof(a))
#define lowbit(x) (x&(-x))
using namespace std;

#ifdef __int64
typedef __int64 LL;
#else
typedef long long LL;
#endif

const int maxn = 61005;
const int maxm = 2560005;

int n, q, m, tot;
int a[maxn], h[maxn];
int T[maxm], lson[maxm], rson[maxm], c[maxm];

struct Qt
{
    int kind;
    int l, r, k;
    Qt(){}
    Qt(int kind, int l, int r, int k)
        :kind(kind), l(l), r(r), k(k){}
}Q[maxn];

void init_hash(int n)
{
    sort(h + 1, h + 1 + n);
    m = unique(h + 1, h + n + 1) - h - 1;
}

int hash(int x)
{
    return lower_bound(h + 1, h + m + 1, x) - h;
}

int build(int l, int r)
{
    int root = tot ++;
    c[root] = 0;
    if(l != r)
    {
        int mid = (l + r) >> 1;
        lson[root] = build(l, mid);
        rson[root] = build(mid + 1, r);
    }
    return root;
}

int update(int root, int pos, int val)
{
    int newroot = tot ++, tmp = newroot;
    c[newroot] = c[root] + val;
    int l = 1, r = m;
    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 use[maxn];

void add(int x, int pos, int val)
{
    for(int i = x; i <= n; i += lowbit(i))
        T[i] = update(T[i], pos, val);
}

int sum(int x)
{
    int ret = 0;
    for(int i = x; i; i -= lowbit(i))
        ret += c[lson[use[i]]];
    return ret;
}

int query(int left_root, int right_root, int k)
{
    int l = 1, r = m;
    for(int i = left_root - 1; i; i -= lowbit(i)) use[i] = T[i];
    for(int i = right_root; i; i -= lowbit(i)) use[i] = T[i];

    while(l < r)
    {
        int mid = (l + r) >> 1;
        int tmp = sum(right_root) - sum(left_root - 1);
        if(tmp >= k)
        {
            r = mid;
            for(int i = left_root - 1; i; i -= lowbit(i))
                use[i] = lson[use[i]];
            for(int i = right_root; i; i -= lowbit(i))
                use[i] = lson[use[i]];
        }
        else
        {
            l = mid + 1;
            k -= tmp;
            for(int i = left_root - 1; i; i -= lowbit(i))
                use[i] = rson[use[i]];
            for(int i = right_root; i; i -= lowbit(i))
                use[i] = rson[use[i]];
        }
    }
    return l;
}

void init_tree()
{
    T[0] = build(1, m);
    for(int i = 1; i <= n; i ++)
        T[i] = T[0];
    for(int i = 1; i <= n; i ++)
        add(i, hash(a[i]), 1);
}

int main()
{
//    int cas;
//    scanf("%d", &cas);
//    while(cas --)
    while(scanf("%d%d", &n, &q) != EOF)
    {
        tot = 0; m = 1;
//        scanf("%d%d", &n, &q);
        for(int i = 1; i <= n; i ++)
        {
            scanf("%d", &a[i]);
            h[m ++] = a[i];
        }
        for(int i = 0; i < q; i ++)
        {
            char op[3]; int l, r, k;
            scanf("%s", op);
            if(op[0] == 'Q')
            {
                scanf("%d%d%d", &l, &r, &k);
                Q[i] = Qt(0, l, r, k);
            }
            else
            {
                scanf("%d%d", &l, &k);
                Q[i] = Qt(1, l, 0, k);
                h[m ++] = k;
            }
        }
        init_hash(m);
        init_tree();

        for(int i = 0; i < q; i ++)
        {
            if(Q[i].kind == 0)
            {
                printf("%d\n", h[query(Q[i].l, Q[i].r, Q[i].k)]);
            }
            else
            {
                add(Q[i].l, hash(a[Q[i].l]), -1);
                add(Q[i].l, hash(Q[i].k), 1);
                a[Q[i].l] = Q[i].k;
            }
        }
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值