原文链接 https://www.cnblogs.com/cly-none/p/CSA72G.html

题意:有一个\(n \times n\)的矩阵\(A\),\(m\)次操作,每次在\(A\)上三角部分的一个子矩形中加上一个数。最后构造\(n\)个点的图\(G\),且对于所有\(i,j \ (i < j)\),边\((i,j)\)的边权为\(A_{i,j}\)。求图\(G\)的最小生成树的边权和。

\(n,m \leq 10^5\)

先把上三角矩阵补成邻接矩阵。这样每次操作就是加两个邻接矩阵的子矩形。

这种题目通常要对经典算法进行拓展。常用的最小生成树算法有Prim和Kruskal,然而在尝试之后我们发现,由于边权种类太多,Prim不行;同样Kruskal也难以提出排序后的边权。

但还有Borůvka。这个算法要求对每个联通块找到边权最小的邻边,还要合并联通块。后者用并查集能容易实现,现在仅考虑前者。

先想一个更简单的问题:对每个结点找到边权最小的邻边。这是简单的,我们只需要扫描邻接矩阵的每一行,这样每次矩形加都变成了两个区间加。用线段树维护最小值就好了。考虑原问题。这个最小值可能和这个点在同一个联通块内,因此,非常套路地,我们就再记录与最小值不在一个联通块内的次小值就可以了。

因为上述扫描线需要执行\(O(\log n)\)次,故复杂度为\(O(n \log^2 n)\)。

#include <bits/stdc++.h>
#define data DATA
using namespace std;
#define gc() getchar()
template <typename tp>
inline void read(tp& x) {
x = 0; char tmp; bool key = 0;
for (tmp = gc() ; !isdigit(tmp) ; tmp = gc())
key = (tmp == '-');
for ( ; isdigit(tmp) ; tmp = gc())
x = (x << 3) + (x << 1) + (tmp ^ '0');
if (key) x = -x;
}
typedef long long ll;
const int N = 100010;
const ll INF = 1ll << 60;
int n,m,uni[N],cnt;
ll ans;
int getfa(int pos) {
return pos == uni[pos] ? pos : uni[pos] = getfa(uni[pos]);
}
struct data {
int p,l,r,v;
bool operator < (const data& a) const {
return p < a.p;
}
} dat[N << 2];
typedef pair<ll,int> pli;
#define fi first
#define se second
struct node {
pli mn[2];
ll tag;
node() {
tag = 0;
mn[0] = mn[1] = pli(INF,0);
}
} t[N << 2];
void puttag(int x,ll v) {
t[x].mn[0].fi += v;
t[x].mn[1].fi += v;
t[x].tag += v;
}
void push_down(int x) {
puttag(x<<1,t[x].tag);
puttag(x<<1|1,t[x].tag);
t[x].tag = 0;
}
void push_up(node& x,node ls,node rs) {
x.mn[1].fi = INF;
if (ls.mn[0] < rs.mn[0]) {
x.mn[0] = ls.mn[0];
if (rs.mn[0].se != x.mn[0].se)
x.mn[1] = rs.mn[0];
} else {
x.mn[0] = rs.mn[0];
if (ls.mn[0].se != x.mn[0].se)
x.mn[1] = ls.mn[0];
}
if (ls.mn[1].se != x.mn[0].se)
x.mn[1] = min(x.mn[1], ls.mn[1]);
if (rs.mn[1].se != x.mn[0].se)
x.mn[1] = min(x.mn[1], rs.mn[1]);
}
void modify(int l,int r,ll v,int x=1,int lp=1,int rp=n) {
if (lp > r || l > rp) return;
if (lp >= l && rp <= r)
return (void) puttag(x,v);
push_down(x);
int mid = (lp + rp) >> 1;
modify(l,r,v,x<<1,lp,mid);
modify(l,r,v,x<<1|1,mid+1,rp);
push_up(t[x],t[x<<1],t[x<<1|1]);
}
void build(int x=1,int lp=1,int rp=n) {
t[x].tag = 0;
if (lp == rp) {
t[x].mn[0] = pli(0ll,uni[lp]);
t[x].mn[1] = pli(INF,0);
return;
}
int mid = (lp + rp) >> 1;
build(x<<1,lp,mid);
build(x<<1|1,mid+1,rp);
push_up(t[x], t[x<<1], t[x<<1|1]);
}
pli nex[N];
void solve() {
build();
for (int i = 1 ; i <= n ; ++ i)
nex[i] = pli(INF,0);
for (int i = 1, j = 1 ; i <= n ; ++ i) {
while (j <= cnt && dat[j].p <= i)
modify(dat[j].l, dat[j].r, dat[j].v), ++ j;
node tmp = t[1];
if (tmp.mn[0].se != uni[i])
nex[uni[i]] = min(nex[uni[i]], tmp.mn[0]);
else nex[uni[i]] = min(nex[uni[i]], tmp.mn[1]);
}
for (int i = 1, j ; i <= n ; ++ i) {
j = getfa(i);
if (nex[j].se == INF) continue;
if (getfa(nex[j].se) != j) {
ans += nex[j].fi;
uni[j] = getfa(nex[j].se);
}
}
}
bool check() {
for (int i = 1 ; i <= n ; ++ i)
uni[i] = getfa(i);
for (int i = 2 ; i <= n ; ++ i)
if (uni[i] != uni[i-1]) return 1;
return 0;
}
signed main() {
read(n), read(m);
for (int i = 1, a, b, c, d, e ; i <= m ; ++ i) {
read(a), read(b), read(c), read(d), read(e);
dat[++cnt] = (data) {a, c, d, e};
dat[++cnt] = (data) {b+1, c, d, -e};
dat[++cnt] = (data) {c, a, b, e};
dat[++cnt] = (data) {d+1, a, b, -e};
}
for (int i = 1 ; i <= n ; ++ i)
uni[i] = i;
sort(dat+1,dat+cnt+1);
while (check())
solve();
cout << ans << endl;
return 0;
}

小结:本题做法乍一看是几个套路的综合,但并不简单。还是要求清晰的思维,以及熟练掌握基础算法和技巧。

【做题】CSA72G - MST and Rectangles——Borůvka&线段树的更多相关文章

  1. 【CSA72G】【XSY3316】rectangle 线段树 最小生成树

    题目大意 有一个 \(n\times n\) 的矩阵 \(A\).最开始 \(A\) 中每个元素的值都为 \(0\). 有 \(m\) 次操作,每次给你 \(x_1,x_2,y_1,y_2,w\),对 ...

  2. 刷题总结——二逼平衡树(bzoj3224线段树套splay)

    题目: Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:1.查询k在区间内的排名2.查询区间内排名为k的值3.修改某一位值上的数值4.查询k在 ...

  3. GSS4 - Can you answer these queries IV || luogu4145上帝造题的七分钟2 / 花神游历各国 (线段树)

    GSS4 - Can you answer these queries IV || luogu4145上帝造题的七分钟2 / 花神游历各国 GSS4 - Can you answer these qu ...

  4. Codeforces 1396D - Rainbow Rectangles(扫描线+线段树)

    Codeforces 题面传送门 & 洛谷题面传送门 一道鸽了整整一年的题目,上一次提交好像是 2020 年 9 月 13 日来着的(?) 乍一看以为第 2 个提交和第 3 个提交只差了 43 ...

  5. Gym - 101982F Rectangles (扫描线+线段树)

    链接:http://codeforces.com/gym/101982/attachments 思路: 问被覆盖次数为奇数次的矩阵的面积并 扫描线求矩阵面积并我们是上界赋为-1,下界赋为1,因为要求覆 ...

  6. day1 晚上 P4145 上帝造题的七分钟2 / 花神游历各国 线段树

    #include<iostream> #include<cstdio> #include<cmath> using namespace std; ; struct ...

  7. [日记&做题记录]-Noip2016提高组复赛 倒数十天

    写这篇博客的时候有点激动 为了让自己不颓 还是写写日记 存存模板 Nov.8 2016 今天早上买了两个蛋挞 吃了一个 然后就做数论(前天晚上还是想放弃数论 但是昨天被数论虐了 woc noip模拟赛 ...

  8. NOIP2016考前做题(口胡)记录

    NOIP以前可能会持续更新 写在前面 NOIP好像马上就要到了,感觉在校内训练里面经常被虐有一种要滚粗的感觉(雾.不管是普及组还是提高组,我都参加了好几年了,结果一个省一都没有,今年如果还没有的话感觉 ...

  9. zoj-1610线段树刷题

    title: zoj-1610线段树刷题 date: 2018-10-16 16:49:47 tags: acm 刷题 categories: ACM-线段树 概述 这道题是一道简单的线段树区间染色问 ...

随机推荐

  1. 从github上下载一个项目的子目录

    https://github.com/pbojinov/developer.chrome.com/tree/master/extensions/examples/extensions/proxy_co ...

  2. 当离开浏览器窗口,提示语title更改

    head里面插入一下代码 <script> document.addEventListener('visibilitychange',function(){if(document.visi ...

  3. PySe-007-解决“Chrome正在受到自动软件的控制”

    python使用selenium启动chrome的代码如下所示: #!/usr/local/bin/python # -*- coding: utf-8 -*- from selenium impor ...

  4. opencart3属性attribute实现换行等简单html代码

    opencart3属性attribute在前台页面默认是没有解析html代码功能的,比如想实现换行,后台这样写:line 1<br>line 2,但前台产品页也是line 1<br& ...

  5. 学习animation、transition、transform和@keyframes的使用

    当我们需要给页面添加动画效果时,需要用到CSS3的animation样式属性: 例如: animation: test 2s infinite; 其中test是动画的名称,2s是动画的时长,infin ...

  6. Hydra(爆破神器)

    PS:这款暴力密码破解工具相当强大,支持几乎所有协议的在线密码破解,其密码能否被破解关键在于字典是否足够强大.对于社会工程型渗透来说,有时能够得到事半功倍的效果.本文仅从安全角度去探讨测试,使用本文内 ...

  7. OI养老专题02:约瑟夫问题求幸存者

    如题.人数为n(1<=n<=30000),共k(1<=k<=30000)组数据,所报的数m恒为2,只要求输出幸存者. 如果你还不知道什么是约瑟夫问题...——https://w ...

  8. 对int类型的数据,如何让获取长度

    下面为大家写一个列子   int a = 124;<br> Integer a1 = a;//转换为包装类Integer<br> System.out.println(a1.t ...

  9. [macOS] finder变慢提速

    原文地址:http://ntfs-formac.com/fix-slow-finder-macos-sierra/ 我采取的是第二种方法,够简单,直接在终端执行 rm ~/Library/Caches ...

  10. web攻击之xss(一)

    1,xss简介 跨站脚本攻击(Cross Site Scripting),为了不和层叠样式表(Cascading Style Sheets, CSS)的缩写混淆,故将跨站脚本攻击缩写为XSS.恶意攻击 ...