题:https://codeforces.com/contest/1311/problem/F

题意:给定x轴上的点以及他们的速度v,只在x轴上运动,求最小的dis之和,注意,这里的时间是可随意的,比如对于其中一个点 i 来说,只要其他点运动到离自己距离最小即可,而不是同步运动

分析:对于一对点 i 和 j 来说,以点 i 为基准,要是xj<xi,那么只要vj>xi就可以让dis为0,要是xj>xi,,那么只要vj<vi就可以让dis为0,其他情况就俩者不动就能保持dis最小为abs(xi-xj);

   总结一下,只要xi<=xj&&vi<=vj,那么对答案的贡献就是abs(xi-xj),(因为越移动dis越大)其他的贡献就为0;

   所以我们只要讲x升序,就可避免重复算点对,对于一个点 i 而言,对答案的贡献就是:比xi小的点的数目*xi - 比xi小的点的x之和;

   因为统计全程与v的具体值无关只有大小,所可以进行离散化处理;

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int M=5e5+;
const int N=2e5+;
ll tr[M][];
struct node{
ll x,v;
bool operator <(const node &b)const{
return x<b.x;
}
}a[M];
ll lisan[M];
void update(int x,ll c){
while(x<=N){
tr[x][]++;
tr[x][]+=c;
x+=x&(-x);
}
}
ll query(int x,int k){
ll ans=;
while(x){
ans+=tr[x][k];
x-=(x&(-x));
}
return ans;
}
int main(){
ios::sync_with_stdio(false);
cin.tie();
int n;
cin>>n;
for(int i=;i<=n;i++)
cin>>a[i].x;
for(int i=;i<=n;i++)
cin>>a[i].v,lisan[i]=a[i].v;
sort(a+,a++n);
sort(lisan+,lisan++n);
int m=unique(lisan+,lisan++n)-lisan-;
ll ans=;
for(int i=;i<=n;i++){
int pos=lower_bound(lisan+,lisan++m,a[i].v)-lisan;
ll num=query(pos,);
ll sum=query(pos,);
ans+=a[i].x*num-sum;
update(pos,a[i].x);
}
cout<<ans<<endl;
return ;
}

F - Moving Points树状数组的更多相关文章

  1. [CF1311F] Moving Points - 树状数组

    Solution 按 \(x\) 关键字升序排序,依次枚举每个点 考虑对任意 \(x_j < x_i\),那么当 \(v_j \leq v_i\) 时,它们不会相交,且 \(dis\) 就是它们 ...

  2. 2016-2017 ACM-ICPC Southwestern European Regional Programming Contest (SWERC 2016) F dfs序+树状数组

    Performance ReviewEmployee performance reviews are a necessary evil in any company. In a performance ...

  3. 牛客网暑期ACM多校训练营(第五场) F - take —— 期望+树状数组+逆元

    看到一篇好的博客特意转出来观摩大佬:转:https://blog.csdn.net/greybtfly/article/details/81413526 题目大意:给n个箱子排成一排,从头到尾按顺序依 ...

  4. 2019牛客多校第七场 F Energy stones 树状数组+算贡献转化模拟

    Energy stones 题意 有n块石头,每块有初始能量E[i],每秒石头会增长能量L[i],石头的能量上限是C[i],现有m次时刻,每次会把[s[i],t[i]]的石头的能量吸干,问最后得到了多 ...

  5. CF1096.F. Inversion Expectation(树状数组)

    A permutation of size n is an array of size n such that each integer from 1 to n occurs exactly once ...

  6. 51Nod 1272最大距离 (树状数组维护前缀最小值)

    题目链接 最大距离 其实主流解法应该是单调栈……我用了树状数组. #include <bits/stdc++.h> using namespace std; #define rep(i, ...

  7. 北邮校赛 F. Gabriel's Pocket Money(树状数组)

    F. Gabriel's Pocket Money 2017- BUPT Collegiate Programming Contest - sync 时间限制 2000 ms 内存限制 65536 K ...

  8. Codeforces Round #300 F - A Heap of Heaps (树状数组 OR 差分)

    F. A Heap of Heaps time limit per test 3 seconds memory limit per test 512 megabytes input standard ...

  9. 牛客小白月赛12 F 华华开始学信息学 (分块+树状数组)

    链接:https://ac.nowcoder.com/acm/contest/392/F来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 32768K,其他语言65536K ...

随机推荐

  1. php5.3不支持 ereg、ereg_replace等函数问题

    在php5.3环境下运行oscommerce,常常会出现Deprecated: Function ereg() is deprecated in...和Deprecated: Function ere ...

  2. Linux系统发现新恶意软件

    导读 安全研究人员发现了一种新的Linux恶意软件,它似乎是由中国黑客创建的,并被用作远程控制受感染系统的手段. 这个恶意软件命名为HiddenWasp,由用户模式rootkit,木马和初始部署脚本组 ...

  3. Visual Studio Code 断点调试配置方法(请按我的步骤 一定可以做到)

    1 visual studio code 的 extentions 里安装插件 debugger for chrome2 devtool: 'eval-source-map', cacheBustin ...

  4. 118-PHP调用带参数的成员方法

    <?php class ren{ //定义人类 public function info($name,$age=3){ //定义有两个参数的成员方法 echo "我是{$name},年 ...

  5. 数论 CF27E Number With The Given Amount Of Divisors

    求因子数一定的最小数(反素数) #include<iostream> #include<string> #include<cmath> #include<cs ...

  6. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-italic

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...

  7. Sublime 一些常用快捷键

    Sublime插件安装和使用-----------------------------最常用的1.新建文件-输入"html:xt"后 按"Ctrl+E键"或 & ...

  8. Linux Mysql 安装 开启远程连接 供python agent 连接测试 Mark

    Linux     6.3 (1) cat  /etc/redhat-release uname -a 查看yum 源:   阿里源 无源运行: echo 下载阿里云的yum源配置 wget -O / ...

  9. Linux每日一练20200221

  10. UVA - 11892 ENimEN(博弈)

    题意:有n堆石子,两个人拿,拿走最后的石子的人赢,poopi先拿,条件是,每个人必须从另外一个人最后拿过的石子堆中取石子,若那堆石子被拿没了,才可以自由地拿其他堆.要求每次拿的石子数不能为0.问谁赢. ...