题目链接

根据题意,d是两个点的最短距离,分析知,假设\(x_i\)<\(x_j\), 若\(v_i\)>\(v_j\),那么d(i,j)一定为0,因为i一定能追上j,否则,d(i,j)就为其初始距离

那我们就转化问题为一个二维偏序问题,求\(x_i\)<\(x_j\)且\(v_i\)<\(v_j\),满足这个条件的每个点对之间的距离

很容易想到定一序,另一序用树状数组维护的统计法,假设现在是\(x_i\),满足上述条件有k个,那么,对\(x_i\)的统计答案为:\(\sum_{j=i-k}^{i-1}(x_i-x_j)\),拆开,就是\(k*x_i-\sum_{j=i-k}^{i-1}x_j\)

那我们可以维护2个树状数组,分别维护上述的2个数,满足条件的个数与满足条件的这些数的x的和即可

#include<bits/stdc++.h>
using namespace std;
#define lowbit(x) ((x)&(-x))
typedef long long LL;
typedef pair<int,int> pii; const int maxn = 2e5+5; struct Node {
int x, v;
} Nodes[maxn]; int all_x[maxn];
LL C1[maxn], C2[maxn]; void add(LL val, int pos, int n) {
for(; pos <= n; pos += lowbit(pos))
C1[pos] += val, C2[pos]++;
} pair<LL,LL> getsum(int pos) {
LL ret1 = 0, ret2 = 0;
for(;pos;pos-=lowbit(pos))
ret1 += C1[pos], ret2 += C2[pos];
return make_pair(ret1,ret2);
} void run_case() {
int n;
cin >> n;
for(int i = 1; i <= n; ++i) {
cin >> Nodes[i].x;
all_x[i] = Nodes[i].x;
}
for(int i = 1; i <= n; ++i) {
cin >> Nodes[i].v;
}
sort(all_x+1, all_x+1+n);
int len = unique(all_x+1, all_x+1+n) - all_x - 1;
for(int i = 1; i <= n; ++i) {
Nodes[i].x = lower_bound(all_x+1, all_x+1+len, Nodes[i].x) - all_x;
}
sort(Nodes+1, Nodes+1+n, [&](Node &a, Node&b) {
return a.v < b.v || (a.v == b.v && a.x < b.x);
});
LL ans = 0;
for(int i = 1; i <= n; ++i) {
add(all_x[Nodes[i].x], Nodes[i].x, n);
auto now = getsum(Nodes[i].x-1);
ans += 1LL*now.second*all_x[Nodes[i].x] - now.first;
}
cout << ans;
} int main() {
ios::sync_with_stdio(false), cin.tie(0);
cout.flags(ios::fixed);cout.precision(10);
//int t; cin >> t;
//while(t--)
run_case();
cout.flush();
return 0;
}

Codeforces 1311F Moving Points的更多相关文章

  1. HDOJ 4717 The Moving Points

    The Moving Points Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  2. HDU 4717The Moving Points warmup2 1002题(三分)

    The Moving Points Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  3. The Moving Points hdu4717

    The Moving Points Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  4. HDU 4717 The Moving Points (三分)

    The Moving Points Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  5. HDUOJ---The Moving Points

    The Moving Points Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  6. HDU-4717 The Moving Points(凸函数求极值)

    The Moving Points Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  7. F. Moving Points 解析(思維、離散化、BIT、前綴和)

    Codeforce 1311 F. Moving Points 解析(思維.離散化.BIT.前綴和) 今天我們來看看CF1311F 題目連結 題目 略,請直接看原題. 前言 最近寫1900的題目更容易 ...

  8. Codeforces Round #624 (Div. 3) F. Moving Points 题解

    第一次写博客 ,请多指教! 翻了翻前面的题解发现都是用树状数组来做,这里更新一个 线段树+离散化的做法: 其实这道题是没有必要用线段树的,树状数组就能够解决.但是个人感觉把线段树用熟了会比树状数组更有 ...

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

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

随机推荐

  1. go 语言实现栈原理

    package main import "fmt" type StackNode struct { Data interface{} //数据 Next *StackNode // ...

  2. IDEA与Tomcat相关配置

    idea会为每一个Tomcat部署的项目,独立建一份配置文件. 配置文件所在位置 怎么部署的(查看虚拟目录)使用的第三种部署方式 部署项目存放的路径 项目目录和Tomcat部署目录 Tomcat真正访 ...

  3. 通过CSS禁用页面内容选中和复制操作

    -moz-user-select: none;-webkit-user-select: none;-ms-user-select: none;-khtml-user-select: none;user ...

  4. 交叉连接(CROSS JOIN)

    除了在FROM子句中使用逗号间隔连接的表外,SQL还支持另一种被称为交叉连接的操作,它们都返回被连接的两个表所有数据行的笛卡尔积,返回到的数据行数等于第一个表中符合查询条件的数据行数乘以第二个表中符合 ...

  5. Virtual Judge POJ 1328 Radar Installation

    贪心 #include<algorithm> #include<iostream> #include<cstdio> #include<cmath> u ...

  6. idea tomcat启动无效

    今天换了台电脑,用idea 部署tomcat启动死活启动不了,报 Application Server was not connected before run configuration stop, ...

  7. 牛客竞赛第二场D Kth Minimum Clique 贪心+bitmap

    Kth Minimum Clique 题意 给出n(n<100)个点的邻接表,和n个点的权值,求第k大的团(完全子图) 分析 n很小,并且好像没有什么算法和这个有关系,所以可以往暴力枚举的方向想 ...

  8. 2019年牛客多校第一场B题Integration 数学

    2019年牛客多校第一场B题 Integration 题意 给出一个公式,求值 思路 明显的化简公式题,公式是分母连乘形式,这个时候要想到拆分,那如何拆分母呢,自然是裂项,此时有很多项裂项,我们不妨从 ...

  9. HTML学习(17)URL

    HTML 统一资源定位器(Uniform Resource Locators) URL - 统一资源定位器 Web浏览器通过URL从Web服务器请求页面. scheme://host.domain:p ...

  10. [Note]后缀自动机

    后缀自动机 代码 #include <cstdio> #include <algorithm> #include <cstring> const int M = 1 ...