第一次写博客 ,请多指教!

翻了翻前面的题解发现都是用树状数组来做,这里更新一个 线段树+离散化的做法:

其实这道题是没有必要用线段树的,树状数组就能够解决。但是个人感觉把线段树用熟了会比树状数组更有优势一点

不多废话 

http://codeforces.com/contest/1311/problem/F 题目链接

题意是 给你一堆点的位置和他们运动的速度(可正可负),然后时间无限往后推的情况下问你他们之间所有点的最小距离之和。(不明白自行读题)

n的范围是2,200000; 显然单纯的一个一个看是不行的,那么自然想到用树状数组或者线段树来做。

接着可以想到

1.当两个点的位置xi ,xj满足 xi<xj 并且两个点的速度满足vi <=vj时 他们之间的最短距离一定是当前距离即xj与xi之差。

2.其余情况最短距离均为零(这一点很容易想)

那么就好办了 

思路就是

1.先对每个点对按照位置从小到大进行排序

2.按照位置从前到后遍历这些点,进行建树。由当前点的速度确定在数组中的位置。

3.建树时每更新一个点之前可以求出这个点对答案的贡献 即:

ans+=(数组中位于当前点前面的点的数量(速度小于当前点速度的点的数量)×当前点的位置)- 前面所有点的位置值的和。

代码中的公式为 ans+=s[i].x*querye(1,x,1,n,1)-query(1,x,1,n,1);

由于是按照位置从前到后的顺序上树的,所以不必担心出现比当前点位置靠后的点;

而关于 统计速度小于当前点速度的点的数量 维护了一个he数组 查询用querye函数,而前面所有点的位置值的和则是最一般的线段树查询。

至于速度给的数据范围有点大,可以使用离散化将他们集中起来,以便作为下标进行更新。

上代码 (代码虽然看起来不是很简短但是基本都是模块化操作,耐心看下来就会感觉很容易理解)

https://www.cnblogs.com/LH2000/category/1656597.html  这里有一些本人写的相关模板函数,可以用作参考。

 #include<bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define rush! ios::sync_with_stdio(false);cin.tie(0);
const int inf = 0x3f3f3f3f;
const long long linf = 0x3f3f3f3f3f3f3f3f;
const int maxn=;
long long tree[maxn<<]; //线段树
long long lsh[]; //离散化 数组
long long he[maxn<<];
struct trees{
long long x, v;
}s[];
bool cmp(const trees &x,const trees &y)
{
return x.x<y.x;
}
void pushup(int rt)
{
tree[rt]=tree[rt<<]+tree[rt<<|]; //用子节点更新父节点的位置值
he[rt]=he[rt<<]+he[rt<<|]; //用子节点更新父节点数量值
} void update(long long p,long long add,int l,int r,int rt) //上树
{
if(l==r)
{
tree[rt]+=add;//更新位置值
he[rt]++;//更新数量值
return;
}
int m=(l+r>>);
if(p<=m) update(p,add,lson);
else update(p,add,rson);
pushup(rt);
} long long query(int L,int R,int l,int r,int rt) //查询速度小于当前点速度的元素位置值的和
{
if(L<=l&&r<=R)
{
return tree[rt];
}
int m=(l+r)>>;
long long ret=;
if(L<=m) ret+=query(L,R,lson);
if(R>m) ret+=query(L,R,rson);
return ret;
}
long long querye(int L,int R,int l,int r,int rt) //查询速度小于当前点速度的元素数量
{
if(L<=l&&r<=R)
{
return he[rt];
}
int m=(l+r)>>;
long long ret=;
if(L<=m) ret+=querye(L,R,lson);
if(R>m) ret+=querye(L,R,rson);
return ret;
} int main()
{
rush! //加速流
int n;
cin>>n;
for(int i=;i<=n;i++)
{
cin>>s[i].x;
}
for(int i=;i<=n;i++){
cin>>s[i].v;
lsh[i]=s[i].v;
}
sort(lsh+,lsh+n+); //离散化排序
sort(s+,s+n+,cmp);
int cnt=unique(lsh+,lsh+n+)-lsh-; //离散化数数
long long ans=;
for(int i=;i<=n;i++)
{
long long x=lower_bound(lsh+,lsh+cnt+,s[i].v)-lsh; //离散化
ans+=s[i].x*querye(,x,,n,)-query(,x,,n,);
update(x,s[i].x,,n,); //上树
}
cout<<ans<<endl;
}

Codeforces Round #624 (Div. 3) F. Moving Points 题解的更多相关文章

  1. 详细讲解Codeforces Round #624 (Div. 3) F. Moving Points

    题意:给定n个点的初始坐标x和速度v(保证n个点的初始坐标互不相同), d(i,j)是第i个和第j个点之间任意某个时刻的最小距离,求出n个点中任意一对点的d(i,j)的总和. 题解:可以理解,两个点中 ...

  2. Codeforces Round #624 (Div. 3) F

    题意: 给出n的质点,带着初位置和速度: 如果中途两点可以相遇dis(i,j)=0: 如果不可以相遇,mindis(i,j): 求n个点的两两质点最小dis(i,j)之和 思路: 因为当初位置x和速度 ...

  3. Codeforces Round #501 (Div. 3) F. Bracket Substring

    题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60 ...

  4. Codeforces Round #486 (Div. 3) F. Rain and Umbrellas

    Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...

  5. Codeforces Round #624 (Div. 3)(题解)

    Codeforces Round #624 (Div.3) 题目地址:https://codeforces.ml/contest/1311 B题:WeirdSort 题意:给出含有n个元素的数组a,和 ...

  6. Codeforces Round #485 (Div. 2) F. AND Graph

    Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...

  7. Codeforces Round #499 (Div. 1) F. Tree

    Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...

  8. Codeforces Round #609 (Div. 2)前五题题解

    Codeforces Round #609 (Div. 2)前五题题解 补题补题…… C题写挂了好几个次,最后一题看了好久题解才懂……我太迟钝了…… 然后因为longlong调了半个小时…… A.Eq ...

  9. Codeforces Round #624 (Div. 3)(题解)

    A. Add Odd or Subtract Even 思路: 相同直接为0,如果两数相差为偶数就为2,奇数就为1 #include<iostream> #include<algor ...

随机推荐

  1. zookeeper从入门到放弃

    第1章 Zookeeper入门 1.1 概述 Zookeeper是一个开源的分布式的,为分布式应用提供协调服务的Apache项目. 1.2 特点 1.3 数据结构 1.4 应用场景 提供的服务包括:统 ...

  2. gitlab(五):一个开发流程实例

    一个多人开发的样例 开发的流程我们都知道: 根据项目版本,创建里程碑,创建开发的issue,分配给dev dev从master clone代码,创建分支就行开发,开发完成之后,提交分支 dev给开发负 ...

  3. k8s系列---ingress资源和ingress-controller

    https://www.cnblogs.com/zhangeamon/p/7007076.html http://blog.itpub.net/28916011/viewspace-2214747/ ...

  4. mongoDB常用命令与安全加固

    一.介绍 MongoDB 是一个基于分布式文件存储的数据库.由C++语言编写.旨在为WEB应用提供可扩展的高性能数据存储解决方案.MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系 ...

  5. Linux安装virtualenvwrapper

    1.pip install virtualenvwrapper 2.export WORKON_HOME=/home/virtualenv  //配置虚拟环境变量,以后直接mkvirtualenv 虚 ...

  6. 珠峰-node

    ##### 文件流的读写. ##### 文件流对pipe的封装. ####

  7. centos7安装node.js

    安装版本:node-v10.15.3 一.安装必要的编译软件包 # yum install gcc gcc-c++ -y 二.从源码下载Nodejs 进入官网选择自己需要的版本 https://nod ...

  8. 将 ASP.NET Core 2.1 升级到最新的长期支持版本ASP.NET Core 3.1

    目录 前言 Microsoft.AspNetCore.Mvc.ViewFeatures.Internal 消失了 升级到 ASP.NET Core 3.1 项目文件(.csproj) Program. ...

  9. SQLServer之查询当前服务器下所有目录视图表

    SQL脚本 /*************1:删除临时表*************/ if exists(select * from tempdb..sysobjects where id=object ...

  10. Escape(反思与总结)

    题目描述: BH is in a maze,the maze is a matrix,he wants to escape! Input: The input consists of multiple ...