CF620E New Year Tree 题解
题目链接:CF 或者 洛谷
这题很简单,看到颜色数,HH的项链?树,树上的HH的项链?带修,树上的镜中的昆虫?\(c_i \le 60\),噢,easy 了。
考虑一类信息,表示有和无,对于某种颜色来讲,\(0/1\) 表示无或者有,而或运算让我们从小区间的有无情况反映到大区间的有无情况。一种暴力的想法,为每种颜色建立一棵有无颜色的线段树,每次依次查询这 \(60\) 棵线段树对应区间内的该颜色的有无情况,\(60\times m\log{n}\) 看上去可行,不过我们有一种更简单的方式反应一堆量的有无,显然是:\(状态压缩\),观察到颜色不超过 \(60\) 种,我们每个维护一个 \(64\ 位的\ val\) 即可。观察到有覆盖操作,那么再加一个覆盖懒标记就好了。由于跟子树有关,那就别考虑树剖了,直接 \(dfs\) 序简简单单的,每次我们可以用 \(1<<(val-1)\) 表示 \(val\) 表示这种颜色是有的。线段树维护状态压缩即可。
参照代码
#include <bits/stdc++.h>
// #pragma GCC optimize(2)
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
// #define isPbdsFile
#ifdef isPbdsFile
#include <bits/extc++.h>
#else
#include <ext/pb_ds/priority_queue.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/list_update_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/exception.hpp>
#include <ext/rope>
#endif
using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> tii;
typedef tuple<ll, ll, ll> tll;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef __int128 i128;
#define hash1 unordered_map
#define hash2 gp_hash_table
#define hash3 cc_hash_table
#define stdHeap std::priority_queue
#define pbdsHeap __gnu_pbds::priority_queue
#define sortArr(a, n) sort(a+1,a+n+1)
#define all(v) v.begin(),v.end()
#define yes cout<<"YES"
#define no cout<<"NO"
#define Spider ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
#define MyFile freopen("..\\input.txt", "r", stdin),freopen("..\\output.txt", "w", stdout);
#define forn(i, a, b) for(int i = a; i <= b; i++)
#define forv(i, a, b) for(int i=a;i>=b;i--)
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
#define endl '\n'
//用于Miller-Rabin
[[maybe_unused]] static int Prime_Number[13] = {0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37};
template <typename T>
int disc(T* a, int n)
{
return unique(a + 1, a + n + 1) - (a + 1);
}
template <typename T>
T lowBit(T x)
{
return x & -x;
}
template <typename T>
T Rand(T l, T r)
{
static mt19937 Rand(time(nullptr));
uniform_int_distribution<T> dis(l, r);
return dis(Rand);
}
template <typename T1, typename T2>
T1 modt(T1 a, T2 b)
{
return (a % b + b) % b;
}
template <typename T1, typename T2, typename T3>
T1 qPow(T1 a, T2 b, T3 c)
{
a %= c;
T1 ans = 1;
for (; b; b >>= 1, (a *= a) %= c)if (b & 1)(ans *= a) %= c;
return modt(ans, c);
}
template <typename T>
void read(T& x)
{
x = 0;
T sign = 1;
char ch = getchar();
while (!isdigit(ch))
{
if (ch == '-')sign = -1;
ch = getchar();
}
while (isdigit(ch))
{
x = (x << 3) + (x << 1) + (ch ^ 48);
ch = getchar();
}
x *= sign;
}
template <typename T, typename... U>
void read(T& x, U&... y)
{
read(x);
read(y...);
}
template <typename T>
void write(T x)
{
if (typeid(x) == typeid(char))return;
if (x < 0)x = -x, putchar('-');
if (x > 9)write(x / 10);
putchar(x % 10 ^ 48);
}
template <typename C, typename T, typename... U>
void write(C c, T x, U... y)
{
write(x), putchar(c);
write(c, y...);
}
template <typename T11, typename T22, typename T33>
struct T3
{
T11 one;
T22 tow;
T33 three;
bool operator<(const T3 other) const
{
if (one == other.one)
{
if (tow == other.tow)return three < other.three;
return tow < other.tow;
}
return one < other.one;
}
T3() { one = tow = three = 0; }
T3(T11 one, T22 tow, T33 three) : one(one), tow(tow), three(three)
{
}
};
template <typename T1, typename T2>
void uMax(T1& x, T2 y)
{
if (x < y)x = y;
}
template <typename T1, typename T2>
void uMin(T1& x, T2 y)
{
if (x > y)x = y;
}
constexpr int N = 4e5 + 10;
struct Node
{
ll val, cover;
} node[N << 2];
#define val(x) node[x].val
#define cover(x) node[x].cover
inline void push_up(const int curr)
{
val(curr) = val(ls(curr)) | val(rs(curr));
}
inline void push_down(const int curr)
{
if (cover(curr))
{
val(ls(curr)) = val(rs(curr)) = cover(ls(curr)) = cover(rs(curr)) = cover(curr);
cover(curr) = 0;
}
}
int n, q;
inline void Cover(const int curr, const int l, const int r, const ll val, const int s = 1, const int e = n)
{
const int mid = s + e >> 1;
if (l <= s and e <= r)
{
val(curr) = cover(curr) = val;
return;
}
push_down(curr);
if (l <= mid)Cover(ls(curr), l, r, val, s, mid);
if (r > mid)Cover(rs(curr), l, r, val, mid + 1, e);
push_up(curr);
}
inline ll Query(const int curr, const int l, const int r, const int s = 1, const int e = n)
{
if (l <= s and e <= r)return val(curr);
push_down(curr);
const int mid = s + e >> 1;
ll ans = 0;
if (l <= mid)ans |= Query(ls(curr), l, r, s, mid);
if (r > mid)ans |= Query(rs(curr), l, r, mid + 1, e);
return ans;
}
int cnt, s[N], e[N];
vector<int> child[N];
int a[N];
int rnk[N];
inline void dfs(const int curr, const int fa)
{
s[curr] = ++cnt;
rnk[cnt]=curr;
for (const auto nxt : child[curr])if (nxt != fa)dfs(nxt, curr);
e[curr] = cnt;
}
inline void build(const int curr = 1, const int l = 1, const int r = n)
{
const int mid = l + r >> 1;
if (l == r)
{
val(curr) = 1LL << a[rnk[l]];
return;
}
build(ls(curr), l, mid);
build(rs(curr), mid + 1, r);
push_up(curr);
}
inline void solve()
{
cin >> n >> q;
forn(i, 1, n)cin >> a[i], a[i]--;
forn(i, 1, n-1)
{
int u, v;
cin >> u >> v;
child[u].push_back(v), child[v].push_back(u);
}
dfs(1, 0);
build();
while (q--)
{
int op;
cin >> op;
if (op == 1)
{
int u, val;
cin >> u >> val;
Cover(1, s[u], e[u], 1LL << --val);
}
else
{
int u;
cin >> u;
ll ans = Query(1, s[u], e[u]);
cout << __popcount(ans) << endl;
}
}
}
signed int main()
{
// MyFile
Spider
//------------------------------------------------------
// clock_t start = clock();
int test = 1;
// read(test);
// cin >> test;
forn(i, 1, test)solve();
// while (cin >> n, n)solve();
// while (cin >> test)solve();
// clock_t end = clock();
// cerr << "time = " << double(end - start) / CLOCKS_PER_SEC << "s" << endl;
}
\[时间复杂度忽略掉\ popcount\ 的复杂度:O(t\log{n})
\]