ZOJ 2112 Dynamic Rankings(树状数组+主席树)
The Company Dynamic Rankings has developed a new kind of computer that is no longer satisfied with the query like to simply find the k-th smallest number of the given N numbers. They have developed a more powerful system such that for N numbers a[1], a[2], ..., a[N], you can ask it like: what is the k-th smallest number of a[i], a[i+1], ..., a[j]? (For some i<=j, 0<k<=j+1-i that you have given to it). More powerful, you can even change the value of some a[i], and continue to query, all the same.
Your task is to write a program for this computer, which
- Reads N numbers from the input (1 <= N <= 50,000)
- Processes M instructions of the input (1 <= M <= 10,000). These instructions include querying the k-th smallest number of a[i], a[i+1], ..., a[j] and change some a[i] to t.
Input
The first line of the input is a single number X (0 < X <= 4), the number of the test cases of the input. Then X blocks each represent a single test case.
The first line of each block contains two integers N and M, representing N numbers and M instruction. It is followed by N lines. The (i+1)-th line represents the number a[i]. Then M lines that is in the following format
Q i j k or
C i t
It represents to query the k-th number of a[i], a[i+1], ..., a[j] and change some a[i] to t, respectively. It is guaranteed that at any time of the operation. Any number a[i] is a non-negative integer that is less than 1,000,000,000.
There're NO breakline between two continuous test cases.
<b< dd="">
Output
For each querying operation, output one integer to represent the result. (i.e. the k-th smallest number of a[i], a[i+1],..., a[j])
There're NO breakline between two continuous test cases.
<b< dd="">
Sample Input
2
5 3
3 2 1 4 7
Q 1 4 3
C 2 6
Q 2 5 3
5 3
3 2 1 4 7
Q 1 4 3
C 2 6
Q 2 5 3
题解:
树状数组的每个节点都是一颗线段树,但这棵线段树不再保存每个前缀的信息了,而是由树状数组的sum函数计算出这个前缀的信息,那么显而易见这棵线段树保存的是辅助数组S的值,即S=A[i-lowbit+1]+...+A[i],其中A[i]表示值为i的元素出现的次数。
那么对于每次修改,我们要修改树状数组上的logn棵树,对于每棵树,我们要修改logn个结点,所以时空复杂度为
O((n+q)*logn*logn),由于这道题n比较大,查询次数q比较小,所以我们可以初始时建立一颗静态的主席树,树状数组只保存每次修改的信息,那么时空复杂度降为了O(n*logn+q*logn*logn)
参考代码:
#include<bits/stdc++.h>
using namespace std;
#define lowbit(x) (x&-x)
typedef long long ll;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-') f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=(x<<)+(x<<)+ch-'';ch=getchar();}
return x*f;
}
const int maxn=6e4+;
struct Tree{
int ls,rs;
int sum;
} node[maxn*]; struct Qnode{
bool flag;
int l,r,k;
} qn[maxn]; int T,n,q,a[maxn],b[maxn],num,rt[maxn];
char str[];
int ul[maxn],ur[maxn],ca[maxn],s[maxn],cnt; void Build(int rt,int l,int r)
{
rt=++cnt;
node[rt].sum=;
if(l==r) return ;
int mid=l+r>>;
Build(node[rt].ls,l,mid);
Build(node[rt].rs,mid+,r);
} void Update(int y,int &x,int l,int r,int pos,int val)
{
node[++cnt]=node[y];node[cnt].sum+=val;x=cnt;
if(l==r) return ;
int mid=l+r>>;
if(pos<=mid) Update(node[y].ls,node[x].ls,l,mid,pos,val);
else Update(node[y].rs,node[x].rs,mid+,r,pos,val);
} void Add(int x,int val)
{
int res=lower_bound(b+,b++num,a[x])-b;
while(x<=n)
{
Update(s[x],s[x],,num,res,val);
x+=lowbit(x);
}
} int Sum(int x,bool temp)
{
int res=;
while(x>)
{
if(temp) res+=node[node[ur[x]].ls].sum;
else res+=node[node[ul[x]].ls].sum;
x-=lowbit(x);
}
return res;
} int Query(int L,int R,int x,int y,int l,int r,int k)
{
if(l==r) return l;
int mid=l+r>>;
int res=Sum(R,true)-Sum(L,false)+node[node[y].ls].sum-node[node[x].ls].sum;
if(k<=res)
{
for(int i=R;i;i-=lowbit(i)) ur[i]=node[ur[i]].ls;
for(int i=L;i;i-=lowbit(i)) ul[i]=node[ul[i]].ls;
return Query(L,R,node[x].ls,node[y].ls,l,mid,k);
}
else
{
for(int i=R;i;i-=lowbit(i)) ur[i]=node[ur[i]].rs;
for(int i=L;i;i-=lowbit(i)) ul[i]=node[ul[i]].rs;
return Query(L,R,node[x].rs,node[y].rs,mid+,r,k-res);
}
} int main()
{
T=read();
while(T--)
{
n=read();q=read(); cnt=num=;
memset(rt,,sizeof(rt));
for(int i=;i<=n;++i) a[i]=read(),b[++num]=a[i];
for(int i=;i<=q;++i)
{
scanf("%s",str);
if(str[]=='Q')
{
qn[i].flag=true;
qn[i].l=read();qn[i].r=read();qn[i].k=read();
}
else
{
qn[i].flag=false;
qn[i].l=read();qn[i].r=read();b[++num]=qn[i].r;
}
}
sort(b+,b++num);
int tot=unique(b+,b++num)-b-;
num=tot;
for(int i=;i<=n;++i) ca[i]=lower_bound(b+,b++num,a[i])-b;
Build(rt[],,num);
for(int i=;i<=n;++i) Update(rt[i-],rt[i],,num,ca[i],);
for(int i=;i<=n;++i) s[i]=rt[];
for(int i=;i<=q;++i)
{
if(qn[i].flag)
{
for(int j=qn[i].r;j;j-=lowbit(j)) ur[j]=s[j];
for(int j=qn[i].l-;j;j-=lowbit(j)) ul[j]=s[j];
printf("%d\n",b[Query(qn[i].l-,qn[i].r,rt[qn[i].l-],rt[qn[i].r],,num,qn[i].k)]);
}
else
{
Add(qn[i].l,-);
a[qn[i].l]=qn[i].r;
Add(qn[i].l,);
}
}
} return ;
}
ZOJ 2112 Dynamic Rankings(树状数组+主席树)的更多相关文章
- ZOJ - 2112 Dynamic Rankings(BIT套主席树)
纠结了好久的一道题,以前是用线段树套平衡树二分做的,感觉时间复杂度和分块差不多了... 终于用BIT套函数式线段树了过了,120ms就是快,此题主要是卡内存. 假设离散后有ns个不同的值,递归层数是l ...
- BZOJ_1901_Zju2112 Dynamic Rankings_树状数组+主席树
BZOJ_1901_Zju2112 Dynamic Rankings_树状数组+主席树 题意: 给定一个含有n个数的序列a[1],a[2],a[3]……a[n],程序必须回答这样的询问:对于给定的i, ...
- zoj2112 树状数组+主席树 区间动第k大
Dynamic Rankings Time Limit: 10000MS Memory Limit: 32768KB 64bit IO Format: %lld & %llu Subm ...
- 【bzoj1146】[CTSC2008]网络管理Network 倍增LCA+dfs序+树状数组+主席树
题目描述 M公司是一个非常庞大的跨国公司,在许多国家都设有它的下属分支机构或部门.为了让分布在世界各地的N个部门之间协同工作,公司搭建了一个连接整个公司的通信网络.该网络的结构由N个路由器和N-1条高 ...
- 【bzoj3744】Gty的妹子序列 分块+树状数组+主席树
题目描述 我早已习惯你不在身边, 人间四月天 寂寞断了弦. 回望身后蓝天, 跟再见说再见…… 某天,蒟蒻Autumn发现了从 Gty的妹子树(bzoj3720) 上掉落下来了许多妹子,他发现 她们排成 ...
- BZOJ_2120_数颜色_Set+树状数组+主席树
BZOJ_2120_数颜色_Set+树状数组+主席树 Description 墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会像你发布如下指令: 1. Q L ...
- P1972 [SDOI2009]HH的项链[离线+树状数组/主席树/分块/模拟]
题目背景 无 题目描述 HH 有一串由各种漂亮的贝壳组成的项链.HH 相信不同的贝壳会带来好运,所以每次散步完后,他都会随意取出一段贝壳,思考它们所表达的含义.HH 不断地收集新的贝壳,因此,他的项链 ...
- ZOJ 2112 Dynamic Rankings(树状数组+主席树)
题意 \(n\) 个数,\(m\) 个操作,每次操作修改某个数,或者询问某个区间的第 \(K\) 小值. \(1 \leq n \leq 50000\) \(1 \leq m \leq 10000\) ...
- [luogu2617][bzoj1901][Zju2112]Dynamic Rankings【树套树+树状数组+主席树】
题目网址 [传送门] 题目大意 请你设计一个数据结构,支持单点修改,区间查询排名k. 感想(以下省略脏话inf个字) 真的强力吹爆洛谷数据,一般的树套树还给我T了一般的点,加强的待修主席树还给我卡了几 ...
随机推荐
- javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: ResultSet is from UPDATE. No Data.
Java jpa调用存储过程,抛出异常如下: javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCEx ...
- 6.2.2 辅助类GenericOptionsParser,Tool和ToolRunner深入解析
辅助类GenericOptionsParser,Tool和ToolRunner (1)为什么要用ToolRunner 将MapReduce Job配置参数写到java代码里,一旦变更意味着修改java ...
- Batch批处理获取当前时间
这不是一个新问题,但是由于网上写的都是针对自己的电脑设置,没有通用性,而我呢,又需要在不同电脑上使用,因此,这命题一个问题了.其实也没有什么好说的,直接上代码. @ECHO OFF set split ...
- java编程思想第四版第五章总结
1. 构造器 构造器的一个重要的作用: 保证对象被使用之前初始化了. 构造器是一种特殊类型的方法, 因为他没有返回值.这与返回值为空(void)明显不同.对于空返回值,尽管方法本身不会自动返回什么, ...
- 关于配置Nginx反向代理后SpringSecurity认证失败的问题解决
问题背景 最近在写的一个项目,采用前后端分离的方式进行开发,登录认证使用的是SpringSecurity框架. 问题描述 在项目部署的时候出现了一个问题,在自己电脑上运行的时候一切顺畅,可是部署到服务 ...
- AsyncDisplayKit编译和使用注意事项
Facebook开源框架,在github上可下载到.首先要编译AsyncDisplayKit库项目,有可能会出现下面错误: cocoaPods是基于ruby的项目版本控制软件,如果是ruby新手就会不 ...
- Oracle10g安装步骤(一)
本例使用安装程序:10201_database_win32 首先将所有文件提取解压出来后,执行setup.exe 安装步骤如下:
- HTTP 协议漫谈
转载出处:HTTP 协议漫谈 简介 网络上已经有不少介绍 HTTP 的好文章,对HTTP的一些细节介绍的比较好,所以本篇文章不会对 HTTP 的细节进行深究,而是从够高和更结构化的角度将 HTTP 协 ...
- 初探three.js光源
上一节我们简单的说了一下THREE中必要的元素.今天我们深入探讨一下各种THREE的光源. 一 基础光源 基础光源有4种1.THREE.AmbientLight(环境光源)2.THREE.PointL ...
- nginx支持wss配置
nginx证书 nginx.conf配置