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

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

代码(及模板):

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<algorithm>
  4. #include<cstring>
  5. using namespace std;
  6. #define MAXN 60010
  7. #define M 2500010
  8. int n,q,m,tot;
  9. int a[MAXN],t[MAXN];
  10. int T[MAXN],lc[M],rc[M],c[M];
  11. int S[MAXN];
  12. struct data{
  13. int kind;int l,r,k;
  14. }query[10010];
  15. void init_hash(int num)
  16. {
  17. sort(t,t+num);
  18. m=unique(t,t+num)-t;
  19. }
  20. int hash(int x)
  21. {
  22. return lower_bound(t,t+m,x)-t;
  23. }
  24. int build(int l,int r)
  25. {
  26. int root=tot++;
  27. c[root]=0;
  28. if (l!=r)
  29. {
  30. int mid=(l+r)/2;
  31. lc[root]=build(l,mid);
  32. rc[root]=build(mid+1,r);
  33. }
  34. return root;
  35. }
  36. int insert(int root,int pos,int val)
  37. {
  38. int newroot=tot++,tmp=newroot;
  39. int l=0,r=m-1;
  40. c[newroot]=c[root]+val;
  41. while (l<r)
  42. {
  43. int mid=(l+r)>>1;
  44. if (pos<=mid)
  45. {
  46. lc[newroot]=tot++; rc[newroot]=rc[root];
  47. newroot=lc[newroot]; root=lc[root];
  48. r=mid;
  49. }
  50. else
  51. {
  52. rc[newroot]=tot++; lc[newroot]=lc[root];
  53. newroot=rc[newroot]; root=rc[root];
  54. l=mid+1;
  55. }
  56. c[newroot]=c[root]+val;
  57. }
  58. return tmp;
  59. }
  60. int lowbit(int x){return x&(-x);}
  61. int use[MAXN];
  62. void add(int x,int pos,int val)
  63. {
  64. while (x<=n)
  65. {
  66. S[x]=insert(S[x],pos,val);
  67. x+=lowbit(x);
  68. }
  69. }
  70. int sum(int x)
  71. {
  72. int re=0;
  73. while (x>0)
  74. {
  75. re+=c[lc[use[x]]];
  76. x-=lowbit(x);
  77. }
  78. return re;
  79. }
  80. int Query(int L,int R,int k)
  81. {
  82. int l_root=T[L-1];
  83. int r_root=T[R];
  84. int l=0,r=m-1;
  85. for (int i=L-1; i; i-=lowbit(i)) use[i]=S[i];
  86. for (int i=R; i; i-=lowbit(i)) use[i]=S[i];
  87. while (l<r)
  88. {
  89. int mid=(l+r)>>1;
  90. int tmp=sum(R)-sum(L-1)+c[lc[r_root]]-c[lc[l_root]];
  91. if (tmp>=k)
  92. {
  93. r=mid;
  94. for (int i=L-1; i; i-=lowbit(i)) use[i]=lc[use[i]];
  95. for (int i=R; i; i-=lowbit(i)) use[i]=lc[use[i]];
  96. l_root=lc[l_root];
  97. r_root=lc[r_root];
  98. }
  99. else
  100. {
  101. l=mid+1;k-=tmp;
  102. for (int i=L-1; i; i-=lowbit(i)) use[i]=rc[use[i]];
  103. for (int i=R; i; i-=lowbit(i)) use[i]=rc[use[i]];
  104. l_root=rc[l_root];
  105. r_root=rc[r_root];
  106. }
  107. }
  108. return l;
  109. }
  110. void Modify(int x,int p,int d)
  111. {
  112. while (x<=n)
  113. {
  114. S[x]=insert(S[x],p,d);
  115. x+=lowbit(x);
  116. }
  117. }
  118. int main()
  119. {
  120. int test;
  121. scanf("%d",&test);
  122. while (test--)
  123. {
  124. scanf("%d%d",&n,&q);
  125. tot=0;m=0;
  126. for (int i=1; i<=n; i++)
  127. {
  128. scanf("%d",&a[i]);
  129. t[m++]=a[i];
  130. }
  131. char opt[10];
  132. for (int i=0; i<q; i++)
  133. {
  134. scanf("%s",opt);
  135. if (opt[0]=='Q')
  136. {
  137. query[i].kind=0;
  138. scanf("%d%d%d",&query[i].l,&query[i].r,&query[i].k);
  139. }
  140. else
  141. {
  142. query[i].kind=1;
  143. scanf("%d%d",&query[i].l,&query[i].r);
  144. t[m++]=query[i].r;
  145. }
  146. }
  147. init_hash(m);
  148. T[0]=build(0,m-1);
  149. for (int i=1; i<=n; i++)
  150. T[i]=insert(T[i-1],hash(a[i]),1);
  151. for (int i=1; i<=n; i++)
  152. S[i]=T[0];
  153. for (int i=0; i<q; i++)
  154. {
  155. if (query[i].kind==0)
  156. printf("%d\n",t[Query(query[i].l,query[i].r,query[i].k)]);
  157. else
  158. {
  159. Modify(query[i].l,hash(a[query[i].l]),-1);
  160. Modify(query[i].l,hash(query[i].r),1);
  161. a[query[i].l]=query[i].r;
  162. }
  163. }
  164. }
  165. return 0;
  166. }

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. 第8课 goto 和 void 分析

    1. 遭人遗弃的goto (1)高手潜规则:禁用goto (2)项目经验:程序质量与goto出现的次数成反比 (3)最后的判决:将goto打入冷宫(1)循环语句的基本工作方式 [实例分析]goto副作 ...

  2. Jenkins遇到问题一:jenkins配置权限不对导致无法登陆或者空白页面解决办法

    找到.jenkins/config.xml文件:替换为:1.<authorizationStrategy class="hudson.security.AuthorizationStr ...

  3. Apache Shiro(安全框架)

    当前常用流行的安全框架主要有两种:一个是Apache Shiro:另一个是Springsource. 现在介绍一下apache shiro: 既然是安全框架,解决的肯定是权限的 控制.所谓权限是指:用 ...

  4. js 与ios 交互的三种方法

    第一种:IOS拦截url  实现跳转 参考链接:http://www.cnblogs.com/pengyingh/articles/2354381.html IOS9.0 及以上支持 第二种:IOS ...

  5. HttpClient, 使用C#操作Web

    我们知道, .Net类库里提供了HttpWebRequest等类,方便我们编程与Web服务器进行交互. 但是实际使用中我们经常会遇到以下需求,基础类里没有直接提供相应的功能(WebClient类包含这 ...

  6. Caffe学习系列(12):训练和测试自己的图片

    学习caffe的目的,不是简单的做几个练习,最终还是要用到自己的实际项目或科研中.因此,本文介绍一下,从自己的原始图片到lmdb数据,再到训练和测试模型的整个流程. 一.准备数据 有条件的同学,可以去 ...

  7. 数据库服务器的安装 (MySQL Server 5.7) :

    MySQL 和 MariaDB 都是 Ubuntu 16.04 中的数据库服务器. MySQL Server 和 MariaDB Server的安装包都可以在Ubuntu 的默认软件源中找到,我们可以 ...

  8. LeetCode Question Difficulty Distribution

    参考链接:https://docs.google.com/spreadsheet/pub?key=0Aqt--%20wSNYfuxdGxQWVFsOGdVVWxQRlNUVXZTdEpOeEE& ...

  9. jquery实现文件异步上传

    前言 这里用了2个JS插件,一个是Jquery原生js,我的版本是jquery-1.7.2.min.js,另一个是jquery.form.js.这个form.js 是关键,不可少哦.另外, 我的服务器 ...

  10. Linq动态查询简易解决之道(原创)

    因为项目需要使用Linq来查询数据,但是在多条件查询时,需要使用一大堆if(...!=string.empty)等判断条件感觉不是很优雅.网上搜索以下,大概找到了两种办法,一种是老外写的一个类,感觉用 ...