二次联通门 : 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. Linux学习笔记之tail命令显示最后n行

    tail :输出文件的最后几行. 用于linux查看日志的时候很方便,假如日志文件为:Console.log用法:1. tail Console.log tail # 输出文件最后10行的内容 2. ...

  2. redis cluster slots数量 为何是16384(2的14次方)

    Redis 集群并没有使用一致性hash,而是引入了哈希槽的概念. Redis 集群有16384个哈希槽,每个key通过CRC16校验后对16384取模来决定放置哪个槽,集群的每个节点负责一部分has ...

  3. python程序打包exe文件

    第一步:安装依赖 pip install pyinstaller 第二步:移动文件 把python文件和一个exe拿来当图标的ico文件移动到同一文件中 注意:文件路径不用用中文!!!!!!!! 第三 ...

  4. 数组中[::-1]或[::-n]的区别,如三维数组[:,::-1,:]

    import numpy as npa=np.array([[11,12,13,14,15,16,17,18],[21,22,23,24,25,26,27,28],[31,32,33,34,35,36 ...

  5. Laravel入门及实践,快速上手ThinkSNS+二次开发

    温馨提示: l 本文纯干货,文字和代码居多,且适合零基础Laravel学习者: l 本文会新建一个名为 blog 的 Laravel 程序,这是一个非常简单的博客. l  欢迎随时关注ThinkSNS ...

  6. 学习笔记之Vim

    Vim (text editor) - Wikipedia https://en.wikipedia.org/wiki/Vim_(text_editor) Vim (/vɪm/;[4] a contr ...

  7. Oracle数据库之初识部分知识

    Oracle是比MySql更为严格的数据库.使用时需要更加严谨. 一.安装注意事项: 1.选择地址时需要注意好不能有汉字,以免造成安装的时候注册表显示监听失败: 2.可视化窗口PLSQL编辑器(相当于 ...

  8. MySQL 统计上一周从周一到周日的用户

    这个功能按理说很常见,奇怪的是很难搜索到一个合适的.稍微整理了下,具体的就不展开了,注意这个表中的时间为毫秒,这条语句拷贝复制就能用.照顾大部分的无脑码农. SELECT case when FROM ...

  9. 经典sql server基础语句不全

    1.几个简单的基本的sql语句 选择: select * from table1 where 范围 插入: insert into table1(field1,field2) values(value ...

  10. Java开发环境之Maven

    查看更多Java开发环境配置,请点击<Java开发环境配置大全> 肆章:Maven安装教程 1)下载Maven安装包 https://maven.apache.org/download.c ...