二次联通门 : 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. vuex的Store简单使用过程

    介绍 Store的代码结构一般由State.Getters.Mutation.Actions这四种组成,也可以理解Store是一个容器,Store里面的状态与单纯的全局变量是不一样的,无法直接改变st ...

  2. C语言--简易词法分析器

    #include <stdio.h>#include <stdlib.h>#include <string.h>int p,m,syn,n,sum;       / ...

  3. Java自学-数组 Arrays

    java.util.Arrays类常用方法 Arrays是针对数组的工具类,可以进行 排序,查找,复制填充等功能. 大大提高了开发人员的工作效率. 步骤 1 : 数组复制 与使用System.arra ...

  4. springboot-实现log4j的AOP切面

    参考链接: https://www.cnblogs.com/liaojie970/p/7883687.html https://blog.csdn.net/autfish/article/detail ...

  5. Ubuntu安装Java环境经历

    1.权限不够 sudo su gedit /etc/sudoers 添加 用户名 ALL=(ALL:ALL) ALL 2.配置java 放到 /usr/lib/jvm/下 sudo gedit /et ...

  6. c++ 初始化静态static成员变量或static复合成员变量

    https://stackoverflow.com/questions/185844/how-to-initialize-private-static-members-in-c https://sta ...

  7. Gtest:死亡测试

    转自:玩转Google开源C++单元测试框架Google Test系列(gtest)之五 - 死亡测试 一.前言 “死亡测试”名字比较恐怖,这里的“死亡”指的的是程序的崩溃.通常在测试过程中,我们需要 ...

  8. 变长数组(variable-length array,VLA)(C99)

    处理二维数组的函数有一处可能不太容易理解,数组的行可以在函数调用的时候传递,但是数组的列却只能被预置在函数内部.例如下面这样的定义: #define COLS 4 int sum3d(int ar[] ...

  9. 微信小程序和APP优劣势大对比

    小程序的优势: 1. 无需下载,随走随关 2. 功能丰富,体验更简便 3. 接口众多,可以进行不断的开发 4. 流量入口大,背靠日活9.6亿的微信 5. 有强大的微信生态环境 小程序对比APP的好处: ...

  10. wait,waitpid

    要求 用man wait, man waitpid学习wait waitpid的使用 2 写出wait 的测试代码,要能说明你理解了wait 的返回值的每一位的含义 实验: ---------- ma ...