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 ...
随机推荐
- HTML data属性简介以及低版本浏览器兼容算法
实例 使用 data-* 属性来嵌入自定义数据: <ul> <li data-animal-type="bird">Owl</li> <l ...
- 160928、JQuery解析XML数据的demo
用JavaScript解析XML数据是常见的编程任务,JavaScript能做的,JQuery当然也能做.下面我们来总结几个使用JQuery解析XML的例子. 方案1 当后台返回的数据类型是xml对象 ...
- JavaScript调试技巧之console.log()详解
JavaScript调试技巧之console.log()详解 对于JavaScript程序的调试,相比于alert(),使用console.log()是一种更好的方式,原因在于:alert()函数会阻 ...
- [转]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 ...
- AJAX 数据库实例
AJAX 用于创建动态性更强的应用程序. AJAX ASP 实例 下面的例子将演示当用户在输入框中键入字符时,网页如何与服务器进行通信: 实例 请在下面的输入框中键入字母(A - Z): 姓名: 建议 ...
- oracle监听服务无法打开
原因: 修改了主机名,但没有修改监听文件listener.ora里面的配置. 解决办法:修改HOST与主机名相同 LISTENER = (DESCRIPTION_LIST = (DESCRIPTION ...
- php两种include加载文件方式效率比较如下
1)定义一个字符串变量,里面保存要加载的文件列表.然后foreach加载. $a = '/a.class.php;/Util/b.class.php;/Util/c.class.php'; $b = ...
- Linux命令学习手册-printf命令(转)
分类: LINUX 参考资料:http://sns.linuxpk.com/space-566-do-blog-id-15819.html printf FORMAT [ARGUMENT]... pr ...
- ACM题目————小A的计算器
Description 以往的操作系统内部的数据表示都是二进制方式,小A新写了一个操作系统,系统内部的数据表示为26进制,其中0-25分别由a-z表示. 现在小A要在这个操作系统上实现一个计算器,这 ...
- json返回日期格式化的解决
function jsonDateFormat(jsonDate) {//json日期格式转换为正常格式 try { var date = new Date(parseInt(jsonDate.rep ...