luoguP3359 改造异或树 线段树合并

删边转化为加边
然后每次用线段树合并就行.....
确确实实很简单
然而为什么线段树合并跑不过$splay$的启发式合并,常数稍大了点...
复杂度$O(n \log n)$
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
namespace remoon {
#define ri register int
#define ll long long
#define tpr template <typename ra>
#define rep(iu, st, ed) for(ri iu = st; iu <= ed; iu ++)
#define drep(iu, ed, st) for(ri iu = ed; iu >= st; iu --)
#define gc getchar
inline int read() {
int p = , w = ; char c = gc();
while(c > '' || c < '') { if(c == '-') w = -; c = gc(); }
while(c >= '' && c <= '') p = p * + c - '', c = gc();
return p * w;
}
int wr[], rw;
#define pc(iw) putchar(iw)
tpr inline void write(ra o, char c = '\n') {
if(!o) pc('');
if(o < ) o = -o, pc('-');
while(o) wr[++ rw] = o % , o /= ;
while(rw) pc(wr[rw --] + '');
pc(c);
}
}
using namespace std;
using namespace remoon; #define sid 200050
#define pid 5000500 ll ans;
ll sum[sid];
int n, id, cnp;
int x[sid], y[sid], p[sid];
int cap[sid], nxt[sid], node[sid], val[sid], s[sid];
int fa[sid], rt[sid], ls[pid], rs[pid], sz[pid]; inline void addedge(int u, int v, int w) {
nxt[++ cnp] = cap[u]; cap[u] = cnp;
node[cnp] = v; val[cnp] = w;
nxt[++ cnp] = cap[v]; cap[v] = cnp;
node[cnp] = u; val[cnp] = w;
} inline int find(int o) {
if(fa[o] == o) return o;
return fa[o] = find(fa[o]);
} inline void insert(int &now, int l, int r, int v) {
now = ++ id; sz[now] = ;
if(l == r) return;
int mid = (l + r) >> ;
if(v <= mid) insert(ls[now], l, mid, v);
else insert(rs[now], mid + , r, v);
} inline int merge(int x, int y, int l = , int r = << ) {
if(!x || !y) return x + y;
if(l == r) ans += 1ll * sz[x] * sz[y];
int mid = (l + r) >> ;
ls[x] = merge(ls[x], ls[y], l, mid);
rs[x] = merge(rs[x], rs[y], mid + , r);
sz[x] += sz[y];
return x;
} #define cur node[i]
inline void dfs(int o, int f) {
insert(rt[o], , << , s[o]);
for(int i = cap[o]; i; i = nxt[i])
if(cur != f) s[cur] = s[o] ^ val[i], dfs(cur, o);
} int main() {
n = read();
rep(i, , n - ) {
x[i] = read(); y[i] = read();
int w = read(); addedge(x[i], y[i], w);
}
rep(i, , n) fa[i] = i;
rep(i, , n - ) p[i] = read();
dfs(, );
drep(i, n - , ) {
int u = find(x[p[i]]), v = find(y[p[i]]);
fa[v] = u; merge(rt[u], rt[v]);
sum[i] = ans;
}
rep(i, , n) write(sum[i]);
return ;
}
luoguP3359 改造异或树 线段树合并的更多相关文章
- 浅谈树套树(线段树套平衡树)&学习笔记
0XFF 前言 *如果本文有不好的地方,请在下方评论区提出,Qiuly感激不尽! 0X1F 这个东西有啥用? 树套树------线段树套平衡树,可以用于解决待修改区间\(K\)大的问题,当然也可以用 ...
- HDU 5649 DZY Loves Sorting(二分答案+线段树/线段树合并+线段树分割)
题意 一个 \(1\) 到 \(n\) 的全排列,\(m\) 种操作,每次将一段区间 \([l,r]\) 按升序或降序排列,求 \(m\) 次操作后的第 \(k\) 位. \(1 \leq n \le ...
- [BZOJ4552][TJOI2016&&HEOI2016]排序(二分答案+线段树/线段树分裂与合并)
解法一:二分答案+线段树 首先我们知道,对于一个01序列排序,用线段树维护的话可以做到单次排序复杂度仅为log级别. 这道题只有一个询问,所以离线没有意义,而一个询问让我们很自然的想到二分答案.先二分 ...
- 权值线段树&线段树合并
权值线段树 所谓权值线段树,就是一种维护值而非下标的线段树,我个人倾向于称呼它为值域线段树. 举个栗子:对于一个给定的数组,普通线段树可以维护某个子数组中数的和,而权值线段树可以维护某个区间内数组元素 ...
- BZOJ 3439 Kpm的MC密码 (Trie树+线段树合并)
题面 先把每个串反着插进$Trie$树 每个节点的子树内,可能有一些节点是某些字符串的开头 每个节点挂一棵权值线段树,记录这些节点对应的原来字符串的编号 查询的时候在线段树上二分即可 为了节省空间,使 ...
- LG4556 [Vani有约会]雨天的尾巴 动态开点线段树+线段树合并
问题描述 LG4556 题解 对于每一个结点,建立一棵动态开点线段树. 然后自低向上合并线段树. 同时维护整个值域的最大值和最大值位置. \(\mathrm{Code}\) #include<b ...
- UOJ#470. 【ZJOI2019】语言 虚树,线段树合并
原文链接www.cnblogs.com/zhouzhendong/p/UOJ470.html 前言 做完情报中心来看这个题突然发现两题有相似之处然后就会做了. 题解 首先,我们考虑将所有答案点对分为两 ...
- EC Round 33 F. Subtree Minimum Query 主席树/线段树合并
这题非常好!!! 主席树版本 很简单的题目,给一个按照指定节点的树,树上有点权,你需要回答给定节点的子树中,和其距离不超过k的节点中,权值最小的. 肯定首先一想,按照dfs序列建树,然后按照深度为下标 ...
- B -- RE:从零开始的异世界生活 线段树
http://www.ifrog.cc/acm/problem/1117?contest=1016&no=1 其实我是第一次这样用线段树. 首先把所有出现过的数字全部离散化.那么数字就是从[1 ...
随机推荐
- 【树】ztree
ztree前端参见官网http://www.ztree.me/v3/main.php Action示例 public String initLabServerTree(){ return SUCCES ...
- js 禁用右键菜单、拖拽、选中、复制
//禁用拖拽 document.ondragstart = function () { return false; }; /** * 禁用右键菜单 */ document.oncontextmenu ...
- Ubuntu自定义终端窗口位置
方法一: 自定义终端启动快捷键 具体方法是自定义一个快速启动终端的快捷键,附带设置终端启动时的位置参数.首先获得需要放置窗口的目标位置信息,可以通过终端命令“ xwininfo ”来获得.步骤是首先打 ...
- UNIX环境高级编程 第14章 高级I/O
这一章涉及很多概念和函数,包括:非阻塞I/O.记录锁.I/O复用.异步I/O.readv和writev函数以及内存映射. 非阻塞I/O 在Unix中,可以将系统调用分为两种,一种是“低速”系统调用,另 ...
- 2016.6.24——vector<vector<int>>【Binary Tree Level Order Traversal】
Binary Tree Level Order Traversal 本题收获: 1.vector<vector<int>>的用法 vector<vector<int ...
- xmlHttpRequest 跨域和上传或下载进度条
跨域 XMLHttpRequest 请求 普通网页能够使用XMLHttpRequest对象发送或者接受服务器数据, 但是它们受限于同源策略. 扩展可以不受该限制. 任何扩展只要它先获取了跨域请求许可, ...
- Chapter 4 深入理解Caffe MNIST DEMO中的LeNet网络模型
明代思想家王阳明提出了"知行合一",谓认识事物的道理与在现实中运用此道理,是密不可分的一回事.我以为这样的中国哲学话语,对于学习者来说,极具启发意义,要细细体会.中华文明源远流长, ...
- HaoZipC不是内部或外部命令
Win7专业版,32位,HaoZip V3.2 将安装命令下HaoZipC.exe,HaoZip.dll,lang\HaoZipLang_chs.dll三个文件拷贝到C:\Windows\System ...
- .net HttpCrawler
using HtmlAgilityPack; using System; using System.Collections.Generic; using System.Diagnostics; usi ...
- maven scope 'provided' 和 ‘compile’的区别
解释 其实这个问题很简单. 对于scope=compile的情况(默认scope),也就是说这个项目在编译,测试,运行阶段都需要这个artifact(模块)对应的jar包在classpath中. 而对 ...