[luoguP2486] [SDOI2011]染色(树链剖分)

传送门

 

就是个模板啦

 

记录每一个点的左端点颜色和右端点颜色和当前端点颜色段数。

合并时如果左孩子右端点和右孩子左端点不同就 ans--

在重链上跳的时候别忘记统计一下

 

——代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include <cstdio>
#include <cstring>
#include <iostream>
#define N 400001
#define ls now << 1
#define rs now << 1 | 1
#define swap(x, y) ((x) ^= (y) ^= (x) ^= (y))
#define pushup(now) Lcol[now] = Lcol[ls], Rcol[now] = Rcol[rs], col[now] = col[ls] + col[rs] - (Rcol[ls] == Lcol[rs])
 
int n, m, cnt, tim;
int head[N], to[N << 1], next[N << 1];
int a[N], f[N], deep[N], size[N], son[N], tid[N], top[N], rank[N], L[N], R[N], Lcol[N], Rcol[N], col[N], laz[N];
 
inline int read()
{
    int x = 0, f = 1;
    char ch = getchar();
    for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -1;
    for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - '0';
    return x * f;
}
 
inline void add(int x, int y)
{
    to[cnt] = y;
    next[cnt] = head[x];
    head[x] = cnt++;
}
 
inline void dfs1(int u)
{
    int i, v;
    size[u] = 1;
    deep[u] = deep[f[u]] + 1;
    for(i = head[u]; i ^ -1; i = next[i])
    {
        v = to[i];
        if(v ^ f[u])
        {
            f[v] = u;
            dfs1(v);
            size[u] += size[v];
            if(son[u] == -1 || size[son[u]] < size[v]) son[u] = v;
        }
    }
}
 
inline void dfs2(int u, int t)
{
    int i, v;
    top[u] = t;
    tid[u] = ++tim;
    rank[tim] = u;
    if(son[u] == -1) return;
    dfs2(son[u], t);
    for(i = head[u]; i ^ -1; i = next[i])
    {
        v = to[i];
        if(v ^ f[u] && v ^ son[u]) dfs2(v, v);
    }
}
 
inline void build(int now, int l, int r)
{
    L[now] = l;
    R[now] = r;
    if(l == r)
    {
        col[now] = 1;
        Lcol[now] = Rcol[now] = a[rank[l]];
        return;
    }
    int mid = (l + r) >> 1;
    build(ls, l, mid);
    build(rs, mid + 1, r);
    pushup(now);
}
 
inline void pushdown(int now)
{
    if(laz[now])
    {
        col[ls] = col[rs] = 1;
        laz[ls] = laz[rs] = Lcol[ls] = Lcol[rs] = Rcol[ls] = Rcol[rs] = laz[now];
        laz[now] = 0;
    }
}
 
inline void update(int now, int l, int r, int z)
{
    if(l <= L[now] && R[now] <= r)
    {
        col[now] = 1;
        laz[now] = Lcol[now] = Rcol[now] = z;
        return;
    }
    pushdown(now);
    int mid = (L[now] + R[now]) >> 1;
    if(l <= mid) update(ls, l, r, z);
    if(mid < r) update(rs, l, r, z);
    pushup(now);
}
 
inline void q_update(int u, int v, int z)
{
    while(top[u] ^ top[v])
    {
        if(deep[top[u]] < deep[top[v]]) swap(u, v);
        update(1, tid[top[u]], tid[u], z);
        u = f[top[u]];
    }
    if(deep[u] > deep[v]) swap(u, v);
    update(1, tid[u], tid[v], z);
}
 
inline int q_color(int now, int x)
{
    if(L[now] == R[now]) return Lcol[now];
    pushdown(now);
    int mid = (L[now] + R[now]) >> 1;
    if(x <= mid) return q_color(ls, x);
    else return q_color(rs, x);
}
 
inline int query(int now, int l, int r)
{
    if(l <= L[now] && R[now] <= r) return col[now];
    pushdown(now);
    int mid = (L[now] + R[now]) >> 1, ans = 0;
    if(l <= mid) ans += query(ls, l, r);
    if(mid < r) ans += query(rs, l, r);
    if(l <= mid && mid < r) ans -= (Rcol[ls] == Lcol[rs]);
    return ans;
}
 
inline int q_query(int u, int v)
{
    int ans = 0;
    while(top[u] ^ top[v])
    {
        if(deep[top[u]] < deep[top[v]]) swap(u, v);
        ans += query(1, tid[top[u]], tid[u]);
        ans -= (q_color(1, tid[f[top[u]]]) == q_color(1, tid[top[u]]));
        u = f[top[u]];
    }
    if(deep[u] > deep[v]) swap(u, v);
    ans += query(1, tid[u], tid[v]);
    return ans;
}
 
int main()
{
    int i, x, y, z;
    char s[10];
    n = read();
    m = read();
    memset(son, -1, sizeof(son));
    memset(head, -1, sizeof(head));
    for(i = 1; i <= n; i++) a[i] = read();
    for(i = 1; i < n; i++)
    {
        x = read();
        y = read();
        add(x, y);
        add(y, x);
    }
    dfs1(1);
    dfs2(1, 1);
    build(1, 1, n);
    for(i = 1; i <= m; i++)
    {
        scanf("%s", s);
        x = read();
        y = read();
        if(s[0] == 'C')
        {
            z = read();
            q_update(x, y, z);
        }
        else printf("%d\n", q_query(x, y));
    }
    return 0;
}

  

posted @   zht467  阅读(96)  评论(0编辑  收藏  举报
编辑推荐:
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
阅读排行:
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· 程序员常用高效实用工具推荐,办公效率提升利器!
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 【译】WinForms:分析一下(我用 Visual Basic 写的)
点击右上角即可分享
微信分享提示