惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
S
Secure Thoughts
C
Cyber Attacks, Cyber Crime and Cyber Security
SecWiki News
SecWiki News
AWS News Blog
AWS News Blog
P
Proofpoint News Feed
H
Help Net Security
I
InfoQ
T
Tor Project blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Schneier on Security
Schneier on Security
The GitHub Blog
The GitHub Blog
S
Schneier on Security
The Hacker News
The Hacker News
博客园 - Franky
雷峰网
雷峰网
S
Security @ Cisco Blogs
Google Online Security Blog
Google Online Security Blog
Engineering at Meta
Engineering at Meta
G
GRAHAM CLULEY
Security Latest
Security Latest
The Cloudflare Blog
M
MIT News - Artificial intelligence
云风的 BLOG
云风的 BLOG
K
Kaspersky official blog
C
Cisco Blogs
Microsoft Security Blog
Microsoft Security Blog
宝玉的分享
宝玉的分享
Scott Helme
Scott Helme
腾讯CDC
阮一峰的网络日志
阮一峰的网络日志
A
Arctic Wolf
Latest news
Latest news
Last Week in AI
Last Week in AI
The Register - Security
The Register - Security
Google DeepMind News
Google DeepMind News
博客园 - 聂微东
T
Threatpost
G
Google Developers Blog
有赞技术团队
有赞技术团队
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
L
LINUX DO - 热门话题
aimingoo的专栏
aimingoo的专栏
S
Securelist
WordPress大学
WordPress大学
N
Netflix TechBlog - Medium
Y
Y Combinator Blog
T
The Exploit Database - CXSecurity.com
月光博客
月光博客
Recent Announcements
Recent Announcements

某岛

AtCoder Beginner Contest 409 Luogu P5325. 【模板】Min_25 筛 UOJ #188. 【UR #13】Sanrd AtCoder Beginner Contest 371 AtCoder Beginner Contest 369 RPGMaker 2k3 百科 OneShot 的考古 2024“开创拓芯”游戏创享节的相关记录 CJ 回来后的戒断反应 Luogu P10221. [省选联考 2024] 重塑时光 Luogu P5308 [COCI2018-2019#4] Akvizna wqs 二分 歌唱王国 Lean 相关 BZOJ 3153. Sone1 The 2023 ICPC World Finals Luxor 新巴别塔 Sora 的想象与思考 Facebook Hacker Cup 2023 Round 1 AtCoder Beginner Contest 322 LLaMA 2 相关 HuggingFace AI Game Jam ACL 2023 Trans 相关… Luogu P2053. [SCOI2007] 修车 Luogu P1973. [NOI2011] NOI 嘉年华 Luogu P1933. [NOI2010] 旅行路线 Luogu P1954. [NOI2010] 航空管制 Luogu P2048. [NOI2010] 超级钢琴 Luogu P2046. [NOI2010] 海拔 Luogu P3227. [HNOI2013] 切糕 Luogu P8500. [NOI2022] 冒泡排序 Luogu P3629. [APIO2010] 巡逻 USACO 2018 February Contest, Gold Problem 2. Directory Traversal Luogu P3647. [APIO2014] 连珠线 IZhO 2017. Problem F. Hard route SPOJ TWOPATHS. Two Paths 换根 dp 洪恩电脑 —— 开天辟地 Facebook Hacker Cup 2022 Round 2 Codeforces Round #875 Luogu P5828 边双连通图计数 EC Final 拉格朗日反演定理 Luogu P5827. 点双连通图计数 无标号连通图 AtCoder Beginner Contest 284 Luogu P4708. 画画 Luogu P6295. 有标号 DAG 计数 BZOJ #2863. 愤怒的元首 HDU 3303. Harmony Forever 聊聊《明日方舟 Side Story 孤星》与《崩坏:星穹铁道》 SGU 208. Toral Tickets 后日谈,SHLUG 月度分享(上) 钢琴练习 EasyRPG x ChatGPT ControlNet 相关 The 1st Universal Cup, Stage 4, Ukraine EasyRPG —— Sliding Puzzle The 1st Universal Cup, Stage 3, Poland DP 优化练习 NOI 2009 TypeDB Forces 2023 Nas 买来做什么… Global Game Jam 2023 参赛纪录 The 1st Universal Cup, Stage 2, Hongkong The 1st Universal Cup, Stage 0, Nanjing Codeforces Round #850 舟游同人游戏 RM2k3 机能增强 —— EasyRPG Player 魔改版 《海之歌》设定与剧本 dfs 序求 lca Codeforces Round #844 P3768 简单的数学题 AtCoder Beginner Contest 281 ChatGPT 相关 AtCoder Grand Contest 059 AtCoder Beginner Contest 280 Codeforces Global Round 24 事实核查,以乌鲁木齐火灾为例 SPOJ MUSKET. Musketeers Pinely Round 1 Note about FTX Permutation ICPC World Final 2021 CodeTON Round 3 Codeforces Round #831 Educational Codeforces Round 138 NovelAI 法术指南 卡农 Educational Codeforces Round 135 Codeforces Round #819 瓦喵之夏 NOI 2022 Luogu P3765 总统选举 Luogu P3369 【模板】普通平衡树 网络国家 旋转卡壳 OFAC Sanctions && Tornado Cash BZOJ 1185. [HNOI2007]最小矩形覆盖
SPOJ COT6. Cut on a tree
2021-09-09 · via 某岛

前排膜拜主席。。。

做了好几天。。总算搞定了。。

算法一:李超线段树 + 线段树合并

DP 结构和 COT3 一样,方程和 Cash 一样,都是动态维护凸壳,询问与某个向量的点积的最小值。。。
后来我系统的学习了一下 线段树合并
然后发现有一个题的做法是李超线段树 + 线段树合并,来搞树上 dp。。。

这个题的方程和那个题差不多。。。询问向量都是 (x, 1),所以用 kx+b 来搞更方便。。。
可以预处理出所有的 sw,来做离散化,然后打标记只要整体平移 b,不会影响线段树的结构。。。
然后就没有然后了。。。复杂度 O(nlog2n)。。。

经过这个题我们发现。。线性分段函数极值 和 动态凸包 问题是等价的。。
根据情况可以互相转换。。看怎么写好写。。。

似乎还有一种 O(nlogn) 的做法。。。dfs 序 + cdq 分治。。。(尝试了一下失败了,原因是 cdq 分治两边的状态会互相依赖。。。)
晚上再试试。。。

我发现之前的模板里有一个 bug 的地方(我靠居然连 COT6 的样例都过不去。。)

if (T[x].y(ml) > t.y(ml) || T[x].y(mr) > t.y(mr)) swap(T[x], t); //!

就是上面这行,必须要两边都检查一次,否则有可能恰好在边界,然后又相等的话,我这种写法就炸了。。。。

算法二:平衡树维护凸壳 + 启发式合并

https://t.me/TwilightParliament/102671

平衡树有很多写法。。。如果选择 Splay 的话,复杂度可以变成 O(nlogn) 。。。Splay 有着许多非常神奇的性质。。
比如 Dynamic Finger Property 。。。
对于 n>=m,把 m 个有序的元素按顺序插到一个已经有 n 个元素的 Splay 里。。
时间复杂度是 O(mlog(n/m))。。。
一通计算,可以证明启发式合并 n 个东西 from scratch 的总开销是 O(nlogn)。。
(在 n 个元素里顺序访问 m 个的总开销差不多就是把 n 分成 m 个数的和,log 以后加起来。
根据log的凹性,最差的情况是 m 个都是 n/m。。。)

不过我试图找以前自己的 Splay 维护凸壳的模板。。。交以前的题居然 WA 了。。。于是去搜了一下看看大家现在是怎么做的。。。
于是 找到了一份非常短的 Multiset 维护凸壳的代码。。。。。

考据了一下。。来自 CF 的某个瑞典的 IGM 选手 simonlindholm。。
模板仓库在 这里。。。

个人感觉这份代码实现的非常优秀。。。特别是插入过程。。。比 CLJ 的版本 都要短好多。。。

这个是一堆线性函数求极值的。。然后 p 维护的是相邻直线的交点。。。
查询的时候直接用 p 去二分查找就可以了。。。

只要对每个凸壳整体打标记,然后把模板从求最大改成求最小就可以通过了。。

const int N = int(1.2e6) + 9;

#define ll LL
struct Line {
	mutable ll k, m, p;
	ll eval (ll x) { return k*x+m; }
	bool operator<(const Line& o) const { return k > o.k; }
	bool operator<(ll x) const { return p < x; }
};

struct LC : multiset<Line,less<> > {
	const ll inf = LLONG_MAX;
	LL up;
	ll div(ll a, ll b) { return a/b - ((a^b) < 0 && a%b); }
	ll bet(const Line& x, const Line& y) {
		if (x.k == y.k) return x.m <= y.m ? inf : -inf;
		return div(y.m-x.m, x.k-y.k);
	}
	bool isect(iterator x, iterator y) {
		if (y == end()) { x->p = inf; return 0; }
		x->p = bet(*x,*y); return x->p >= y->p;
	}
	void add(ll k, ll m) {
	    m -= up;
		auto z = insert({k,m,0}), y = z++, x = y;
		while (isect(y,z)) z = erase(z);
		if (x != begin() && isect(--x, y)) isect(x, y=erase(y));
		while ((y=x) != begin() && (--x)->p >= y->p) isect(x, erase(y));
	}
	ll query(ll x) {
		assert(!empty());
		auto l = *lower_bound(x);
		return l.k*x+l.m + up;
	}
} H[N]; int HH[N];

const int NN = N * 20;
int n;

VI adj[N]; LL sw[N], dp[N]; int sz[N];


#define hu H[HH[u]]
#define hv H[HH[v]]

void dfs(int u,int p) {
    sw[u] += sw[p];
    sz[u] = 1;
    LL s = 0; int max_sz = 0, vv = 0;
    for (auto v: adj[u]) if (v != p) {
        dfs(v, u);
        s += dp[v]; sz[u] += sz[v];
        if (checkMax(max_sz, sz[v])) vv = v;
    }

    if (max_sz) {
        HH[u] = HH[vv];
        hu.up += s - dp[vv];
        for (auto v: adj[u]) if (v != p && v != vv) {
            hv.up += s - dp[v];
            for (auto p: hv) {
                hu.add(p.k, p.m + hv.up);
            }
        }
    } else {
        HH[u] = u;
    }

    hu.add(sw[u], s + sqr(sw[u]));
    dp[u] = sqr(sw[p]) + hu.query(-2*sw[p]);
}

int main() {

#ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
#endif

    RD(n); REP_1(i, n) RDD(sw[i]);
    DO(n-1) {
        int u, v; RD(u, v);
        adj[u].PB(v);
        adj[v].PB(u);
    }
    dfs(1,0);
    OT(dp[1]);
}

Posted by xiaodao
Category: 日常