Data Structure?

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Problem Description
Data structure is one of the basic skills for Computer Science students, which is a particular way of storing and organizing data in a computer so that it can be used efficiently. Today let me introduce a data-structure-like problem for you.
Original, there are N numbers, namely 1, 2, 3...N. Each round, iSea find out the Ki-th smallest number and take it away, your task is reporting him the total sum of the numbers he has taken away.
 
Input
The first line contains a single integer T, indicating the number of test cases.
Each test case includes two integers N, K, K indicates the round numbers. Then a line with K numbers following, indicating in i (1-based) round, iSea take away the Ki-th smallest away.

Technical Specification
1. 1 <= T <= 128
2. 1 <= K <= N <= 262 144
3. 1 <= Ki <= N - i + 1

 
Output
For each test case, output the case number first, then the sum.
 
Sample Input
2
3 2
1 1
10 3
3 9 1
 
Sample Output
Case 1: 3
Case 2: 14
 
Author
iSea@WHU
 
Source
 思路:taobanzi;
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define pi (4*atan(1.0))
  5. #define eps 1e-14
  6. const int N=3e5+,M=4e6+,inf=1e9+,mod=1e9+;
  7. const ll INF=1e18+;
  8. int tree[N],n,k;
  9. int lowbit(int x)
  10. {
  11. return x&-x;
  12. }
  13. void update(int x,int change)
  14. {
  15. while(x<=n)
  16. {
  17. tree[x]+=change;
  18. x+=lowbit(x);
  19. }
  20. }
  21. int k_thfind(int K)//树状数组求第K小
  22. {
  23. int sum=;
  24. for(int i=;i>=;i--)
  25. {
  26. if(sum+(<<i)<=n&&tree[sum+(<<i)]<K)
  27. {
  28. K-=tree[sum+(<<i)];
  29. sum+=<<i;
  30. }
  31. }
  32. return sum+;
  33. }
  34. int main(){
  35. int T,cas=;
  36. scanf("%d",&T);
  37. while(T--)
  38. {
  39. memset(tree,,sizeof(tree));
  40. scanf("%d%d",&n,&k);
  41. for(int i=;i<=n;i++)
  42. update(i,);
  43. ll ans=;
  44. for(int i=;i<k;i++)
  45. {
  46. int z;
  47. scanf("%d",&z);
  48. int v=k_thfind(z);
  49. ans+=v;
  50. update(v,-);
  51. }
  52. printf("Case %d: %lld\n",cas++,ans);
  53. }
  54. return ;
  55. }

hdu 4217 Data Structure? 树状数组求第K小的更多相关文章

  1. 树状数组求第k小的元素

    int find_kth(int k) { int ans = 0,cnt = 0; for (int i = 20;i >= 0;i--) //这里的20适当的取值,与MAX_VAL有关,一般 ...

  2. 树状数组求第K小值 (spoj227 Ordering the Soldiers &amp;&amp; hdu2852 KiKi&#39;s K-Number)

    题目:http://www.spoj.com/problems/ORDERS/ and pid=2852">http://acm.hdu.edu.cn/showproblem.php? ...

  3. UVA11525 Permutation[康托展开 树状数组求第k小值]

    UVA - 11525 Permutation 题意:输出1~n的所有排列,字典序大小第∑k1Si∗(K−i)!个 学了好多知识 1.康托展开 X=a[n]*(n-1)!+a[n-1]*(n-2)!+ ...

  4. *HDU2852 树状数组(求第K小的数)

    KiKi's K-Number Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  5. poj 2985 The k-th Largest Group 树状数组求第K大

    The k-th Largest Group Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 8353   Accepted ...

  6. HDU 5249 离线树状数组求第k大+离散化

    KPI Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  7. hdu 5147 Sequence II (树状数组 求逆序数)

    题目链接 Sequence II Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  8. POJ2985 The k-th Largest Group[树状数组求第k大值+并查集||treap+并查集]

    The k-th Largest Group Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 8807   Accepted ...

  9. hdu 2838 Cow Sorting 树状数组求所有比x小的数的个数

    Cow Sorting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

随机推荐

  1. HTML data属性简介以及低版本浏览器兼容算法

    实例 使用 data-* 属性来嵌入自定义数据: <ul> <li data-animal-type="bird">Owl</li> <l ...

  2. 160928、JQuery解析XML数据的demo

    用JavaScript解析XML数据是常见的编程任务,JavaScript能做的,JQuery当然也能做.下面我们来总结几个使用JQuery解析XML的例子. 方案1 当后台返回的数据类型是xml对象 ...

  3. JavaScript调试技巧之console.log()详解

    JavaScript调试技巧之console.log()详解 对于JavaScript程序的调试,相比于alert(),使用console.log()是一种更好的方式,原因在于:alert()函数会阻 ...

  4. [转]How Can I Find Out What Is Using a Busy or Reserved Serial Port?

    转自:http://digital.ni.com/public.nsf/allkb/29B079481C5ECE76862578810082394E How Can I Find Out What I ...

  5. AJAX 数据库实例

    AJAX 用于创建动态性更强的应用程序. AJAX ASP 实例 下面的例子将演示当用户在输入框中键入字符时,网页如何与服务器进行通信: 实例 请在下面的输入框中键入字母(A - Z): 姓名: 建议 ...

  6. oracle监听服务无法打开

    原因: 修改了主机名,但没有修改监听文件listener.ora里面的配置. 解决办法:修改HOST与主机名相同 LISTENER = (DESCRIPTION_LIST = (DESCRIPTION ...

  7. php两种include加载文件方式效率比较如下

    1)定义一个字符串变量,里面保存要加载的文件列表.然后foreach加载. $a = '/a.class.php;/Util/b.class.php;/Util/c.class.php'; $b = ...

  8. Linux命令学习手册-printf命令(转)

    分类: LINUX 参考资料:http://sns.linuxpk.com/space-566-do-blog-id-15819.html printf FORMAT [ARGUMENT]... pr ...

  9. ACM题目————小A的计算器

    Description 以往的操作系统内部的数据表示都是二进制方式,小A新写了一个操作系统,系统内部的数据表示为26进制,其中0-25分别由a-z表示.  现在小A要在这个操作系统上实现一个计算器,这 ...

  10. json返回日期格式化的解决

    function jsonDateFormat(jsonDate) {//json日期格式转换为正常格式 try { var date = new Date(parseInt(jsonDate.rep ...