二次联通门 : 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. mysql执行出错:Table 'k_user' is read only

    执行sql的时候发现出错 Table 'k_user' is read only 1.给mysql权限 chmod 777 mysql 2.执行一下命令 mysqladmin -uroot -p re ...

  2. ArcGIS Engine开发鹰眼图的功能(基础篇)

    鹰眼是用于调节全视域范围内主地图显示范围情况的副地图.它体现了地图整体与详细局部的关系. 用户可以通过鼠标单击或者画框等动作实现鹰眼与主地图的交互情况. 鹰眼功能的原理是通过主地图窗口的地图控件和鹰眼 ...

  3. string.Compare()方法

    判断字符串中是否包含一个值 返回一个值,该值指示指定的 String 对象是否出现在此字符串中. String a = "abcd"; if(source.a("a&qu ...

  4. java之mybatis之helloworld

    1. MyBatis 是一款一流的支持自定义SQL.存储过程和高级映射的持久化框架. MyBatis几乎消除了所有的 JDBC 代码,也基本不需要手工去设置参数和获取检索结果. MyBatis几乎能够 ...

  5. Python特色的序列解包、链式赋值、链式比较

    一.序列解包 序列解包(或可迭代对象解包):解包就是从序列中取出其中的元素的过程,将一个序列(或任何可迭代对象)解包,并将得到的值存储到一系列变量中. 一般情况下要解包的序列包含的元素个数必须与你在等 ...

  6. webdriver切换frame的方法

    iframe: iframe 就是一个特殊的html 元素, 它在原来的html 范围内,开辟了一个新的HTML. iframe 元素会创建包含另外一个文档的内联框架(即行内框架) 理解:网页嵌套网页 ...

  7. ovirt平台新建kvm操作

    登录后点击管理门户,随后计算-->虚拟机--> 新建 一般情况下需要操作的选项如下: 普通: 操作系统:linux 优化目标:服务器 描述:自定义 nic1:选择默认的网络 系统: 内存, ...

  8. Jenkins+Docker+Git+Harbor流水线打包

    Jenkins+Docker+Git+Harbor流水线打包 环境: CentOS Linux release 7.6.1810 (Core) 192.168.247.214 Jenkins+dock ...

  9. golang读写文件

    1. 标准输入输出 os提供了标准输入输出文件: Stdin = NewFile(uintptr(syscall.Stdin), "/dev/stdin") Stdout = Ne ...

  10. vmware下ubuntu虚拟机如何安装vmware tools

      依次点击-->虚拟机-->安装VMware Tools   窗口下方会弹出安装提示   系统会加载安装驱动至光盘 双击--VMwareTools-****.tar.gz--的压缩包   ...