树状数组+二分

就是找第几小的数,,找几次,再求和。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
const int N=277777;
int t,n,m,bit[N],num,i;
long long ans;
int low(int g)
{
return g&(-g);
}
void update(int pos)
{
while(pos<=n)
{
bit[pos]--;
pos+=low(pos);
}
}
int query(int x)
{
int sum=0;
while(x>0)
{
sum+=bit[x];
x-=low(x);
}
return sum;
}
int Sum(int k)
{
int l=1,r=n,mid;
while(l<r)
{
mid=(l+r)>>1;
if(query(mid)>=k)
r=mid;
else
l=mid+1;
}
return l;
}
void init()
{
ans=0;
memset(bit,0,sizeof(bit));
scanf("%d %d",&n,&m);
for(i=1;i<=n;i++)
{bit[i]=low(i);}
for(i=0;i<m;i++)
{
scanf("%d",&num);
int s=Sum(num);
update(s);
ans+=s;
}
}
int main()
{
int cas=0;
scanf("%d",&t);
while(t--)
{
init();
cout<<"Case "<<++cas<<": "<<ans<<endl;
}
return 0;
}

二分查找具体解释点击打开链接

hdu 4217Data Structure?的更多相关文章

  1. HDU 5929 Basic Data Structure 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)

    Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  2. HDU 2217 Data Structure?

    C - Data Structure? Time Limit:5000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u ...

  3. HDU 5929 Basic Data Structure 模拟

    Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  4. hdu 4217 Data Structure? 树状数组求第K小

    Data Structure? Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  5. hdu 4217 Data Structure?/treap

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4217 可用线段树写,效率要高点. 这道题以前用c语言写的treap水过了.. 现在接触了c++重写一遍 ...

  6. Basic Data Structure HDU - 5929 (这个模拟我要报警了)

    Mr. Frog learned a basic data structure recently, which is called stack.There are some basic operati ...

  7. hdu 5929 Basic Data Structure

    ゲート 分析: 这题看出来的地方就是这个是左结合的,不适用结合律,交换律. 所以想每次维护答案就不怎么可能了.比赛的时候一开始看成了异或,重读一遍题目了以后就一直去想了怎么维护答案...... 但是很 ...

  8. HDU 5929 Basic Data Structure(模拟 + 乱搞)题解

    题意:给定一种二进制操作nand,为 0 nand 0 = 10 nand 1 = 1 1 nand 0 = 1 1 nand 1 = 0 现在要你模拟一个队列,实现PUSH x 往队头塞入x,POP ...

  9. HDU 6649 Data Structure Problem(凸包+平衡树)

    首先可以证明,点积最值的点对都是都是在凸包上,套用题解的证明:假设里两个点都不在凸包上, 考虑把一个点换成凸包上的点(不动的那个点), 不管你是要点积最大还是最小, 你都可以把那个不动的点跟原点拉一条 ...

随机推荐

  1. 分享一个基于 Node.js 的 Web 开发框架 - Nokitjs

    简介 Nokit 是一个简单易用的基于 Nodejs 的 Web 开发框架,默认提供了 MVC / NSP / RESTful 等支持,并提供对应项目模板.管理工具. 资源 GitHub https: ...

  2. Google Scholar 论文参考文献的自动生成

    写论文经常需要写出参考文献,各种格式实在是麻烦的不行啊,在网上看到一个参考文献自动生成的博客,现在转载下来,以备以后自已能用. 主要是使用Google Scholar. Step 1: 打开Googl ...

  3. 一个十分简洁实用的MD风格的UI主框架

    2017-5-23 详见:https://github.com/baiqiantao/CheesesquareSample MainActivity public class MainActivity ...

  4. 【Python】Django auth 修改密码如何实现?

    使用示例1.创建用户>>> from django.contrib.auth.models import User>>> user = User.objects.c ...

  5. 神奇的container_of

    container_of是linux内核中常用的一个宏,这个宏的功能是,根据某个结构体字段的指针,找到对应的结构体指针. 话不多说,先上源码: /** * container_of - cast a ...

  6. Tensorflow 深度学习简介(自用)

    一些废话,也可能不是废话.可能对,也可能不对. 机器学习的定义:如果一个程序可以在任务T上,随着经验E的增加,效果P也可以随之增加,则称这个程序可以在经验中学习. “程序”指的是需要用到的机器学习算法 ...

  7. FormBorderStyle为None的时候如何拖动窗体

    //为DllImport导出命名空间, using System.Runtime.InteropServices; public partial class Form1 : System.Window ...

  8. KMP Client README

    KMP Client README About KMP is a mod for v0.22 of Kerbal Space Program that adds a multiplayer game ...

  9. component is not authorized by this account hint: [B3GVCa0189e575] 错误解决?

    component is not authorized by this account hint: [aMADoA0312e514] component is not authorized by th ...

  10. es5 - array - sort

    /** * 描述:该sort()方法对数组中的元素进行排序并返回该数组,默认排序顺序是根据字符串Unicode代码点. * 语法:arr .sort([compareFunction]) * 参数: ...