[JOISC2018]道路建设

LOJ传送门

考的时候打的大暴力,其实想到了LCT,但是思路有点没转过来。就算想到了估计也不能切,我没有在考场写LCT的自信。。。

其实这题不是让你直接用LCT维护答案,只是借用了LCT的架构,让权值相同的点在同一棵splay中,用一棵splay顶端的点的权值表示这棵splay中所有点的权值。发现从根到某个点的操作跟LCT的access操作相似,可以把access操作魔改一下,一边splay、跳父亲,一边更新树状数组、统计答案。注意为了维护一棵splay的权值,rotate转到根的时候要更新根的权值。

代码非常简短:

#include <cstdio>
#include <cctype>
#include <algorithm>
#define R register
#define I inline
#define L long long
#define B 1000000
using namespace std;
const int N = 100003;
char buf[B], *p1, *p2;
I char gc() { return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, B, stdin), p1 == p2) ? EOF : *p1++; }
I int rd() {
R int f = 0;
R char c = gc();
while (c < 48 || c > 57)
c = gc();
while (c > 47 && c < 58)
f = f * 10 + (c ^ 48), c = gc();
return f;
}
L o;
int a[N], c[N], b[N], v[N], n, t;
struct T{int p, d[2], f, s;}e[N];
I void mdf(int x, int k) {
for (; x <= n; b[x] += k, x += x & -x)
if (v[x] ^ t)
v[x] = t, b[x] = 0;
}
I int qry(int x) {
R int r = 0;
for (; x; x ^= x & -x)
if (v[x] == t)
r += b[x];
return r;
}
I int nrt(int x) { return e[e[x].p].d[0] == x || e[e[x].p].d[1] == x; }
I void upd(int x) { e[x].s = e[e[x].d[0]].s + e[e[x].d[1]].s + 1; }
I void rtt(int x) {
R int f = e[x].p, g = e[f].p, b = e[f].d[0] == x, c = e[x].d[b];
if (nrt(f))
e[g].d[e[g].d[1] == f] = x;
else
e[x].f = e[f].f;
if (c)
e[c].p = f;
e[x].p = g, e[f].p = x, e[x].d[b] = f, e[f].d[!b] = c, upd(f);
}
I void spl(int x) {
R int f, g;
for (;nrt(x); rtt(x)) {
f = e[x].p, g = e[f].p;
if (nrt(f))
rtt((e[g].d[1] == f) ^ (e[f].d[1] == x) ? x : f);
}
upd(x);
}
I void acc(int x) {
for (R int y = 0; x; x = e[y = x].p)
spl(x), e[e[x].d[1]].f = e[x].f, o += 1ll * (e[e[x].d[0]].s + 1) * qry(e[x].f - 1), mdf(e[x].f, e[e[x].d[0]].s + 1), e[x].d[1] = y, upd(x);
}
int main() {
R int i, x, y;
n = rd();
for (i = 1; i <= n; ++i)
c[i] = rd(), a[i] = c[i];
sort(a + 1, a + n + 1);
for (i = 1; i <= n; ++i)
e[i].f = lower_bound(a + 1, a + n + 1, c[i]) - a;
for (i = 1; i < n; ++i)
x = rd(), y = rd(), ++t, o = 0, acc(x), spl(x), e[x].p = y, e[y].d[0] = x, upd(y), printf("%I64d\n", o);
return 0;
}

换Windows了,%I64d就懒得改了。

[JOISC2018]道路建设 LCT的更多相关文章

  1. LOJ2831 JOISC2018 道路建设 LCT、树状数组

    传送门 题目的操作大概是:求某个点到根的链的逆序对,然后对这条链做区间赋值 求某个点到根的链,就是LCT中的access操作,所以我们每一次把access过后的链打上标记,就可以做到区间赋值了. 计算 ...

  2. 【XSY2528】道路建设 LCT 可持久化线段树

    题目描述 给你一个\(n\)个点\(m\)条边图,\(q\)个询问,每次问你边权在\([l,r]\)之间的边组成的最小生成树(森林)的边权和.强制在线. \(n,m,q\leq 100000\) 题解 ...

  3. bzoj1626 / P2872 [USACO07DEC]道路建设Building Roads

    P2872 [USACO07DEC]道路建设Building Roads kruskal求最小生成树. #include<iostream> #include<cstdio> ...

  4. 洛谷——P2872 [USACO07DEC]道路建设Building Roads

    P2872 [USACO07DEC]道路建设Building Roads 题目描述 Farmer John had just acquired several new farms! He wants ...

  5. 洛谷 P2872 [USACO07DEC]道路建设Building Roads 题解

    P2872 [USACO07DEC]道路建设Building Roads 题目描述 Farmer John had just acquired several new farms! He wants ...

  6. 2018年全国多校算法寒假训练营练习比赛(第四场)B:道路建设

    传送门:https://www.nowcoder.net/acm/contest/76/B 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 65536K,其他语言131072K 64b ...

  7. USACO 07DEC 道路建设(Building Roads)

    Farmer John had just acquired several new farms! He wants to connect the farms with roads so that he ...

  8. 题解 P2872 【[USACO07DEC]道路建设Building Roads】

    这道题真的是令人窒息,Kruskal调了贼久一直RE,最后发现数组大小稍微少了那么一点点.(也就10倍吧..) 言归正传,根据本人的分析(以及算法标签的提示),这是一道求最小生成树的题目,当然要注意已 ...

  9. LOJ #2831. 「JOISC 2018 Day 1」道路建设 线段树+Link-cut-tree

    用 LCT 维护颜色相同连通块,然后在线段树上查一下逆序对个数就可以了. code: #include <cstdio> #include <algorithm> #inclu ...

随机推荐

  1. Custom Settings.in 配置信息收集

    [Settings] Priority=Default Properties=MyCustomProperty [Default] ;是否允许部署操作系统到目标计算机 OSInstall=YES ;是 ...

  2. el表达式便利map集合

    <c:forEach items="${b.goodMap}" var="entry" varStatus="status"> ...

  3. 最小生成数 克鲁斯卡尔 普里姆 matlab

    克鲁斯卡尔: function T=MST_Kruskal(G) n=0; if isfield(G,'w') && ~isempty(G.w) && size(G.w ...

  4. JqGrid中文文档之TreeGrid

    几年之前写过一个非常简单的jqgrid属性说明. 今天又用到jqgrid这个控件了,捣鼓了许久,第一个treegrid完成了 jQuery("#list1").jqGrid({ u ...

  5. codeforces 424D Biathlon Track

    codeforces 424D Biathlon Track 题意 题解 代码 #include<bits/stdc++.h> using namespace std; #define f ...

  6. Linux系统 开通防火墙端口

    Redhat 7内核 Linux系统  开通防火墙端口 使用systemctl 1.查看防火墙状态,root用户登录,执行命令systemctl status firewalld 2.开启防火墙:sy ...

  7. Spring Cloud Eureka 学习记录

    SpringCloud版本 <groupId>org.springframework.cloud</groupId> <artifactId>spring-clou ...

  8. mysql安装后找不到sock文件

    mysql rpm安装方式之后,启动找不到sock文件,经排查是之前安装的由mysql, 需要时yum list installed mysql 或者  rpm -qa |grep -i mysql ...

  9. Kafka设计解析(三)Kafka High Availability (下)

    转载自 技术世界,原文链接 Kafka设计解析(三)- Kafka High Availability (下) 摘要 本文在上篇文章基础上,更加深入讲解了Kafka的HA机制,主要阐述了HA相关各种场 ...

  10. 【题解】洛谷P4158 [SCOI2009] 粉刷匠(DP)

    次元传送门:洛谷P4158 思路 f[i][j][k][0/1]表示在坐标为(i,j)的格子 已经涂了k次 (0是此格子涂错 1是此格子涂对)涂对的格子数 显然的是 每次换行都要增加一次次数 那么当j ...