二次联通门 : BZOJ 3435: [Wc2014]紫荆花之恋

二次联通门 : luogu P3920 [WC2014]紫荆花之恋

/*
luogu P3920 [WC2014]紫荆花之恋 怀疑人生
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <ctime>
#include <algorithm>
#define rg register
#define INF 1e9
#define EPS 0.78
#define Max 100050
typedef long long LL;
int c[Max * ][], h[Max], d[Max], r[Max], li[Max], N, M, EC = ;
LL Answer; int dis[Max][], fi[Max], Dis[Max], Zero; bool Flag;
const int BUF = ; char Buf[BUF], *buf = Buf;
inline void read (int &n)
{
rg char c = getchar ();
for (n = ; !isdigit (*buf); ++ buf);
for (; isdigit (*buf); n = n * + *buf - '', ++ buf);
}
inline void Line (int x, int y, int z)
{
c[++ EC][] = y, c[EC][] = fi[x], fi[x] = EC, c[EC][] = z;
c[++ EC][] = x, c[EC][] = fi[y], fi[y] = EC, c[EC][] = z;
}
struct Node { Node *c[]; int v, s, val; };
Node pool[Max * ], *unp[Max * ], *Ta = pool, **top = unp, *_r[Max][], Null, *null = &Null;
struct Balance_Tree
{
inline Node* Get(int x)
{
Node *k = (top != unp) ? *--top : Ta ++;
k->c[] = k->c[] = null, k->s = , k->v = x, k->val = rand (); return k;
}
inline void Update (Node *x) { x->s = x->c[]->s + x->c[]->s + ; }
Node *Rebuild (int x, int y)
{
Node* R = Get (dis[li[]][y] - r[li[]]);
for (rg int i = ; i <= x; ++ i)
Inse_r (R, Get (dis[li[i]][y] - r[li[i]]));
return R;
}
void Rotate (Node *&x, Node *y)
{
bool k = y == x->c[];
x->c[!k] = y->c[k], y->c[k] = x, y->s=x->s;
Update(x), x = y; return;
}
int Query (Node *x, int y)
{
if (x == null) return ;
if (y >= x->v) return x->c[]->s + + Query (x->c[], y);
return Query (x->c[], y);
}
void Clear(Node* &x)
{
if (x == null) return;
*top ++ = x, Clear (x->c[]), Clear (x->c[]), x = null;
}
void Inse_r (Node *&x, Node *y)
{
bool flag = y->v > x->v;
if (x == null) { x = y; return; } else ++ x->s;
Inse_r (x->c[flag], y);
if (x->c[flag]->val > x->val) Rotate (x, x->c[flag]);
}
} B;
#undef Max
struct Divide_Tree
{
#define Max 100050
int suf[Max], ct[Max], sg[Max], s[Max], R, Sg;
int Get_R (int C)
{
int _C = li[]; rg int i, j;
for (i = C; i >= ; -- i)
for (s[li[i]] = , j = fi[li[i]]; j; j = c[j][])
if (!sg[c[j][]] && s[c[j][]] > ) s[li[i]] += s[c[j][]];
for (; true; )
{
bool flag = false;
for (i = fi[_C]; i; i = c[i][])
if (s[c[i][]] < s[_C] && !sg[c[i][]] && s[c[i][]] * >= C)
{ _C = c[i][], flag = true; break; }
if (!flag) break;
}
for (i = ; i <= C; ++ i) s[li[i]] = ;
return _C;
}
int Get_s (int x)
{
int le = , ri = ; li[] = x, s[x] = -;
for (rg int i; le <= ri; ++ le)
for (i = fi[li[le]]; i; i = c[i][])
if (!s[c[i][]] && !sg[c[i][]]) s[li[++ ri] = c[i][]] = -;
return ri;
}
void Clear (int x)
{
if (!sg[x] || sg[x] < Sg) return;
sg[x] = , B.Clear (_r[x][]), B.Clear (_r[x][]);
for (rg int i = fi[x]; i; i = c[i][]) Clear (c[i][]);
}
void Get (int x, int y)
{
ct[x] = , sg[x] = sg[suf[x]] + ; rg int i, j;
int _f = ; _r[x][] = B.Get (-y), _r[x][] = B.Get (c[EC][] - y);
memcpy (dis[x], dis[suf[x]], sizeof dis[suf[x]]);
for (i = ; i < sg[x]; ++ i) dis[x][i] += c[EC][];
for (dis[x][sg[x]] = , i = suf[x], j = sg[x] - ; i; i = suf[i], -- j)
{
++ ct[i];
if (j - && ct[i] / (ct[suf[i]] + 1.0) > EPS) _f = suf[i];
Answer += B.Query (_r[i][], y - dis[x][j]), B.Inse_r (_r[i][], B.Get (dis[x][j] - y));
if (j - ) Answer -= B.Query (_r[i][], y - dis[x][j - ]), B.Inse_r (_r[i][], B.Get (dis[x][j - ] - y));
}
if (_f) Sg = sg[_f], Clear (_f), Rebuild (_f, Sg, suf[_f]);
}
void Rebuild (int x, int y, int z)
{
int C = Get_s (x), _C = Get_R (C);
dis[_C][y] = , Dfs (_C, , y), sg[_C] = y;
if (z) suf[_C] = z; else suf[R = _C] = , _r[_C][] = null;
_r[_C][] = B.Rebuild (C, y), ct[_C] = C;
if (y > ) _r[_C][] = B.Rebuild (C, y - );
for (rg int i = fi[_C]; i; i = c[i][])
if (!sg[c[i][]]) Rebuild (c[i][], y + , _C);
}
void Dfs (int x,int y,int z)
{
for (rg int i = fi[x]; i; i = c[i][])
if (!sg[c[i][]] && c[i][] != y)
dis[c[i][]][z] = dis[x][z] + c[i][], Dfs (c[i][], x, z);
}
#undef Max
} A;
int main (int argc, char *argv[])
{
fread (buf, , BUF, stdin);
read (Zero), read (N), read (Zero), read (Zero), A.R = ;
Null.c[] = Null.c[] = &Null; int q, w;
srand (time ()), read (r[]), A.Get (, r[]); puts ("");
for (rg int i = ; i <= N; ++ i)
{
read (q), q = q ^ (Answer % (int) INF), read (w);
read (r[i]), h[i] = h[q] + , d[i] = d[q] + w;
Line (i, q, w), A.suf[i] = q, A.Get (i, r[i]);
printf ("%lld\n", Answer);
}
return ;
}

BZOJ 3435: [Wc2014]紫荆花之恋的更多相关文章

  1. bzoj 3435: [Wc2014]紫荆花之恋 替罪羊树维护点分治 && AC400

    3435: [Wc2014]紫荆花之恋 Time Limit: 240 Sec  Memory Limit: 512 MBSubmit: 159  Solved: 40[Submit][Status] ...

  2. 【BZOJ3435】[Wc2014]紫荆花之恋 替罪点分树+SBT

    [BZOJ3435][Wc2014]紫荆花之恋 Description 强强和萌萌是一对好朋友.有一天他们在外面闲逛,突然看到前方有一棵紫荆树.这已经是紫荆花飞舞的季节了,无数的花瓣以肉眼可见的速度从 ...

  3. luogu P3920 [WC2014]紫荆花之恋

    LINK:紫荆花之恋 每次动态加入一个节点 统计 有多少个节点和当前节点的距离小于他们的权值和. 显然我们不能n^2暴力. 考虑一个简化版的问题 树已经给出 每次求某个节点和其他节点的贡献. 不难想到 ...

  4. BZOJ 3435 / Luogu 3920 [WC2014]紫荆花之恋 (替罪羊树 动态点分治 套 Treap)

    题意 略 分析 引用PoPoQQQ的话 吾辈有生之年终于把这道题切了...QAQ (蒟蒻狂笑) Orz PoPoQQQ,我又抄PoPoQQQ的题解了 - 突然发现有旋Treap没那么难写 学习了一波C ...

  5. BZOJ3435 & 洛谷3920 & UOJ55:[WC2014]紫荆花之恋

    https://www.lydsy.com/JudgeOnline/problem.php?id=3435 https://www.luogu.org/problemnew/show/P3920 ht ...

  6. BZOJ3435: [Wc2014]紫荆花之恋(替罪羊树,Treap)

    Description 强强和萌萌是一对好朋友.有一天他们在外面闲逛,突然看到前方有一棵紫荆树.这已经是紫荆花飞舞的季节了,无数的花瓣以肉眼可见的速度从紫荆树上长了出来.仔细看看的话,这个大树实际上是 ...

  7. BZOJ3435[Wc2014]紫荆花之恋——动态点分治(替罪羊式点分树套替罪羊树)

    题目描述 强强和萌萌是一对好朋友.有一天他们在外面闲逛,突然看到前方有一棵紫荆树.这已经是紫荆花飞舞的季节了,无数的花瓣以肉眼可见的速度从紫荆树上长了出来.仔细看看的话,这个大树实际上是一个带权树.每 ...

  8. [WC2014]紫荆花之恋(动态点分治+替罪羊思想)

    题目描述 强强和萌萌是一对好朋友.有一天他们在外面闲逛,突然看到前方有一棵紫荆树.这已经是紫荆花飞舞的季节了,无数的花瓣以肉眼可见的速度从紫荆树上长了出来.仔细看看的话,这个大树实际上是一个带权树.每 ...

  9. UOJ#55 [WC2014]紫荆花之恋

    题目描述 强强和萌萌是一对好朋友.有一天他们在外面闲逛,突然看到前方有一棵紫荆树.这已经是紫荆花飞舞的季节了,无数的花瓣以肉眼可见的速度从紫荆树上长了出来. 仔细看看的话,这个大树实际上是一个带权树. ...

随机推荐

  1. (1)ASP.NET Core 应用启动Startup类简介

    1.前言 Core与早期版本的 ASP.NET 对比,配置应用程序的方式的 Global.asax.FilterConfig.cs和RouteConfig.cs 都被Program.cs 和 Star ...

  2. 基于hystrix的线程池隔离

    hystrix进行资源隔离,其实是提供了一个抽象,叫做command,就是说,你如果要把对某一个依赖服务的所有调用请求,全部隔离在同一份资源池内 对这个依赖服务的所有调用请求,全部走这个资源池内的资源 ...

  3. python3的pip3安装

    ---恢复内容开始--- pip3的安装需要对应一整套python的编译工具库,所以安装好的pip3是这个样子: inear@Ai:~$ pip3 -V pip 18.1 from /usr/lib/ ...

  4. Tomcat组件梳理--Catalina

    Tomcat组件梳理--Catalina 1.定义和功能 Catalina是Tomcat的核心组件,是Servlet容器,Catalina包含了所有的容器组件,其他模块均为Catalina提供支撑.通 ...

  5. 【开发工具】- 推荐一款好用的文本编辑器[Sublime Text]

    作为一个程序员除了IDE外,文本编辑器也是必不可少的一个开发工具.之前一直在用的是NotePad++.EditPlus,这两款编辑器,但是总感觉差点什么,昨天在知乎上看到有人推荐Sublime Tex ...

  6. 【转载】C#中List集合使用GetRange方法获取指定索引范围内的所有值

    在C#的List集合中有时候需要获取指定索引位置范围的元素对象来组成一个新的List集合,此时就可使用到List集合的扩展方法GetRange方法,GetRange方法专门用于获取List集合指定范围 ...

  7. Function.prototype.apply.call 理解分析

    首先需要了解apply,call的基本用法,其目的是改变调用方法中的this指向,将其指向为传入的对象,改变this的指向,两种方法接收参数的方式不同. 代码:console.log var cons ...

  8. Vue项目开发相关问题总结

    Vue项目开发相关问题总结 一.创建一个项目(两种方式) 1.通过CLI命令行创建,具体步骤如下: (1)Node 版本要求 Vue CLI 需要 Node.js 8.9 或更高版本 (推荐 8.11 ...

  9. Workerman简单开发示例实践(一)

    一.去官网下载workerman,地址:https://www.workerman.net/download,下载后解压任意文件夹. 二.在解压文件目录下新建http_test.php,输入如下代码: ...

  10. RTP包的结构

    live555中数据的发送最后是要使用RTP协议发送的,下面介绍一下RTP包格式. RTP packet RTP是基于UDP协议的,RTP服务器会通过UDP协议,通常每次会发送一个RTP packet ...