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. React 之 Hello world

    一入react深似海,从此学习为常态,react 成为了一种趋势,很多人应该很多人准备进坑,下面对react进行简单的描述: 首先学习react,要有多方学习的准备,例如:Webpack, Babel ...

  2. .Net Framework 4.0 内部排序探索

    简介 一时好奇心起,想一窥.Net Framework 4.0内部究竟是使用何种算法排序.以前听人说Framework内部是使用的快速排序,但究竟耳听为虚,眼见为实.主要通过JetBrains dot ...

  3. 分层开发MySchool总结

    由于分层之间存在各层之间的关系窗体之间的方法跳转,故有需要者可以进行下载本地文件 MySchool.rar 3304KB 5/22/2016 9:43:28 AM ,代码中有注释,

  4. Win2008R2配置WebDeploy

    一.配置服务器 1.安装管理服务 2.点击管理服务进行配置 3.安装WebDeploy 3.1通过离线安装包方式安装: https://www.iis.net/downloads/microsoft/ ...

  5. SOAP和WSDL的一些必要知识(转)

    原文地址:SOAP和WSDL的一些必要知识 SOAP和WSDL对Web Service.WCF进行深入了解的基础,因此花一些时间去了解一下是很有必要的. 一.SOAP(Simple Object Ac ...

  6. js实现倒计时 类似团购网站

    一.demo与效果展示 为节约时间,我就直接套用了企鹅团的界面作为demo的背景.因为是倒计时,所以需要一个固定的时间,为了n年后,某位仁兄打开demo页面依然在倒计时,所以我把倒计时时间设成了205 ...

  7. 目标检测——HOG特征

    1.HOG特征: 方向梯度直方图(Histogram of Oriented Gradient, HOG)特征是一种在计算机视觉和图像处理中用来进行物体检测的特征描述子.它通过计算和统计图像局部区域的 ...

  8. [CareerCup] 13.9 Aligned Malloc and Free Function 写一对申请和释放内存函数

    13.9 Write an aligned malloc and free function that supports allocating memory such that the memory ...

  9. LeetCode:Best Time to Buy and Sell Stock I II III

    LeetCode:Best Time to Buy and Sell Stock Say you have an array for which the ith element is the pric ...

  10. IOS开发之—— Core Foundation对象与OC对象相对转换的问题

    对ARC盲目依赖的同学: 1过度使用block后,无法解决循环引用问题 2遇到底层Core Foundation对象,需要自己手工管理它们的引用计数时,显得一筹莫展 first:对于底层Core Fo ...