1901: Zju2112 Dynamic Rankings

Time Limit: 10 Sec Memory Limit: 128 MB

Submit: 6058 Solved: 2521

[Submit][Status][Discuss]

Description

给定一个含有n个数的序列a[1],a[2],a[3]……a[n],程序必须回答这样的询问:对于给定的i,j,k,在a[i],a[i+1],a[i+2]……a[j]中第k小的数是多少(1≤k≤j-i+1),并且,你可以改变一些a[i]的值,改变后,程序还能针对改变后的a继续回答上面的问题。你需要编一个这样的程序,从输入文件中读入序列a,然后读入一系列的指令,包括询问指令和修改指令。对于每一个询问指令,你必须输出正确的回答。 第一行有两个正整数n(1≤n≤10000),m(1≤m≤10000)。分别表示序列的长度和指令的个数。第二行有n个数,表示a[1],a[2]……a[n],这些数都小于10^9。接下来的m行描述每条指令,每行的格式是下面两种格式中的一种。 Q i j k 或者 C i t Q i j k (i,j,k是数字,1≤i≤j≤n, 1≤k≤j-i+1)表示询问指令,询问a[i],a[i+1]……a[j]中第k小的数。C i t (1≤i≤n,0≤t≤10^9)表示把a[i]改变成为t。

Input

对于每一次询问,你都需要输出他的答案,每一个输出占单独的一行。

Output

Sample Input

5 3

3 2 1 4 7

Q 1 4 3

C 2 6

Q 2 5 3

Sample Output

3

6

HINT

20%的数据中,m,n≤100; 40%的数据中,m,n≤1000; 100%的数据中,m,n≤10000。

Source

动态维护区间第K小的模板题...套模板即可A...

代码(及模板):

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define MAXN 60010
#define M 2500010
int n,q,m,tot;
int a[MAXN],t[MAXN];
int T[MAXN],lc[M],rc[M],c[M];
int S[MAXN];
struct data{
int kind;int l,r,k;
}query[10010]; void init_hash(int num)
{
sort(t,t+num);
m=unique(t,t+num)-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;
lc[root]=build(l,mid);
rc[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)
{
lc[newroot]=tot++; rc[newroot]=rc[root];
newroot=lc[newroot]; root=lc[root];
r=mid;
}
else
{
rc[newroot]=tot++; lc[newroot]=lc[root];
newroot=rc[newroot]; root=rc[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 re=0;
while (x>0)
{
re+=c[lc[use[x]]];
x-=lowbit(x);
}
return re;
} int Query(int L,int R,int k)
{
int l_root=T[L-1];
int r_root=T[R];
int l=0,r=m-1;
for (int i=L-1; i; i-=lowbit(i)) use[i]=S[i];
for (int i=R; i; i-=lowbit(i)) use[i]=S[i];
while (l<r)
{
int mid=(l+r)>>1;
int tmp=sum(R)-sum(L-1)+c[lc[r_root]]-c[lc[l_root]];
if (tmp>=k)
{
r=mid;
for (int i=L-1; i; i-=lowbit(i)) use[i]=lc[use[i]];
for (int i=R; i; i-=lowbit(i)) use[i]=lc[use[i]];
l_root=lc[l_root];
r_root=lc[r_root];
}
else
{
l=mid+1;k-=tmp;
for (int i=L-1; i; i-=lowbit(i)) use[i]=rc[use[i]];
for (int i=R; i; i-=lowbit(i)) use[i]=rc[use[i]];
l_root=rc[l_root];
r_root=rc[r_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()
{
int test;
scanf("%d",&test);
while (test--)
{
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 opt[10];
for (int i=0; i<q; i++)
{
scanf("%s",opt);
if (opt[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;
}

BZOJ-1901 Zju2112 Dynamic Rankings 函数式线段树 套 树状数组+离线处理的更多相关文章

  1. BZOJ 1901: Zju2112 Dynamic Rankings[带修改的主席树]【学习笔记】

    1901: Zju2112 Dynamic Rankings Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 7143  Solved: 2968[Su ...

  2. Bzoj 1901: Zju2112 Dynamic Rankings 树套树,线段树,平衡树,Treap

    1901: Zju2112 Dynamic Rankings Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 6471  Solved: 2697[Su ...

  3. bzoj 1901: Zju2112 Dynamic Rankings(树套树)

    1901: Zju2112 Dynamic Rankings 经典的带改动求区间第k小值问题 树套树模板,我是用的线段树套splay实现的,并且用的数组模拟的,所以可能空间略大,bzoj过了,zoj过 ...

  4. BZOJ 1901: Zju2112 Dynamic Rankings( 树状数组套主席树 )

    裸的带修改主席树.. 之前用BIT套Splay( http://www.cnblogs.com/JSZX11556/p/4625552.html )A过..但是还是线段树好写...而且快(常数比平衡树 ...

  5. Bzoj 1901: Zju2112 Dynamic Rankings 主席树,可持久,树状数组,离散化

    1901: Zju2112 Dynamic Rankings Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 6321  Solved: 2628[Su ...

  6. bzoj 1901: Zju2112 Dynamic Rankings -- 主席树,树状数组,哈希

    1901: Zju2112 Dynamic Rankings Time Limit: 10 Sec  Memory Limit: 128 MB Description 给定一个含有n个数的序列a[1] ...

  7. BZOJ 1901 Zju2112 Dynamic Rankings

    树阵主席设置树.维护间隔动态K大. .. ZOJ到空间太小,太大,仅仅能到BZOJ上交 1901: Zju2112 Dynamic Rankings Time Limit: 10 Sec  Memor ...

  8. BZOJ 1901: Zju2112 Dynamic Rankings( BIT 套 BST )

    BIT 套 splay 其实也是不难...每个 BIT 的结点保存一颗 splay , 询问就二分答案然后判断rank... ------------------------------------- ...

  9. BZOJ 1901: Zju2112 Dynamic Rankings 区间k大 带修改 在线 线段树套平衡树

    之前写线段树套splay数组版..写了6.2k..然后弃疗了.现在发现还是很水的..嘎嘎.. zju过不了,超时. upd:才发现zju是多组数据..TLE一版才发现.然后改了,MLE...手写内存池 ...

随机推荐

  1. 三维网格去噪算法(bilateral filter)

    受图像双边滤波算法的启发,[Fleishman et al. 2003]和[Jones et al. 2003]分别提出了利用双边滤波算法对噪声网格进行光顺去噪的算法,两篇文章都被收录于当年的SIGG ...

  2. NVIDIA Physix Unity3D

    提升机器的3D性能 在公司用的台式机看配置不会很差,但是在处理3D方面特别地无奈!例如开个PS,3d MAX就会卡的半死,再多开一会儿就直接未响应,然后机器重启. 真无奈啊,公司暂时也不会给我换电脑或 ...

  3. Linux 删除文件中某一行的方法

    如果有一个abc.txt文件,内容是: aaa bbb ccc ddd eee fff 如果要删除aaa,那么脚本可以这样写: sed -i '/aaa/d' abc.txt 如果删除的是一个变量的值 ...

  4. linux下删除文件名乱码文件

    linux下通过rm命令来删除文件,但是如果要删除文件名乱码的文件,就不能直接使用rm命令了,因为压根就无法输出文件名来.不过借助find命令可以实现对其删除.在linux下对于每个文件都一个对应的不 ...

  5. QTP和WinRunner区别

    QTP,全称为Quick Test Professional,它与WinRunner同为MI公司开发的功能强大的功能测试工具.从时间上来看,WinRunner在1995年便已经推出,远早于QTP,而Q ...

  6. java 20 -2 递归之找特定目录下的特定格式文件

    /* 需求:把C:\Users\Administrator\Desktop\记录目录下所有以.java结尾的文件的绝对路径输出到控制台 分析: A:封装该目录 B:获取该目录下的所有文件或文件夹的Fi ...

  7. 搞懂function(*args,**kwargs)

    给出一个例子: def foo(*args,**kwargs): print 'args=',args print 'kwargs=',kwargs print '------------------ ...

  8. 4201 TortoiseSVN常用配置

    在Windows下推荐使用乌龟(Tortoise)SVN客户端. TortoiseSVN 是 Subversion 版本控制系统的一个免费开源客户端,可以超越时间的管理文件和目录.文件保存在中央版本库 ...

  9. Python Web实战 - 基于Flask实现的黄金点游戏

    一.简介 团队成员: 领航者:张旭 驾驶员:张国庆 项目简介: 项目名称:基于B/S模式的黄金点游戏 采用技术: 后端:Python + Sqlite3 前端:HTML + CSS + JS + Bo ...

  10. UIButton利用分类扩展方法(封装)

    UIButton+BackgroundColor.h #import <UIKit/UIKit.h> @interface UIButton (BackgroundColor) - (vo ...