比较友好的数据结构题

建议读者好好思考思考…….

可以分析出与前缀和的关系, 之后就愉快的套起树状数组辣

#include <cstdio>
#include<queue>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int N=200000+3;
long long C[6][N];
int x[N],v[N],A[N], n;
int cmp(int i,int j)
{
return x[i]<x[j];
}
int lowbit(int t)
{
return t&(-t);
}
void update(int x,int delta,int ty)
{
while(x<N){
C[ty][x]+=delta;
x+=lowbit(x);
}
}
long long query(int x,int ty)
{
long long tmp=0;
while(x>0){
tmp+=C[ty][x];
x-=lowbit(x);
}
return tmp;
}
int main()
{
// freopen("in.txt","r",stdin);
scanf("%d",&n);
for(int i=1;i<=n;++i)scanf("%lld%lld",&v[i],&x[i]);
for(int i=1;i<=n;++i)A[i]=i;
sort(A+1,A+1+n,cmp);
long long ans=0;
int cur;
for(int i=1;i<=n;++i) //正向枚举
{
cur=A[i];
long long a=query(v[cur], 0); //数量
long long b=query(v[cur],1); //距离
// printf("%lld %lld\n",a,b);
ans+=(long long)((a*x[cur]-b)*v[cur]);
update(v[cur]+1,1,0); //更新比x[i]大的数量
update(v[cur]+1,x[cur],1); //更新比x[i]大的距离和
}
for(int i=n;i>=1;--i)
{
cur=A[i];
long long a=query(v[cur],3);
long long b=query(v[cur],4);
ans+=(long long)(b-(a*x[cur]))*v[cur];
update(v[cur],1,3);
update(v[cur],x[cur],4);
}
printf("%lld",ans);
return 0;
}

最新文章

  1. C#设计模式——单件模式
  2. 关于wxwidgets图形界面的关闭窗口的按钮无效的解决办法
  3. 微信支付 APP 支付方式的服务器端处理程序
  4. Spark学习(一)--RDD操作
  5. Dapper 基础用法
  6. hihocoder 1388 fft循环矩阵
  7. python函数部分----函数初识
  8. java线程实现的四种方式
  9. HGOI20180813 (NOIP2018 提高组 Day2 模拟试题)
  10. 题目1454:Piggy-Bank(完全背包问题)
  11. CentOS6.5安装RHadoop
  12. PHP 图片打水印
  13. BZOJ3192:[JLOI2013]删除物品——题解
  14. 如何让access空值变成0?(确切的说是让access Null值变成0)
  15. git add用法
  16. Codeforces 395 D.Pair of Numbers
  17. Leetcode 002. 两数相加
  18. 利用aop完成功能权限验证遇到的问题
  19. Spring Cloud Hystrix 1(熔断器简介)
  20. angularJs(2)表单中下拉框单选多选

热门文章

  1. android下xml放哪儿?
  2. java中类的路径为什么这么长
  3. iOS中基于 Socket 的 C/S 结构网络通信(中)
  4. requireJS文件夹
  5. 11153 kill boss
  6. QString够绕的,分为存储(编译器)和解码(运行期),还有VS编译器的自作主张,还有QT5的变化
  7. 【HDU 1847】 Good Luck in CET-4 Everybody!
  8. 【POJ 2096】 Collecting Bugs
  9. Ubuntu搭建docker环境
  10. html页面中苹果手机遇到数字换行、样式变形