[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. MySQL:关于 unauthenticated user

    一.现象 在生产环境中我们偶尔会遇到show processlist:显示host为 unauthenticated user 这样的连接,同时伴有数据库服务器层面的load,sys cpu较高,或者 ...

  2. 使用CAReplicatorLayer [1]

    使用CAReplicatorLayer [1] 说明 https://developer.apple.com/library/ios/documentation/GraphicsImaging/Ref ...

  3. MVC中JavaScript和CSS的自动打包与压缩

    在程序中安装System.Web.Optimization程序集 依赖关系如下图所示: 添加BundleConfiguration类 代码如下所示 注意必须使用对应的ScriptBundle和Styl ...

  4. 我的第一个springboot应用+maven依赖管理

    第一步:使用Eclipse创建maven工程SpringBootFirst:工程目录如下 第二步:编写依赖文件pom.xml <project xmlns="http://maven. ...

  5. September 19th 2017 Week 38th Tuesday

    Live boldly. Push yourself. Don't settle. 勇敢生活,突破自我,永不设限! Don't indulge in the past, whether it was ...

  6. 【理解】 Error 10053和 Error 10054

    1. 10053 这个错误码的意思是:  A established connection was aborted by the software in your host machine, 一个已建 ...

  7. 1854. [SCOI2010]游戏【二分图】

    Description lxhgww最近迷上了一款游戏,在游戏里,他拥有很多的装备,每种装备都有2个属性,这些属性的值用[1,10000]之间的数表示.当他使用某种装备时,他只能使用该装备的某一个属性 ...

  8. 隐马尔可夫(HMM)模型

    隐马尔可夫(HMM)模型 隐马尔可夫模型,是一种概率图模型,一种著名的有向图模型,一种判别式模型.主要用于时许数据建模,在语音识别.自然语言处理等领域广泛应用. 概率图模型分为两类,一类:使用有向无环 ...

  9. HBase学习之路 (九)HBase phoenix的使用

    HBase phoenix的下载 下载地址http://mirror.bit.edu.cn/apache/phoenix/ 选择对应的hbase版本进行下载,测试使用的是hbase-1.2.6版本

  10. jenkins 基本插件