Codeforces 1311F Moving Points
题目链接
根据题意,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的更多相关文章
- HDOJ 4717 The Moving Points
The Moving Points Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- HDU 4717The Moving Points warmup2 1002题(三分)
The Moving Points Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- The Moving Points hdu4717
The Moving Points Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- HDU 4717 The Moving Points (三分)
The Moving Points Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- HDUOJ---The Moving Points
The Moving Points Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- HDU-4717 The Moving Points(凸函数求极值)
The Moving Points Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- F. Moving Points 解析(思維、離散化、BIT、前綴和)
Codeforce 1311 F. Moving Points 解析(思維.離散化.BIT.前綴和) 今天我們來看看CF1311F 題目連結 題目 略,請直接看原題. 前言 最近寫1900的題目更容易 ...
- Codeforces Round #624 (Div. 3) F. Moving Points 题解
第一次写博客 ,请多指教! 翻了翻前面的题解发现都是用树状数组来做,这里更新一个 线段树+离散化的做法: 其实这道题是没有必要用线段树的,树状数组就能够解决.但是个人感觉把线段树用熟了会比树状数组更有 ...
- 详细讲解Codeforces Round #624 (Div. 3) F. Moving Points
题意:给定n个点的初始坐标x和速度v(保证n个点的初始坐标互不相同), d(i,j)是第i个和第j个点之间任意某个时刻的最小距离,求出n个点中任意一对点的d(i,j)的总和. 题解:可以理解,两个点中 ...
随机推荐
- Spring AOP操作action时无法注入,报NullPointer异常
Spring AOP操作action时无法注入,报NullPointer异常当使用Spring AOP对action层进行操作时,会出现注入失败的问题,出现空指针异常.原因是一般struts2+spr ...
- 解决VS2013报错fopen、sprintf等函数安全的问题
VS2013中使用fopen.sprintf等函数是会出现安全问题: error C4996: 'fopen': This function or variable may be unsafe. Co ...
- Intellij-Idea使用小细节
SpringMVC项目部署到tomcat中文乱码,tomcat的配置里面加上 -Dfile.encoding=UTF-8
- <meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=0.5, maximum-scale=2.0, user-scalable=yes"/>
<meta charset="utf-8" name="viewport" content="width=device-width, initi ...
- 【C语言】输入三个正整数a,b,c,求最大值,要求定义一个计算最大值的函数max(a,b),返回a,b的值
#include<stdio.h> int max(int a, int b)/*定义函数*/ { if (a > b) return a; else return b; } int ...
- 使用Idea构建springmvc框架,出现no bean named 'cacheManager' is defined 错误
由于IDEA的自动补全功能非常强大,当你配置 <mvc:annotation-driven/> 后编译器会帮你自动补全上面两个配置文件约束.这个时候如果你没注意的就会爆出一个很莫名奇妙的错 ...
- opencv:霍夫圆检测
#include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace st ...
- XSS 3
打开第三题然后会看到 然后进行一下添加数据 然后会发现数据被添加到 value=""双引号中然后然后我们会想到提前闭合 代码 然后进行编码 然后就可以通过了 此题与xss 2类似 ...
- 吴裕雄 python 机器学习——支持向量机线性分类LinearSVC模型
import numpy as np import matplotlib.pyplot as plt from sklearn import datasets, linear_model,svm fr ...
- touch命令修改时间
实例[rhel7]: [root@localhost test]# stat 1.txt 文件:"1.txt" 大小:0 块:0 IO 块:4096 普通空文件设备:fd00h/6 ...