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. 161207、高并发:java.util.concurrent.Semaphore实现字符串池及其常用方法介绍

    实现字符串池: StrPool.java import java.util.ArrayList; import java.util.List; import java.util.concurrent. ...

  2. 160906、Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)

    互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架,在这种情况下诞生的.现在核心业务抽取出来,作为独立的服务,使 ...

  3. simpleBLEPeripheral.c 文件分析

    这个配置或者说任务, 让这个蓝牙设备成为了一个简单的BLE外设. 这里定义了外设的广播数据, 以及最重要, char被改变之后的回调, 引出后来的coreHandler里面的, ack 以及写e2pr ...

  4. redis make test报错 Test replication partial resync: ok psync

    更改 tests/integration/replication-psync.tcl 文件: vi tests/integration/replication-psync.tcl 把对应报错的那段代码 ...

  5. Hibernate解决n+1问题

    观点:对于n+1问题的理解. 一般而言说n+1意思是,无论在一对多还是多对一当查询出n条数据之后,每条数据会关联的查询1次他的关联对象,这就叫做n+1. 但是我的理解是,本来所有信息可以一次性查询出来 ...

  6. ectouch第二讲之 文件结构

    相信大家在ectouch官网都注意到了,ectouch采用的MVC框架,之前一直以为它用的和ecshop一样都是smarty,本鸟默默按照smarty的文件结构研究了好几天,结果是各种文件对不上号.无 ...

  7. 如何在ecshop订单中显示客户给商家的留言

    ecshop后台->订单管理->订单打印 在你想要加入的位置加入 客户给商家的留言:{$order.postscript} ok啦,很简单咯

  8. AngularJS 实现的输入自动完成补充功能

    1.首先需要引入:angucomplete.js第三方库 2.增加model : var app = angular.module('app', ["angucomplete"]) ...

  9. raw_input() 与 input()

    这两个均是 python 的内建函数,通过读取控制台的输入与用户实现交互.但他们的功能不尽相同. >>> raw_input_A = raw_input("raw_inpu ...

  10. Balance

    Balance Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 11947 Accepted: 7464 Description ...