hdu 4217 Data Structure? 树状数组求第K小
Data Structure?
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
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.
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
3 2
1 1
10 3
3 9 1
Case 2: 14
#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小的更多相关文章
- 树状数组求第k小的元素
int find_kth(int k) { int ans = 0,cnt = 0; for (int i = 20;i >= 0;i--) //这里的20适当的取值,与MAX_VAL有关,一般 ...
- 树状数组求第K小值 (spoj227 Ordering the Soldiers && hdu2852 KiKi's K-Number)
题目:http://www.spoj.com/problems/ORDERS/ and pid=2852">http://acm.hdu.edu.cn/showproblem.php? ...
- UVA11525 Permutation[康托展开 树状数组求第k小值]
UVA - 11525 Permutation 题意:输出1~n的所有排列,字典序大小第∑k1Si∗(K−i)!个 学了好多知识 1.康托展开 X=a[n]*(n-1)!+a[n-1]*(n-2)!+ ...
- *HDU2852 树状数组(求第K小的数)
KiKi's K-Number Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- poj 2985 The k-th Largest Group 树状数组求第K大
The k-th Largest Group Time Limit: 2000MS Memory Limit: 131072K Total Submissions: 8353 Accepted ...
- HDU 5249 离线树状数组求第k大+离散化
KPI Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- hdu 5147 Sequence II (树状数组 求逆序数)
题目链接 Sequence II Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- POJ2985 The k-th Largest Group[树状数组求第k大值+并查集||treap+并查集]
The k-th Largest Group Time Limit: 2000MS Memory Limit: 131072K Total Submissions: 8807 Accepted ...
- hdu 2838 Cow Sorting 树状数组求所有比x小的数的个数
Cow Sorting Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
随机推荐
- Mac OS finder : 显示和隐藏文件[夹] show and hide files or folders
Finder默认是不显示隐藏文件[夹]的,要显示出怎么办? 要显示的话,可以GUI(graphic user interface)和CLI(command line interface)两种方式 CL ...
- oracle sqlloader 用法
向oracle中导入*.csv文件 1.什么是*.csv,如何得到? 里面存放的是数据表.每行代表数据库表格的一行, 每行中,每两个数据中间由逗号","分割. *.csv可以通 ...
- win7无法通过DHCP获得IP地址
问题:win7无法通过DHCP获得IP地址(手动设置没有问题),但XP可以自动获取. 前些时候,某局域网反应部分WIN7系统无法正常从DHCP服务器(windows dhcp 服务器)获取ip地址,交 ...
- iOS:UITableView 方法 属性
参考:https://developer.apple.com/library/iOS/documentation/UIKit/Reference/UITableView_Class/Reference ...
- HDU 2236:无题II(二分搜索+二分匹配)
http://acm.hdu.edu.cn/showproblem.php?pid=2236 题意:中文题意. 思路:先找出最大和最小值,然后二分差值,对于每一个差值从下界开始枚举判断能不能二分匹配. ...
- JavaScript 网址
1. javascript 模板引擎 http://aui.github.io/artTemplate/
- 使用mysqlbinlog server远程备份binlog的脚本
#注意,备份机到远程mysql服务器需要免密钥登录,此脚本放到计划任务中每五分钟执行一次,避免mysqlbinlog server进程长时间挂掉无人知晓 cat backup_binlog.sh ...
- java JPEGImageEncoder;图像处理
在Eclipse中处理图片,需要引入两个包: import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JP ...
- Android网络连接之HttpURLConnection和HttpClient
1.概念 HTTP 协议可能是现在 Internet 上使用得最多.最重要的协议了,越来越多的 Java 应用程序需要直接通过 HTTP 协议来访问网络资源.在 JDK 的 java.net 包中 ...
- 移动端web出现的一系列问题
今天做移动端的web,在做后期处理的时候,发现了非常多的问题.下面我分别列举一下吧~~ 1.移动端浏览器众多,各种浏览器之间的显示等都有差异,很多需要单独处理,于是我需要判断分别是什么浏览器.js代码 ...