@description@

给定 n 个点,第 i 个点位于 (xi, yi)。

在第 i 个点与第 j 个点之间建边费用为 xi*xj + yi*yj。

求最小生成树。

Input

第一行一个整数 T (1≤T≤2000),表示数据组数。

每组数据给定一个整数 n(2≤n≤100000),表示点数。保证 ∑n≤10^6。

接下来 n 行,每行两个整数 xi, yi(1≤xi,yi≤10^6), 描述一个点的坐标。注意可以点可以重合。

Output

对于每组数据,输出最小生成树的权值和。

Sample Input

1

3

2 4

3 1

5 2

Sample Output

27

@solution@

完全图的最小生成树,套路般的 boruvka 算法(可以自己百度一下)。

boruvka 算法的其他部分都是套路,主要是考虑怎么求一个连通块的最小邻边。

注意到点积 \(x_i*x_j + y_i*y_j\) 其实可以写成斜率优化形式的式子 \(y_i*(\frac{x_i}{y_i}*x_j + y_j)\)。当 i 固定时相当于求 \(\frac{x_i}{y_i}*x_j + y_j\) 的最小值。

然后就可以转成维护凸包了。

但是我们还需要排除同一连通块中的点,而众所周知凸包很难实现删除。

可以考虑 cdq 分治(不清楚是不是,不过很像)。将同一连通块的点视作同一颜色,对颜色进行分治。

具体来说,对于区间 [l, r] 中的颜色,我们只考虑 [l, mid] 对 [mid + 1, r] 的贡献与 [mid + 1, r] 对 [l, mid] 的贡献。

分治时分开维护询问与点,通过先递归再归并排序实现横坐标与询问的有序。然后就可以 O(nlogn) 搞定一次查询最小邻边了。

套上生成树的复杂度为 O(nlog^2n)。但由于 boruvka 一般跑不满所以常数小,可以通过。

@accepted code@

#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
#define mp make_pair
#define fi first
#define se second
typedef long long ll;
typedef pair<ll, int> pli;
const int MAXN = 100000;
const ll INF = (1LL<<60);
struct point{
int x, y, c;
point(int _x=0, int _y=0, int _c=0):x(_x), y(_y), c(_c) {}
}pnt[MAXN + 5], qry[MAXN + 5], tmp[MAXN + 5];
double f(point p) {
return - 1.0 * p.x / p.y;
}
bool cmp1(point a, point b) {
return a.c == b.c ? (a.x == b.x ? a.y < b.y : a.x < b.x) : a.c < b.c;
}
bool cmp2(point a, point b) {
return a.c == b.c ? f(a) < f(b) : a.c < b.c;
}
double slope(point a, point b) {
if( a.x == b.x ) {
if( b.y >= a.y ) return INF;
else return -INF;
}
else return 1.0 * (b.y - a.y) / (b.x - a.x);
}
int que[MAXN + 5]; pli lnk[MAXN + 5];
void solve(int l, int r, int L, int R) {
if( L == R ) return ;
int M = (L + R) >> 1, m;
for(int i=l;i<=r;i++)
if( pnt[i].c <= M )
m = i;
solve(l, m, L, M), solve(m + 1, r, M + 1, R); int s = 1, t = 0;
for(int i=l;i<=m;i++) {
while( s < t && slope(pnt[que[t-1]], pnt[que[t]]) >= slope(pnt[que[t]], pnt[i]) )
t--;
que[++t] = i;
}
for(int i=m+1;i<=r;i++) {
while( s < t && slope(pnt[que[s]], pnt[que[s+1]]) <= f(qry[i]) )
s++;
int j = que[s];
lnk[qry[i].c] = min(lnk[qry[i].c], mp(1LL*pnt[j].x*qry[i].x + 1LL*pnt[j].y*qry[i].y, pnt[j].c));
}
s = 1, t = 0;
for(int i=m+1;i<=r;i++) {
while( s < t && slope(pnt[que[t-1]], pnt[que[t]]) >= slope(pnt[que[t]], pnt[i]) )
t--;
que[++t] = i;
}
for(int i=l;i<=m;i++) {
while( s < t && slope(pnt[que[s]], pnt[que[s+1]]) <= f(qry[i]) )
s++;
int j = que[s];
lnk[qry[i].c] = min(lnk[qry[i].c], mp(1LL*pnt[j].x*qry[i].x + 1LL*pnt[j].y*qry[i].y, pnt[j].c));
}
int p = l, q = m + 1, k = l;
while( p <= m && q <= r ) {
if( pnt[p].x < pnt[q].x )
tmp[k++] = pnt[p++];
else tmp[k++] = pnt[q++];
}
while( p <= m ) tmp[k++] = pnt[p++];
while( q <= r ) tmp[k++] = pnt[q++];
for(int i=l;i<=r;i++) pnt[i] = tmp[i];
p = l, q = m + 1, k = l;
while( p <= m && q <= r ) {
if( f(qry[p]) < f(qry[q]) )
tmp[k++] = qry[p++];
else tmp[k++] = qry[q++];
}
while( p <= m ) tmp[k++] = qry[p++];
while( q <= r ) tmp[k++] = qry[q++];
for(int i=l;i<=r;i++) qry[i] = tmp[i];
}
int id[MAXN + 5], fa[MAXN + 5], clr[MAXN + 5];
int find(int x) {
return fa[x] = (fa[x] == x ? x : find(fa[x]));
}
bool unite(int x, int y) {
int fx = find(x), fy = find(y);
if( fx != fy ) {
fa[fx] = fy;
return true;
}
else return false;
}
int x[MAXN + 5], y[MAXN + 5], n;
void solve() {
scanf("%d", &n);
for(int i=1;i<=n;i++)
scanf("%d%d", &x[i], &y[i]), fa[i] = i;
ll ans = 0;
while( true ) {
int cnt = 0;
for(int i=1;i<=n;i++)
if( fa[i] == i )
id[clr[i] = (++cnt)] = i;
if( cnt == 1 ) break;
for(int i=1;i<=n;i++)
clr[i] = clr[find(i)];
for(int i=1;i<=n;i++)
pnt[i] = qry[i] = point(x[i], y[i], clr[i]);
sort(pnt + 1, pnt + n + 1, cmp1);
sort(qry + 1, qry + n + 1, cmp2);
for(int i=1;i<=cnt;i++)
lnk[i] = mp(INF, -1);
solve(1, n, 1, cnt);
for(int i=1;i<=cnt;i++)
if( unite(id[i], id[lnk[i].se]) )
ans += lnk[i].fi;
}
printf("%lld\n", ans);
}
int main() {
int T; scanf("%d", &T);
while( T-- ) solve();
}

@details@

其实。。。我一直卡在把询问和横坐标分开排序这一点上。。。

一直在想怎么才能避免在凸包上二分。。。

@hdu - 6329@ Problem K. Transport Construction的更多相关文章

  1. HDU 6342.Problem K. Expression in Memories-模拟-巴科斯范式填充 (2018 Multi-University Training Contest 4 1011)

    6342.Problem K. Expression in Memories 这个题就是把?变成其他的使得多项式成立并且没有前导零 官方题解: 没意思,好想咸鱼,直接贴一篇别人的博客,写的很好,比我的 ...

  2. 2018 Multi-University Training Contest 4 Problem K. Expression in Memories 【模拟】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6342 Problem K. Expression in Memories Time Limit: 200 ...

  3. Codeforces Gym 100610 Problem K. Kitchen Robot 状压DP

    Problem K. Kitchen Robot Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10061 ...

  4. Codeforces 1089K - King Kog's Reception - [线段树][2018-2019 ICPC, NEERC, Northern Eurasia Finals Problem K]

    题目链接:https://codeforces.com/contest/1089/problem/K time limit per test: 2 seconds memory limit per t ...

  5. Gym 101981K - Kangaroo Puzzle - [玄学][2018-2019 ACM-ICPC Asia Nanjing Regional Contest Problem K]

    题目链接:http://codeforces.com/gym/101981/problem/K Your friend has made a computer video game called “K ...

  6. Western Subregional of NEERC, Minsk, Wednesday, November 4, 2015 Problem K. UTF-8 Decoder 模拟题

    Problem K. UTF-8 Decoder 题目连接: http://opentrains.snarknews.info/~ejudge/team.cgi?SID=c75360ed7f2c702 ...

  7. 2010-2011 ACM-ICPC, NEERC, Moscow Subregional Contest Problem K. KMC Attacks 交互题 暴力

    Problem K. KMC Attacks 题目连接: http://codeforces.com/gym/100714 Description Warrant VI is a remote pla ...

  8. XVII Open Cup named after E.V. Pankratiev Grand Prix of Moscow Workshops, Sunday, April 23, 2017 Problem K. Piecemaking

    题目:Problem K. PiecemakingInput file: standard inputOutput file: standard outputTime limit: 1 secondM ...

  9. HDU 6343.Problem L. Graph Theory Homework-数学 (2018 Multi-University Training Contest 4 1012)

    6343.Problem L. Graph Theory Homework 官方题解: 一篇写的很好的博客: HDU 6343 - Problem L. Graph Theory Homework - ...

随机推荐

  1. mysql 主从复制 配置

    mysql 的 默认配置文件在 /etc/my.cnf 1 修改主库 配置文件: 设置 服务id,并且开启二进制日志文件. server-id=1 log-bin=mysql-bin 2重启服务:se ...

  2. JDBC 操作数据库实例

    JDBC是什么 JDBC代表Java数据库连接(Java Database Connectivity),它是用于Java编程语言和数据库之间的数据库无关连接的标准Java API,换句话说:JDBC是 ...

  3. WPF 实现简单的跑马灯

    本文用WPF的动画实现一个简单的跑马灯 xmal: <Window x:Class="wpfstatusBar.MainWindow" xmlns="http:// ...

  4. mysql优化-数据库设计基本原则

    mysql优化-数据库设计基本原则 一.数据库设计三范式 第一范式:字段具有原子性 原子性是指数据库的所有字段都不可被再次划分,如下表就不满足原子性,起点与终点 字段就可被拆分为起点与终点两个字段. ...

  5. hdoj 1325 Is It A Tree? 【并查集】

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/shengweisong/article/details/34099151 做了一上午,最终ac了 w ...

  6. CF1132G

    听说,一个好的Oier都是题目喂出来的. 题目 定义一个序列的最长贪心严格上升子序列为:若选出的子序列为 \(a\),对于其中相邻两项 \(i,j\),不存在 b\(i<k<j\),满足在 ...

  7. PHP通过sql生成CSV文件并下载,PHP实现文件下载

    /** * PHP通过sql生成CSV文件并下载 * @param string $sql 查询sql,结果为二维数组 * @param array $title 数据,CSV文件标题 * @para ...

  8. Handling Missing Values

    1) A Simple Option: Drop Columns with Missing Values 如果这些列具有有用信息(在未丢失的位置),则在删除列时,模型将失去对此信息的访问权限. 此外, ...

  9. UML时序图(Sequence Diagram)学习笔记

    什么是时序图时序图(Sequence Diagram),又名序列图.循序图,是一种UML交互图.它通过描述对象之间发送消息的时间顺序显示多个对象之间的动态协作. 让我们来看一看visio2016对时序 ...

  10. python 布尔值索引