大概学了下树上莫队, 其实就是在欧拉序上跑莫队, 特判lca即可. #include <iostream> #include <algorithm> #include <cstdio> #include <math.h> #include <set> #include <map> #include <queue> #include <string> #include <string.h> #incl…
树上莫队就是把莫队搬到树上-利用欧拉序乱搞.. 子树自然是普通莫队轻松解决了 链上的话 只能用树上莫队了吧.. 考虑多种情况 [X=LCA(X,Y)] [Y=LCA(X,Y)] else void dfs(int u) { sz[u] = 1 ; rev[st[u] = ++ cnt] = u ; for(int i = 0 ; i < G[u].size() ; i ++) { int v = G[u][i] ; if(v == fa[u]) { continue ; } fa[v] = u…
题目链接 http://codeforces.com/blog/entry/43230树上莫队从这里学的,  受益匪浅.. #include <iostream> #include <vector> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <map> #include <set> #i…
题意: 给出一棵\(n(n \leq 4 \times 10^4)\)个节点的树,每个节点上有个权值,和\(m(m \leq 10^5)\)个询问. 每次询问路径\(u \to v\)上有多少个权值不同的点. 分析: 树分块 首先将树分块,每块的大小为\(\sqrt{n}\)左右. 然后将询问离线处理,按照区间上的莫队算法将询问按块排序. 这里有一道裸的树分块的题目. 树上的路径转移 定义\(S(u,v)\)表示路径\(u \to v\)上的点集,定义\(\bigoplus\)为集合的对称差,类…
树上莫队模板题. 使用欧拉序将树上路径转化为普通区间. 之后莫队维护即可.不要忘记特判LCA #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #define N 200005 using namespace std; int read() { ,f=;char ch=getchar(); ;ch=getchar();…
链接 https://vjudge.net/problem/SPOJ-COT2 https://www.luogu.org/problemnew/show/SP10707 思路 dfs欧拉序转化为普通莫队(并不算树上莫队,不过也可做) 好神仙啊,原来欧拉序是可以求任意两点的点,不过要加lca. 代码 #include <bits/stdc++.h> using namespace std; const int N=2e5+7; int read() { int x=0,f=1;char s=g…
题目大意:有一棵$n$个节点的树,第$i$个点有一个颜色$C_i$,$m$组询问,每次问$x->y$的路径上有多少种颜色 题解:树上莫队,把树按欧拉序展开成一条链,令第$i$个节点第一次出现在序列中为$in_i$,第二次为$out_i$,每一个询问就是看$in_x->in_y$中只出现一次的节点的颜色,但发现如果$x$不为$x,y$的$lca$的话$lca$不会被计入答案,特判一下就行 卡点:1$\sim$2.数组未开大 3.$tarjan$求$lca$时加询问加错 4.为先加入第一个点导致答…
题意:求一条链 \((u,v)\) 上不同的颜色数. 我们可以求出树的出栈入栈序(or 括号序?我也不确定). 图(from attack) 然后有一个很优美的性质: 设点 \(u\) 的入栈时间为 \(dfn[u]\) ,出栈时间为 \(low[u]\) 设两个点 \(u,v\) 满足 \(dfn[u]<dfn[v]\) 若 \(u\) 为 \(v\) 的 \(lca\),那么我们只需查询 \([dfn[u],dfn[v]]\) 中只出现一次的点有多少不同的颜色. 若 \(u\) , \(v\…
COT2 - Count on a tree II #tree You are given a tree with N nodes. The tree nodes are numbered from 1 to N. Each node has an integer weight. We will ask you to perform the following operation: u v : ask for how many different integers that represent…
COT2 - Count on a tree II You are given a tree with N nodes. The tree nodes are numbered from 1 to N. Each node has an integer weight. We will ask you to perform the following operation: u v : ask for how many different integers that represent the we…