[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. Asp.net单点登录解决方案

    原文出处:http://www.cnblogs.com/wu-jian 主站:Passport集中验证服务器,DEMO中为:http://www.passport.com/ 分站:http://www ...

  2. [翻译] BTSimpleRippleButton

    BTSimpleRippleButton https://github.com/balram3429/btSimpleRippleButton This is a custom button for ...

  3. [翻译] SAMCoreImageView

    SAMCoreImageView https://github.com/soffes/SAMCoreImageView Render a CIImage in an OpenGL thingy so ...

  4. elastic search报错——“failed to obtain node locks”

    启动时报错信息: 这里写图片描述 寻找主要信息:failed to obtain node locks这里写图片描述简单理解为:绑定节点失败!!! 百度后,好多人同样遇到了这个问题,导致的原因可能是因 ...

  5. Top 25 Most Frequently Asked Interview Core Java Interview Questions And Answers

    We are sharing 25 java interview questions , these questions are frequently asked by the recruiters. ...

  6. 【接口】常见接口集合(返回JSON)

    转<JSON校验网站…>http://www.bejson.com/go.html?u=http://www.bejson.com/webInterface.html 这里为大家搜集了一些 ...

  7. 关于layui(layer)子页面获取不到父页面jQuery对象的问题。

    如果在使用layui-layer模块过程中,在子页面执行代码: window.parent.$("#id").val() 报错:window.parent.$ is not a f ...

  8. [转]Linux下查看CPU信息、机器型号等硬件信息

    From: http://www.jbxue.com/LINUXjishu/14582.html 查看CPU信息(型号) : # cat /proc/cpuinfo | grep name | cut ...

  9. 对于socket发送数据时是否要加锁及write read的阻塞非阻塞

    偶尔讨论到了socket发送数据时是否应该加锁的问题,就在网上查了一下,下面是大神陈硕的答案 对于 UDP,多线程读写同一个 socket 不用加锁,不过更好的做法是每个线程有自己的 socket,避 ...

  10. 解决Struts2 json-plugin Date或Timestamp等日期格式带T的问题

    如果没有对日期时间对象类进行json日期格式声明,会出现类似"2013-06-18T12:08:56.23"日期,在日期中间多出一个T字母: 从通过查询数据,以及调试程序发现直到返 ...