BZOJ 3631 松鼠的新家 - 树链剖分 / 树上差分
分析:
树链剖分:x->y,将x到y的路径加一,并将x端点的答案-1,最后统计答案。
树上差分:x->y,x+1,y+1,lca-1,fa[lca]-1,并将x打上标记,最后统计前缀和时将打上标记的点-1.
两种方法最后都要将终点答案-1.
code
差分
#include<bits/stdc++.h>
using namespace std;
namespace IO {
template<typename T>
inline void read(T &x) {
T i = 0, f = 1;
char ch = getchar();
for(; (ch < '0' || ch > '9') && ch != '-'; ch = getchar());
if(ch == '-') f = -1, ch = getchar();
for(; ch >= '0' && ch <= '9'; ch = getchar()) i = (i << 3) + (i << 1) + (ch - '0');
x = i * f;
}
template<typename T>
inline void wr(T x) {
if(x < 0) putchar('-'), x = -x;
if(x > 9) wr(x / 10);
putchar(x % 10 + '0');
}
} using namespace IO;
const int N = 3e5 + 5;
int n, a[N], ans[N];
vector<int> G[N];
int sum[N];
bool mark[N];
namespace Tree{int idx[N];}
namespace Tree{
int dep[N], son[N], top[N], pos[N], sze[N], tot, fa[N];
inline void dfs1(int u, int f){
fa[u] = f;
dep[u] = dep[f] + 1;
for(int e = G[u].size() - 1; e >= 0; e--){
int v = G[u][e];
if(v == f) continue;
dfs1(v, u);
sze[u] += sze[v];
if(sze[v] > sze[son[u]] || !son[u]) son[u] = v;
}
}
inline void dfs2(int u, int f){
if(son[u]){
pos[son[u]] = ++tot;
idx[tot] = son[u];
top[son[u]] = top[u];
dfs2(son[u], u);
}
for(int e = G[u].size() - 1; e >= 0; e--){
int v = G[u][e];
if(v == f || v == son[u]) continue;
pos[v] = ++tot;
idx[tot] = v;
top[v] = v;
dfs2(v, u);
}
}
inline void splitTree(){
dfs1(a[1], 0);
tot = 1, top[a[1]] = a[1], pos[a[1]] = 1, idx[1] = a[1];
dfs2(a[1], 0);
}
inline int getLca(int u, int v){
while(top[u] != top[v]){
if(dep[top[u]] < dep[top[v]]) swap(u, v);
u = fa[top[u]];
}
return dep[u] < dep[v] ? u : v;
}
}
inline void getSum(int u, int f){
for(int e = G[u].size() - 1; e >= 0; e--){
int v = G[u][e];
if(v == f) continue;
getSum(v, u);
sum[u] += sum[v];
}
}
int main() {
freopen("h.in", "r" ,stdin);
read(n);
for(int i = 1; i <= n; i++) read(a[i]);
for(int i = 1; i < n; i++){
int x, y; read(x), read(y);
G[x].push_back(y), G[y].push_back(x);
}
Tree::splitTree();
// dfs(a[1], 0);
sum[a[1]]++;
for(int i = 1; i < n; i++){
sum[a[i]]++, sum[a[i + 1]]++;
int lca = Tree::getLca(a[i], a[i + 1]);
mark[a[i]] = true;
sum[lca]--, sum[Tree::fa[lca]]--;
}
getSum(a[1], 0);
for(int i = 1; i <= n; i++) wr(sum[i] - (i == a[n] ? 1 : 0) - (mark[i] ? 1 : 0)), putchar('\n');
return 0;
}
树链剖分
#include<bits/stdc++.h>
using namespace std;
namespace IO {
template<typename T>
inline void read(T &x) {
T i = 0, f = 1;
char ch = getchar();
for(; (ch < '0' || ch > '9') && ch != '-'; ch = getchar());
if(ch == '-') f = -1, ch = getchar();
for(; ch >= '0' && ch <= '9'; ch = getchar()) i = (i << 3) + (i << 1) + (ch - '0');
x = i * f;
}
template<typename T>
inline void wr(T x) {
if(x < 0) putchar('-'), x = -x;
if(x > 9) wr(x / 10);
putchar(x % 10 + '0');
}
} using namespace IO;
const int N = 3e5 + 5;
int n, a[N], ans[N];
vector<int> G[N];
namespace Tree{int idx[N];}
namespace Seg{
int tree[N << 2], tag[N << 2];
inline void upt(int k){
tree[k] = tree[k << 1] + tree[k << 1 | 1];
}
inline void add(int k, int v, int l, int r){
tree[k] += (r - l + 1) * v;
tag[k] += v;
}
inline void modify(int k, int l, int r, int x, int y, int v){
if(x <= l && r <= y){
add(k, v, l, r);
return;
}
int mid = l + r >> 1, lc = k << 1, rc = k << 1 | 1;
if(x <= mid) modify(lc, l, mid, x, y, v);
if(y > mid) modify(rc, mid + 1, r, x, y, v);
upt(k);
}
inline void pushDown(int k, int l, int r){
int mid = l + r >> 1;
if(tag[k]){
add(k << 1, tag[k], l, mid);
add(k << 1 | 1, tag[k], mid + 1, r);
tag[k] = 0;
}
}
inline void getAns(int k, int l, int r){
if(l == r){
ans[Tree::idx[l]] += tree[k];
return;
}
int mid = l + r >> 1;
pushDown(k, l, r);
getAns(k << 1, l, mid);
getAns(k << 1 | 1, mid + 1, r);
}
}
namespace Tree{
int dep[N], son[N], top[N], pos[N], sze[N], tot, fa[N];
inline void dfs1(int u, int f){
fa[u] = f;
dep[u] = dep[f] + 1;
for(int e = G[u].size() - 1; e >= 0; e--){
int v = G[u][e];
if(v == f) continue;
dfs1(v, u);
sze[u] += sze[v];
if(sze[v] > sze[son[u]] || !son[u]) son[u] = v;
}
}
inline void dfs2(int u, int f){
if(son[u]){
pos[son[u]] = ++tot;
idx[tot] = son[u];
top[son[u]] = top[u];
dfs2(son[u], u);
}
for(int e = G[u].size() - 1; e >= 0; e--){
int v = G[u][e];
if(v == f || v == son[u]) continue;
pos[v] = ++tot;
idx[tot] = v;
top[v] = v;
dfs2(v, u);
}
}
inline void splitTree(){
dfs1(a[1], 0);
tot = 1, top[a[1]] = a[1], pos[a[1]] = 1, idx[1] = a[1];
dfs2(a[1], 0);
}
inline void pathModify(int u, int v){
while(top[u] != top[v]){
if(dep[top[u]] < dep[top[v]]) swap(u, v);
Seg::modify(1, 1, n, pos[top[u]], pos[u], 1);
u = fa[top[u]];
}
if(dep[u] > dep[v]) swap(u, v);
Seg::modify(1, 1, n, pos[u], pos[v], 1);
}
}
inline void dfs(int u, int f){
cout<<u<<" ";
for(int e = G[u].size() - 1; e >= 0; e--){
int v = G[u][e];
if(v == f) continue;
dfs(v, u);
}
cout<<endl;
}
int main() {
freopen("h.in", "r" ,stdin);
read(n);
for(int i = 1; i <= n; i++) read(a[i]);
for(int i = 1; i < n; i++){
int x, y; read(x), read(y);
G[x].push_back(y), G[y].push_back(x);
}
Tree::splitTree();
// dfs(a[1], 0);
ans[a[1]] = 1;
for(int i = 1; i < n; i++) Tree::pathModify(a[i], a[i + 1]), ans[a[i]]--;
Seg::getAns(1, 1, n);
for(int i = 1; i <= n; i++) wr(ans[i] - (i == a[n] ? 1 : 0)), putchar('\n');
return 0;
}