【题目分析】

BZOJ这个题目抄的挺霸气。

主席树是第一时间想到的,但是修改又很麻烦。

看了别人的题解,原来还是可以用均摊的思想,用树状数组套主席树。

学到了新的姿势,2333o(* ̄▽ ̄*)ブ

【代码】

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>

#include <set>
#include <map>
#include <string>
#include <algorithm>
#include <vector>
#include <iostream>
#include <queue>

using namespace std;

#define maxn 20005
#define mlog 16
#define inf (0x3f3f3f3f)

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

int rt[maxn],ls[maxn*mlog*mlog],rs[maxn*mlog*mlog],siz[maxn*mlog*mlog];
int a[maxn],b[maxn],n,m,top=0,tot=0;
int opt[maxn],x[maxn],y[maxn],z[maxn],L[mlog],R[mlog];
char s[11];

void ins(int o1,int & o2,int l,int r,int x,int f)
{
//  printf("ins %d %d %d %d %d %d\n",o1,o2,l,r,x,f);
    o2=++tot;
    siz[o2]=siz[o1]+f;
    if (l==r) return ;
    int mid=(l+r)/2;
    if (x<=mid) rs[o2]=rs[o1],ins(ls[o1],ls[o2],l,mid,x,f);
    else ls[o2]=ls[o1],ins(rs[o1],rs[o2],mid+1,r,x,f);
    return ;
}

int query(int l,int r,int x)
{
//  printf("query %d %d %d\n",l,r,x);
    if (l==r) return l;
    int mid=(l+r)/2,tmp=0;
    for (int i=1;i<=L[0];++i) tmp-=siz[ls[L[i]]];
    for (int i=1;i<=R[0];++i) tmp+=siz[ls[R[i]]];
//  printf("tmp is %d\n",tmp);
    if (x<=tmp)
    {
        for (int i=1;i<=L[0];++i) L[i]=ls[L[i]];
        for (int i=1;i<=R[0];++i) R[i]=ls[R[i]];
        return query(l,mid,x);
    }
    else
    {
        for (int i=1;i<=L[0];++i) L[i]=rs[L[i]];
        for (int i=1;i<=R[0];++i) R[i]=rs[R[i]];
        return query(mid+1,r,x-tmp);
    }
}

int main()
{
    top=n=read();m=read();
    for (int i=1;i<=n;++i) b[i]=a[i]=read();
    for (int i=1;i<=m;++i)
    {
        scanf("%s",s);
        switch(s[0])
        {
            case 'Q': opt[i]=0;x[i]=read();x[i]--;y[i]=read();z[i]=read();break;
            case 'C': opt[i]=1;x[i]=read();y[i]=read();b[++top]=y[i];break;
        }
    }
    sort(b+1,b+top+1);
    top=unique(b+1,b+top+1)-b-1;
    for (int i=1;i<=n;++i) a[i]=lower_bound(b+1,b+top+1,a[i])-b;
    for (int i=1;i<=m;++i) if (opt[i]) y[i]=lower_bound(b+1,b+top+1,y[i])-b;
//  printf("lisan is OK!\n");
//  for (int i=1;i<=n;++i) printf("%d ",a[i]); printf("\n");
//  for (int i=1;i<=m;++i) printf("%d ",y[i]); printf("\n");
    for (int i=1;i<=n;++i)
    {
//      printf("ins %d %d\n",a[i],i);getchar();
        for (int j=i;j<=n;j+=j&(-j))
            ins(rt[j],rt[j],1,top,a[i],1);
    }
    for (int i=1;i<=m;++i)
    {
        if (opt[i])
        {
//          printf("fix %d to %d\n",x[i],y[i]);
            for (int j=x[i];j<=n;j+=j&(-j))
            {
                ins(rt[j],rt[j],1,top,a[x[i]],-1);
                ins(rt[j],rt[j],1,top,y[i],1);
            }
            a[x[i]]=y[i];
        }
        else
        {
//          printf("query %d %d %d \n",x[i],y[i],z[i]);
            L[0]=0; R[0]=0;
            for (int j=x[i];j;j-=j&(-j)) L[++L[0]]=rt[j];
            for (int j=y[i];j;j-=j&(-j)) R[++R[0]]=rt[j];
//          printf("L :"); for (int j=1;j<=L[0];++j) printf("%d ",L[i]);
//          printf("R :"); for (int j=1;j<=R[0];++j) printf("%d ",R[i]);
            printf("%d\n",b[query(1,top,z[i])]);
        }
    }
}

  

BZOJ 1901 Zju2112 Dynamic Rankings ——树状数组套主席树的更多相关文章

  1. P2617 Dynamic Rankings(树状数组套主席树)

    P2617 Dynamic Rankings 单点修改,区间查询第k大 当然是无脑树套树了~ 树状数组套主席树就好辣 #include<iostream> #include<cstd ...

  2. ZOJ 2112 Dynamic Rankings(树状数组套主席树 可修改区间第k小)题解

    题意:求区间第k小,节点可修改 思路:如果直接用静态第k小去做,显然我更改一个节点后,后面的树都要改,这个复杂度太高.那么我们想到树状数组思路,树状数组是求前缀和,那么我们可以用树状数组套主席树,求出 ...

  3. LUOGU P2617 Dynamic Rankings(树状数组套主席树)

    传送门 解题思路 动态区间第\(k\)大,树状数组套主席树模板.树状数组的每个位置的意思的是每棵主席树的根,维护的是一个前缀和.然后询问的时候\(log\)个点一起做前缀和,一起移动.时空复杂度\(O ...

  4. BZOJ 3196 Tyvj 1730 二逼平衡树 ——树状数组套主席树

    [题目分析] 听说是树套树.(雾) 怒写树状数组套主席树,然后就Rank1了.23333 单点修改,区间查询+k大数查询=树状数组套主席树. [代码] #include <cstdio> ...

  5. BZOJ 2141 排队(树状数组套主席树)

    解法很多的题,可以块套树状数组,可以线段树套平衡树.我用的是树状数组套主席树. 题意:给出一段数列,m次操作,每次操作是交换两个位置的数,求每次操作后的逆序对数.(n,m<=2e4). 对于没有 ...

  6. BZOJ_3196_Tyvj 1730 二逼平衡树_树状数组套主席树

    BZOJ_3196_Tyvj 1730 二逼平衡树_树状数组套主席树 Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作: 1.查询k在区间内的排 ...

  7. [COGS257]动态排名系统 树状数组套主席树

    257. 动态排名系统 时间限制:5 s   内存限制:512 MB [问题描述]给定一个长度为N的已知序列A[i](1<=i<=N),要求维护这个序列,能够支持以下两种操作:1.查询A[ ...

  8. 洛谷P3759 [TJOI2017]不勤劳的图书管理员 【树状数组套主席树】

    题目链接 洛谷P3759 题解 树状数组套主席树板题 #include<algorithm> #include<iostream> #include<cstring> ...

  9. Codeforces Round #404 (Div. 2) E. Anton and Permutation(树状数组套主席树 求出指定数的排名)

    E. Anton and Permutation time limit per test 4 seconds memory limit per test 512 megabytes input sta ...

随机推荐

  1. 【XLL API 函数】 xlfSetName

    常常用于创建和删除与DLL定义的名称 原型 Excel12(xlfSetName, LPXLOPER12 pxRes, 2, LPXLOPER12 pxNameText, LPXLOPER12 pxN ...

  2. xcode7 免费真机调试

    原文链接:http://www.cnblogs.com/tandaxia/p/4839997.html 刚新安装了Xcode7 Version 7.1 beta , 据说这个版本可以免费真机调试,于是 ...

  3. August 23rd 2016 Week 35th Tuesday

    The very essence of romance is uncertainty. 浪漫的精髓就在于它充满种种可能. And the uncertainty of life may be also ...

  4. Warp divergence

    Threads are executed in warps of 32, with all threads in the warp executing the same instruction at ...

  5. 解决win10无法完成更新 正在撤销更改

    删除Windows 更新缓存文件按Windows+X,选择“命令提示符(管理员)”:输入:net stop wuauserv,回车(此处会提醒服务停止):输入: %windir%\SoftwareDi ...

  6. python处理html的table标签

    转载:http://www.xuebuyuan.com/583071.html python处理html的table标签 2012年01月06日 ⁄ 综合 ⁄ 共 5279字 ⁄ 字号 小 中 大 ⁄ ...

  7. nfs server的配置 Starting NFS daemon: [FAILED]

    总结了一下是nfs server的制作过程:nfs(Network File System)其实就是说,这个机器的硬盘不够了,我要把文件放到别的服务器上去,服务器端的配置如下:首先(1)确保你的机器上 ...

  8. Google 如何修复 TrustManager 实施方式不安全的应用

    引用谷歌市场的帮助说明:https://support.google.com/faqs/answer/6346016 本文面向的是发布的应用中 X509TrustManager 接口实施方式不安全的开 ...

  9. poj 1837

    题目链接:http://poj.org/problem?id=1837 题目大意: 有一个天平,左臂右臂各长15,然后给出n,m,n代表有几个挂钩,挂钩给出负数代表在左臂的距离,正数则在右臂m代表有m ...

  10. JSON C# Class Generator ---由json字符串生成C#实体类的工具(转)

    转载地址:http://www.cnblogs.com/finesite/archive/2011/07/31/2122984.html json作为互联网上轻量便捷的数据传输格式,越来越受到重视.但 ...