zoj 2112 (主席树,树状数组套线段树)
来源:互联网 发布:mac 照片 导出 编辑:程序博客网 时间:2024/06/05 19:30
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.http://www.sn180.com/buyer/buyview/1115832741.html
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.
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.http://www.sn180.com/buyer/buyview/1115821323.html
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
Sample Output
3
6
3
6
题意:查找区间第K大值,值可修改
分析:
每一棵线段树是维护每一个序列前缀的值在任意区间的个数,
如果还是按照静态的来做的话,那么每一次修改都要遍历O(n)棵树,
时间就是O(2*M*nlogn)->TLE
考虑到前缀和,我们通过树状数组来优化,即树状数组套主席树,
每个节点都对应一棵主席树,那么修改操作就只要修改logn棵树,
o(nlognlogn+Mlognlogn)时间是可以的,
但是直接建树要nlogn*logn(10^7)会MLE
我们发现对于静态的建树我们只要nlogn个节点就可以了,
而且对于修改操作,只是修改M次,每次改变俩个值(减去原先的,加上现在的)
也就是说如果把所有初值都插入到树状数组里是不值得的,
所以我们分两部分来做,所有初值按照静态来建,内存O(nlogn),
而修改部分保存在树状数组中,每次修改logn棵树,每次插入增加logn个节点
代码:
- zoj 2112 (主席树,树状数组套线段树)
- zoj 2112 (主席树,树状数组套线段树)
- ZOJ 2112 [树状数组套主席树]
- bzoj 1901 ZOJ 2112 Dynamic Rankings [树状数组套主席树] [线段树套平衡树]
- ZOJ 2112 Dynamic Rankings(树状数组套主席树)
- ZOJ 2112 Dynamic Rankings(主席树套树状数组+静态主席树)
- ZOJ 2112 Dynamic Rankings [树状数组套主席树]
- zoj 2112(主席树套树状数组+优化)
- ZOJ 2112 Dynamic Rankings (动态第k大,树状数组套主席树)★★
- ZOJ 2112 Dynamic Rankings (动态第k大,树状数组套主席树)
- zoj 2112 Dynamic Rankings(动态第k大,树状数组套主席树)
- 主席树&树状数组套主席树
- ZOJ 2112 Dynamic Rankings 树状数组套主席树 动态第K大
- ZOJ 2112 Dynamic Rankings [树状数组套主席树 || CDQ分治&整体二分]
- zoj 2112 树状数组 套主席树 动态求区间 第k个数
- 动态区间第k小(主席树+线段树套树状数组)
- bzoj 1901(主席树套树状数组)
- bzoj1901 [ Zju2112 ] --树状数组套主席树
- JVM的结构及内容分配
- HashMap中的indexFor方法分析
- 用jDB 调试Java程序
- Spring2.5的新特性:第一部分
- hadoop JOB的性能优化实践
- zoj 2112 (主席树,树状数组套线段树)
- 算法排序(2):插入排序
- MySql命令行
- JavaScript框架之AngularJS学习——双向数据绑定
- hadoop分布式平台优化
- ccf 游戏 bfs
- 分清tcp/ip与http
- sql中having和where的区别
- 树形菜单构造