博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu-2838(Cow Sorting)----》树状数组
阅读量:4286 次
发布时间:2019-05-27

本文共 954 字,大约阅读时间需要 3 分钟。

#include
#include
typedef __int64 LL;const LL M = 100005;LL a[M],c[M];LL n;LL lowbit(LL x){ return x&(-x);}void add(LL pos,LL x){ LL num = pos; while(pos <= n) { a[pos] += num; c[pos] += x; pos += lowbit(pos); }}LL get_sum1(LL pos){ LL res = 0; while(pos > 0) { res += c[pos]; pos -= lowbit(pos); } return res;}LL get_sum2(LL pos){ LL res = 0; while(pos > 0) { res += a[pos]; pos -= lowbit(pos); } return res;}int main(){ LL m,x,ret,k; while(~scanf("%I64d",&n)) { LL ret = 0; memset(a,0,sizeof(a)); memset(c,0,sizeof(c)); for(LL i = 1;i <= n;i++) { scanf("%I64d",&m); add(m,1); x = i - get_sum1(m); if(x) { k = get_sum2(n) - get_sum2(m); ret += x * m + k; } } printf("%I64d\n",ret); }}

转载地址:http://aysgi.baihongyu.com/

你可能感兴趣的文章
git回到指定版本命令
查看>>
C#计算两个时间年份月份差
查看>>
Cordova 扫码插件整理-cordova-plugin-qrscanner
查看>>
cordova-plugin-splashscreen设置启动页面和图标
查看>>
cordova-plugin-battery-status监听电池状态
查看>>
cordova-plugin-globalization本地化
查看>>
cordova-plugin-contacts联系人操作
查看>>
cordova-plugin-camera相机插件使用
查看>>
cordova-plugin-media音频播放和录制
查看>>
Visual Studio 2017使用Emmet风格编写Html--ZenCoding
查看>>
iis提示“另一个程序正在使用此文件,进程无法访问。(异常来自HRESULT:0x80070020) ”解决办法
查看>>
Visual Studio Code v1.21发布
查看>>
C# Newtonsoft.Json JObject移除属性,在序列化时忽略
查看>>
Git移除版本控制操作
查看>>
分块编码(Transfer-Encoding: chunked)(转)
查看>>
Http缓存机制(转)
查看>>
C# 本地时间格式,UTC时间格式,GMT时间格式处理
查看>>
Windows系统搭建GitServer--Bonobo Git Server
查看>>
Bootstrap3 datetimepicker控件之smalot的使用
查看>>
小程序Canvas隐藏问题处理
查看>>