Bzoj2243-线段树

题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2243

Description
给定一棵有n个节点的无根树和m个操作,操作有2类:
1、将节点a到节点b路径上所有点都染成颜色c;
2、询问节点a到节点b路径上的颜色段数量(连续相同颜色被认为是同一段),
如“112221”由3段组成:“11”、“222”和“1”。
请你写一个程序依次完成这m个操作。

Input

第一行包含2个整数n和m,分别表示节点数和操作数;
第二行包含n个正整数表示n个节点的初始颜色
下面 行每行包含两个整数x和y,表示x和y之间有一条无向边。
下面 行每行描述一个操作:
“C a b c”表示这是一个染色操作,把节点a到节点b路径上所有点(包括a和b)都染成颜色c;
“Q a b”表示这是一个询问操作,询问节点a到节点b(包括a和b)路径上的颜色段数量。

Output
对于每个询问操作,输出一行答案。

Sample Input
6 5
2 2 1 2 1 1
1 2
1 3
2 4
2 5
2 6
Q 3 5
C 2 1 1
Q 3 5
C 5 1 2
Q 3 5

Sample Output
3
1
2

HINT
数N<=10^5,操作数M<=10^5,所有的颜色C为整数且在[0, 10^9]之间。

思路
线段树+树链剖分,在线段树需要每次用lt和rt两个数组记录当前区间的左右边界的颜色,向上更新时需要判断左区间的右边界是否和右区间的左边界相等。在剖分求LCA的过程中需要在求值之后查询与下一次求值的边界是否相等。


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
184
185
#include<cstdio>
#include<iostream>
#include<vector>
#include<cstdlib>
#include<map>
#include<queue>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<set>
#define lson l,mid,i<<1
#define rson mid+1,r,i<<1|1
using namespace std;
typedef long long ll;
const int maxn = 200005;
const int INF = 2e9;
struct node {
int s, e, next;
}edge[maxn * 2];
int n, m;
int son[maxn], top[maxn], tid[maxn], fat[maxn], siz[maxn], dep[maxn], rak[maxn];
int head[maxn], len, dfx;
//siz保存以i为根的子树节点个数,top保存i节点所在链的顶端节点,son保存i节点的重儿子,fat保存i节点的父亲节点\
dep保存i节点的深度(根为1),,tid保存i节点dfs后的新编号,rak保存新编号i对应的节点(rak[i]=j,tid[j]=i)。
void init() {
memset(head, -1, sizeof(head));
len = 0, dfx = 0;
}
void add(int s, int e) {//邻接表存值
edge[len].s = s;
edge[len].e = e;
edge[len].next = head[s];
head[s] = len++;
}
//搜出每个节点的siz,son,fat,dep
void dfs1(int x, int fa, int d) {
siz[x] = 1, son[x] = -1, fat[x] = fa, dep[x] = d;
for (int i = head[x]; i != -1; i = edge[i].next) {
int y = edge[i].e;
if (y == fa)
continue;
dfs1(y, x, d + 1);
siz[x] += siz[y];
if (son[x] == -1 || siz[y] > siz[son[x]])
son[x] = y;
}
}
//搜出每个节点的top,tid,rak
void dfs2(int x, int c) {
top[x] = c;
tid[x] = ++dfx;
rak[dfx] = x;
if (son[x] == -1)
return;
dfs2(son[x], c);
for (int i = head[x]; i != -1; i = edge[i].next) {
int y = edge[i].e;
if (y == fat[x] || y == son[x])
continue;
dfs2(y, y);
}
}
int a[maxn];
int cr[maxn * 2];
int rt[maxn * 2];
int lt[maxn * 2];
int lazy[maxn * 2];
void up(int i) {
lt[i] = lt[i << 1], rt[i] = rt[i << 1 | 1];
cr[i] = cr[i << 1] + cr[i << 1 | 1];
if (lt[i << 1 | 1] == rt[i << 1])
cr[i]--;
}
void down(int i) {
if (lazy[i] != -1) {
lt[i << 1] = lt[i << 1 | 1] = rt[i << 1] = rt[i << 1 | 1] = lazy[i];
cr[i << 1] = cr[i << 1 | 1] = cr[i];
lazy[i << 1] = lazy[i << 1 | 1] = lazy[i];
lazy[i] = -1;
}
}
void build(int l, int r, int i) {
lazy[i] = -1;
if (l == r) {
cr[i] = 1;
rt[i] = a[rak[l]];
lt[i] = a[rak[l]];
return;
}
int mid = (l + r) >> 1;
build(lson);
build(rson);
up(i);
}
void update(int L, int R, int k, int l, int r, int i) {
if (L <= l && r <= R) {
cr[i] = 1;
lazy[i] = k;
lt[i] = rt[i] = k;
return;
}
down(i);
int mid = (l + r) >> 1;
if (L <= mid)
update(L, R, k, lson);
if (R > mid)
update(L, R, k, rson);
up(i);
}
int qquery(int L, int R, int l, int r, int i) {
if (L <= l && r <= R) return cr[i];
down(i);
int mid = (l + r) >> 1;
if (R <= mid) return qquery(L, R, lson);
if (L > mid) return qquery(L, R, rson);
int ans1 = qquery(L, R, lson);
int ans2 = qquery(L, R, rson);
int ans = ans1 + ans2;
if (rt[i << 1] == lt[i << 1 | 1]) ans--;
return ans;
}

int dquery(int k, int l, int r, int i) {
if (l == r) {
return lt[i];
}
int mid = (l + r) / 2;
down(i);
if (k <= mid)
return dquery(k, lson);
else
return dquery(k, rson);
}
int solve(int x, int y, int w, int flg) {
int ans = 0;
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]])
swap(x, y);
if (!flg)
update(tid[top[x]], tid[x], w, 1, n, 1);
else {
ans += qquery(tid[top[x]], tid[x], 1, n, 1);
if (dquery(tid[top[x]], 1, n, 1) == dquery(tid[fat[top[x]]], 1, n, 1))
ans--;
}
x = fat[top[x]];
}
if (dep[x] < dep[y])
swap(x, y);
if (!flg)
update(tid[y], tid[x], w, 1, n, 1);
else {
ans += qquery(tid[y], tid[x], 1, n, 1);
return ans;
}
}
int main() {
while (scanf("%d%d", &n, &m) != EOF) {
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
init();
int x, y, z;
for (int i = 0; i < n - 1; i++) {
scanf("%d%d", &x, &y);
add(x, y);
add(y, x);
}
dfs1(1, 0, 1);
dfs2(1, 1);
build(1, n, 1);
while (m--) {
char s[10];
scanf("%s", s);
if (s[0] == 'C') {
scanf("%d%d%d", &x, &y, &z);
solve(x, y, z, 0);
}
else {
scanf("%d%d", &x, &y);
printf("%d\n", solve(x, y, 0, 1));
}
}
}
}

提供一组数据

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

/*
9 5
1 1 1 2 3 3 2 1 3
1 2
1 3
1 4
2 5
2 6
2 7
3 8
3 9
Q 2 4
C 2 6 4
Q 1 6
Q 6 4
Q 1 9
*/
//答案
/*
2
2
3
2
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
博客
32132
07-14 324
07-12 260
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值