题目链接

根据题意,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. Python之路【第三十二篇】:django 分页器

    Django的分页器paginator 文件为pageDemo models.py from django.db import models # Create your models here. cl ...

  2. 屏蔽tips

    在屏蔽的地方打上记号,这样解屏蔽时就容易找到屏蔽的地方了

  3. Oracle中行转列,列转行pivot的用法

    测试数据准备 --建表 --drop table SalesList; create table SalesList( keHu ), --客户 shangPin ), --商品名称 salesNum ...

  4. vscode中vim插件对ctrl键的设置

    vim配置 在使用中经常想使用ctrl-c,虽然在vscode中有配置选项可以让vim与ctrl键解绑,但是这样就使用不了vim的VISUAL BLOCK.所以进行了自定义设置. 设置 - Vim C ...

  5. 1.6 APP需要怎么测试

    来源:  https://tieba.baidu.com/p/5011439767           http://www.cnblogs.com/testwriter/p/6702624.html ...

  6. Codeforces补题2020.2.28(Round624 Div 3)

    A.Add Odd or Subtract Even 签到题~ #include<bits/stdc++.h> using namespace std; int T; int a,b; i ...

  7. Java数据处理,Map中数据转double并取小数点后两位

    BigDecimal order = (BigDecimal) map.get("finishrat"); double d = (order == null ? 0 : orde ...

  8. D. Lunar New Year and a Wander bfs+优先队列

    D. Lunar New Year and a Wander bfs+优先队列 题意 给出一个图,从1点开始走,每个点至少要经过一次(可以很多次),每次经过一个没有走过的点就把他加到走过点序列中,问最 ...

  9. angular6 路由拼接查询参数如 ?id=1 并获取url参数

    angular6 路由拼接查询参数如 ?id=1 并获取url参数 路由拼接参数: <div class="category-border" [routerLink]=&qu ...

  10. EAC3 mantissa quantization(VQ & GAQ)

    EAC3基于hebap来决定mantissa的quantizer. hebap如下: mantissa 使用VQ(vector quantization) 和GAQ(gain adaptive qua ...