「SPOJ10707」Count on a tree II

「SPOJ10707」Count on a tree II

传送门
树上莫队板子题。
锻炼基础,没什么好说的。
参考代码:

#include <algorithm>
#include <cstdio>
#include <cmath>
#define rg register
#define file(x) freopen(x".in", "r", stdin), freopen(x".out", "w", stdout)
using namespace std;
template < class T > inline void read(T& s) {
    s = 0; int f = 0; char c = getchar();
    while ('0' > c || c > '9') f |= c == '-', c = getchar();
    while ('0' <= c && c <= '9') s = s * 10 + c - 48, c = getchar();
    s = f ? -s : s;
}

const int _ = 40005, __ = 1e5 + 5;

int tot, head[_]; struct Edge { int ver, nxt; } edge[_ << 1];
inline void Add_edge(int u, int v) { edge[++tot] = (Edge) { v, head[u] }; head[u] = tot; }

int n, q, a[_], X0, X[_];
int fir[_], las[_], vis[_], dep[_], fa[17][_];
int len, ord[_ << 1], m, pos[_ << 1];
int ans, cnt[_], res[__];
struct node { int l, r, lca, id; } t[__];
inline bool cmp(const node& x, const node& y)
{ return pos[x.l] != pos[y.l] ? pos[x.l] < pos[y.l] : ((pos[x.l] & 1) ? x.r < y.r : y.r < x.r); }

inline void dfs(int u, int f) {
    fir[u] = ++len, ord[len] = u;
    dep[u] = dep[f] + 1, fa[0][u] = f;
    for (rg int i = 1; i <= 16; ++i) fa[i][u] = fa[i - 1][fa[i - 1][u]];
    for (rg int i = head[u]; i; i = edge[i].nxt) if (edge[i].ver != f) dfs(edge[i].ver, u);
    las[u] = ++len, ord[len] = u;
}

inline int LCA(int x, int y) {
    if (dep[x] < dep[y]) swap(x, y);
    for (rg int i = 16; ~i; --i) if (dep[fa[i][x]] >= dep[y]) x = fa[i][x];
    if (x == y) return x;
    for (rg int i = 16; ~i; --i) if (fa[i][x] != fa[i][y]) x = fa[i][x], y = fa[i][y];
    return fa[0][x];
}

inline void calc(int x) { vis[x] ? ans -= !--cnt[a[x]] : ans += !cnt[a[x]]++, vis[x] ^= 1; }

int main() {
    read(n), read(q);
    for (rg int i = 1; i <= n; ++i) read(a[i]), X[i] = a[i];
    sort(X + 1, X + n + 1);
    X0 = unique(X + 1, X + n + 1) - X - 1;
    for (rg int i = 1; i <= n; ++i) a[i] = lower_bound(X + 1, X + X0 + 1, a[i]) - X;
    for (rg int x, y, i = 1; i < n; ++i) read(x), read(y), Add_edge(x, y), Add_edge(y, x);
    dfs(1, 0);
    for (rg int x, y, lca, i = 1; i <= q; ++i) {
        read(x), read(y), lca = LCA(x, y);
        if (fir[x] > fir[y]) swap(x, y);
        if (x == lca)
            t[i].l = fir[x], t[i].r = fir[y], t[i].lca = 0;
        else
            t[i].l = las[x], t[i].r = fir[y], t[i].lca = lca;
        t[i].id = i;
    }
    m = sqrt(1.0 * len);
    for (rg int i = 1; i <= len; ++i) pos[i] = (i - 1) / m + 1;
    sort(t + 1, t + q + 1, cmp);
    for (rg int l = 1, r = 0, i = 1; i <= q; ++i) {
        while (l > t[i].l) calc(ord[--l]);
        while (r < t[i].r) calc(ord[++r]);
        while (l < t[i].l) calc(ord[l++]);
        while (r > t[i].r) calc(ord[r--]);
        if (t[i].lca) calc(t[i].lca);
        res[t[i].id] = ans;
        if (t[i].lca) calc(t[i].lca);
    }
    for (rg int i = 1; i <= q; ++i) printf("%d\n", res[i]);
    return 0;
}

原文地址:https://www.cnblogs.com/zsbzsb/p/12231689.html

时间: 2024-11-06 21:08:56

「SPOJ10707」Count on a tree II的相关文章

「luogu2633」Count on a tree

「luogu2633」Count on a tree 传送门 树上主席树板子. 每个节点的根从其父节点更新得到,查询的时候差分一下就好了. 参考代码: #include <algorithm> #include <cstdio> #define rg register #define file(x) freopen(x".in", "r", stdin), freopen(x".out", "w", s

SPOJ10707 COT2 - Count on a tree II 【树上莫队】

题目分析: 考虑欧拉序,这里的欧拉序与ETT欧拉序的定义相同而与倍增LCA不同.然后不妨对于询问$u$与$v$让$dfsin[u] \leq dfsin[v]$,这样对于u和v不在一条路径上,它们可以改成询问$dfsin[u]$到$dfsin[v]$.否则改成$dfsout[u]$到$dfsin[v]$,并加上LCA上的影响,如果在询问过程中没有加入就加入,否则删除. 代码: 1 #include<bits/stdc++.h> 2 using namespace std; 3 4 const

spoj COT2 - Count on a tree II

COT2 - Count on a tree II http://www.spoj.com/problems/COT2/ #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

AC日记——Count on a tree II spoj

Count on a tree II 思路: 树上莫队: 先分块,然后,就好办了: 来,上代码: #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define maxn 40005 #define maxm 100005 struct QueryType { in

「SPOJ1487」Query on a tree III

「SPOJ1487」Query on a tree III 传送门 把树的 \(\text{dfs}\) 序抠出来,子树的节点的编号位于一段连续区间,然后直接上建主席树区间第 \(k\) 大即可. 参考代码: #include <algorithm> #include <cstdio> #define rg register #define file(x) freopen(x".in", "r", stdin), freopen(x"

Count on a tree II(树上莫队)

Count on a tree II(luogu) Description 题目描述 给定一个n个节点的树,每个节点表示一个整数,问u到v的路径上有多少个不同的整数. 输入格式 第一行有两个整数n和m(n=40000,m=100000). 第二行有n个整数.第i个整数表示第i个节点表示的整数. 在接下来的n-1行中,每行包含两个整数u v,描述一条边(u,v). 在接下来的m行中,每一行包含两个整数u v,询问u到v的路径上有多少个不同的整数. 输出格式 对于每个询问,输出结果. Solutio

[SPOJ10707]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 weight of nodes there a

【SPOJ10707】COT2 - Count on a tree II

题目大意:给定一棵 N 个节点的无根树,每个节点有一个颜色.现有 M 个询问,每次询问一条树链上的不同颜色数. 题解:学会了树上莫队. 树上莫队是将节点按照欧拉序进行排序,将树上问题转化成序列上的问题进行求解的算法.需要分两种情况进行讨论,第一种情况是对于询问 x,y 来说,x 为 y 的祖先,则询问的区间为 \(st[x],st[y]\),第二种情况是 x 与 y 处在两个不同的子树内,这时发现 \(lca(x,y)\) 的欧拉序并不在 [ed[x], st[y]] 内,因此需要额外考虑 lc

SPOJ COT2 Count on a tree II(树上莫队)

题目链接:http://www.spoj.com/problems/COT2/ 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 perfrom the following operation: u v : ask for how many different integers that repr