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)的总和. 题解:可以理解,两个点中 ...
随机推荐
- Federal Learning(联邦学习)认知
本人是学生党,同时也是小菜鸡一枚,撞运气有机会能够给老师当项目助理,在这个过程中肯定会学到一些有趣的知识,就在此平台上记录一下,在知识点方面有不对的还请各位指正. What(什么是联邦学习?) 联邦学 ...
- layui之表单验证
这篇文章的表单验证我只是随便记录下,望各位看官理解 1.排序 验证 html代码 <div class="layui-form-item"> <label cla ...
- Thinkcmf对接支付宝支付和获取用户信息
一. 登录支付宝开放平台 平台地址:https://open.alipay.com/ 二. 创建应用并申请上线 登录后,[进入我的开放平台],依次点击[开发者中心]-& ...
- 用python脚本测试接口
自己写一个脚本,统计调用200次接口的请求时长. # -*- coding=utf-8 -*-import osimport requestsimport time url = "http: ...
- 初篇——目录(JavaMail)
结构图 目录 邮件的知识体系由三部分组成,邮箱服务器,邮件程序,邮件协议. 邮箱服务器一般都是由公司的网络工程师搭建完成,基本上与程序员没有关系.但是笔者还是亲自尝试,使用Apache James搭建 ...
- Lose it!
You are given an array aa consisting of nn integers. Each aiai is one of the six following numbers: ...
- HTML 5 视频直播一站式扫盲(转载)
http://www.alloyteam.com/2016/05/h5-camera-literacy/
- LED Decorative Light Manufacturer - Led Wall Lamp Performance Characteristics
LED Decorative Light Manufacturer introduction: LED wall lamp is a light-emitting diode as a ligh ...
- 每天进步一点点------Allegro使用脚本记录文件设置工作环境的颜色
script脚本文件在Allegro PCB DESIGN中能完成很多参数设定,功能很强大.使用script脚本我们能够快速定制自己的Allegro workbench environment. 案例 ...
- js实现页面下拉后展示导航,以及点击导航自动滑动到相关页面
//监控,下拉750px后展示导航 $(window).scroll(function(){ var $this = $(this); var targetTop = $(this).scrollTo ...