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;
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
const int N=3e5+,M=4e6+,inf=1e9+,mod=1e9+;
const ll INF=1e18+;
int tree[N],n,k;
int lowbit(int x)
{
return x&-x;
}
void update(int x,int change)
{
while(x<=n)
{
tree[x]+=change;
x+=lowbit(x);
}
}
int k_thfind(int K)//树状数组求第K小
{
int sum=;
for(int i=;i>=;i--)
{
if(sum+(<<i)<=n&&tree[sum+(<<i)]<K)
{
K-=tree[sum+(<<i)];
sum+=<<i;
}
}
return sum+;
}
int main(){
int T,cas=;
scanf("%d",&T);
while(T--)
{
memset(tree,,sizeof(tree));
scanf("%d%d",&n,&k);
for(int i=;i<=n;i++)
update(i,);
ll ans=;
for(int i=;i<k;i++)
{
int z;
scanf("%d",&z);
int v=k_thfind(z);
ans+=v;
update(v,-);
}
printf("Case %d: %lld\n",cas++,ans);
}
return ;
}

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. memcache缓存详解

    这篇文章主要介绍了PHP中的Memcache,从Memcache简介开始,详细讲解了如Memcache和memcached的区别.PHP的 Memcache所有操作方法.每个操作方法的详细解释等,需要 ...

  2. FastDFS配置说明

    前面了解了fastdfs的原理,接下来就熟悉一下安装过程,准备了三台机器,一台模拟client,一台模拟storage,一台模拟tracker.     三台机器均为debian6,系统为最小化安装, ...

  3. CentOS 7 更新源 – 使用国内 163 yum 源

    突然想起试试 Docker,在一台计算机上安装了 CentOS 7,准备开工,突然想起还需要做一件事情,更改源,不然安装肯定会很慢,网上搜索了一下,文章很多,但是会出一些问题,所以将自己的成功的日志写 ...

  4. IOS 网络请求中设置cookie

    版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[+]   1. ASIHTTPRequest ASIHTTPRequest 是一款极其强劲的 HTTP 访问开源项目.让简单的 A ...

  5. PHP将多张小图拼接成一张大图

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> < ...

  6. ectouch第七讲 之ECshop模板机制整理

    网上的资源感觉还是有些用,可以看看,帮助理解,ECshop模板机制整理原文:http://blog.sina.com.cn/s/blog_6900af430100nkn8.html 一.模板引擎: E ...

  7. textarea 在浏览器中禁用拖动和固定大小

    HTML 标签 textarea 在大部分浏览器中只要指定行(rows)和列(cols)属性,就可以规定 textarea 的尺寸,大小就不会改变,不过更好的办法是使用 CSS 的 height 和 ...

  8. HDU 2236:无题II(二分搜索+二分匹配)

    http://acm.hdu.edu.cn/showproblem.php?pid=2236 题意:中文题意. 思路:先找出最大和最小值,然后二分差值,对于每一个差值从下界开始枚举判断能不能二分匹配. ...

  9. move语义和右值引用

    C++11支持move语义,用以避免非必要拷贝和临时对象. 具体内容见收藏中的“C++右值引用” .

  10. js词法作用域规则

    function foo() {console.log( a ); // 2不是3} function bar() {var a = 3;foo();} var a = 2;bar(); js中的作用 ...