[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. java中如何打war包

    1.利用jdk里的工具   例如我们要打包的文件在D:\Project:运行 cmd: cd D:\Project 进入D:\Project ,然后输入jar -cvf  Project.war *回 ...

  2. oracle中存储过程把表导出txt文件

    create or replace directory MY_DIR as 'D:\MY_DIR\'; grant read,write on directory MY_DIR to adm; sel ...

  3. UINavigationController与UITabBarController相关问题

    UINavigationController与UITabBarController相关问题 UINavigationController与UITabBarController混用是非常常见的,有时候会 ...

  4. 工作总结 [all]

    2. 工作总结 3. 面试经验 4. 其他

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

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

  6. Spring MVC Hello World 404

    下面的例子说明了如何使用 Spring MVC 框架来编写一个简单的基于 web 的 Hello World 应用程序.下面让我们使用 Eclipse IDE,然后按照下面的步骤使用 Spring 的 ...

  7. Jenkins 配合 GitLab 实现分支的自动合并、自动创建 Tag

    Jenkins 配合 GitLab 实现分支的自动合并.自动创建 Tag 背景 GitFlow工作流简介 Gitflow工作流定义了一个围绕项目发布的严格分支模型,它会相对复杂一点,但提供了用于一个健 ...

  8. 使用MS的ScriptDom来拆解TSQL脚本

    此处提供9.1.40413.0版本的DLL一共4个:Microsoft.Data.Schema.dll.Microsoft.Data.Schema.ScriptDom.dll.Microsoft.Da ...

  9. python第十四课--排序及自定义函数之自定义函数(案例一)

    案例一: 演示自定义函数的使用:包含:1).定义格式的掌握2).函数的好处 自定义函数:实现打印矩形的操作两个原则需要考虑:1).有没有形参?有,2个 2).有没有返回值?没有. def printR ...

  10. CentOs7.2编译安装Nginx服务器

    1. 安装nginx依赖 首先安装nginx的依赖 yum install gcc gcc-c++ openssl openssl-devel cyrus-sasl-md5 2,创建nginx用户 如 ...