题目大意:
就是现在给出一棵树, 两种操作
1.将u到v路径上的所有点的颜色更改为c
2.询问u到v路径上的点的颜色按经过顺序写成一排之后, 连续相同的数的段数
大致思路:
树链剖分练习第4题
没有什么难度...就是用线段树维护区间中不同连续颜色数量的时候, 记录当前区间最左边和最右边的颜色然后进行合并的时候怕暖两个区间将要合并的边界颜色是否一样就行
因为我使用的树链剖分模板是同一条链中靠近树叶子的点在线段树中区间靠前, 两个点向上爬的时候都是区间左端代表下边, 右端代表上边, 所以合并也是看两个区间左右是否一样
稍微要注意一点的就是两个点向上爬相遇的时候两边的链合并的情况
代码如下:
Result : Accepted Memory : 14660 KB Time : 4604 ms
/* * Author: Gatevin * Created Time: 2015/9/8 14:18:02 * File Name: Sakura_Chiyo.cpp */ #include<iostream> #include<sstream> #include<fstream> #include<vector> #include<list> #include<deque> #include<queue> #include<stack> #include<map> #include<set> #include<bitset> #include<algorithm> #include<cstdio> #include<cstdlib> #include<cstring> #include<cctype> #include<cmath> #include<ctime> #include<iomanip> using namespace std; const double eps(1e-8); typedef long long lint; #define maxn 100010 int top[maxn]; int grandson[maxn]; int dep[maxn]; int siz[maxn]; int belong[maxn]; int father[maxn]; int Q[maxn]; int cnt; int hson[maxn]; int n; struct Edge { int u, v, nex; Edge(int _u, int _v, int _nex) { u = _u, v = _v, nex = _nex; } Edge(){} }; Edge edge[maxn << 1]; int tot; int head[maxn]; void add_Edge(int x, int y) { edge[++tot] = Edge(x, y, head[x]); head[x] = tot; } bool vis[maxn]; int id[maxn]; int antiID[maxn]; void split() { cnt = 0; int l = 0, r = 1; dep[Q[r] = 1] = 1; father[r] = -1; while(l < r) { int x = Q[++l]; if(head[x] == -1) continue; for(int j = head[x]; j + 1; j = edge[j].nex) { int y = edge[j].v; if(y == father[x]) continue; dep[Q[++r] = y] = dep[x] + 1; father[y] = x; } } for(int i = n; i; i--) { int x = Q[i], p = -1; siz[x] = 1; if(head[x] == -1) continue; for(int j = head[x]; j + 1; j = edge[j].nex) { int y = edge[j].v; if(y == father[x]) continue; siz[x] += siz[y]; if(p == -1 || (p > 0 && siz[y] > siz[p])) p = y; } if(p == -1) { hson[x] = -1; grandson[++cnt] = x; belong[top[cnt] = x] = cnt; } else { hson[x] = p; belong[x] = belong[p]; top[belong[x]] = x; } } int idx = 0; memset(vis, 0, sizeof(vis)); for(int i = n; i; i--) { int x = Q[i]; if(vis[x]) continue; vis[x] = 1; id[x] = ++idx; antiID[idx] = x; while(father[x] != -1 && belong[father[x]] == belong[x] && !vis[father[x]]) { x = father[x]; id[x] = ++idx; antiID[idx] = x; vis[x] = 1; } } return; } int w[maxn]; struct State { int left, right, kind; State(int _l, int _r, int _k) { left = _l, right = _r, kind = _k; } State(){} }; struct Segment_Tree { #define lson l, mid, rt << 1 #define rson mid + 1, r, rt << 1 | 1 State s[maxn << 2]; int flag[maxn << 2];//懒惰标记 void pushUp(int rt) { s[rt] = State(s[rt << 1].left, s[rt << 1 | 1].right, s[rt << 1].kind + s[rt << 1 | 1].kind - (s[rt << 1].right == s[rt << 1 | 1].left)); return; } void pushDown(int rt) { if(flag[rt] != -1) { s[rt << 1] = s[rt << 1 | 1] = State(flag[rt], flag[rt], 1); flag[rt << 1] = flag[rt << 1 | 1] = flag[rt]; flag[rt] = -1; } return; } void build(int l, int r, int rt) { flag[rt] = -1; if(l == r) { s[rt] = State(w[antiID[l]], w[antiID[l]], 1); return; } int mid = (l + r) >> 1; build(lson); build(rson); pushUp(rt); return; } void update(int l, int r, int rt, int L, int R, int value) { if(l >= L && r <= R) { s[rt] = State(value, value, 1); flag[rt] = value; return; } int mid = (l + r) >> 1; pushDown(rt); if(mid >= L) update(lson, L, R, value); if(mid + 1 <= R) update(rson, L, R, value); pushUp(rt); return; } State query(int l, int r, int rt, int L, int R) { if(l >= L && r <= R) return s[rt]; int mid = (l + r) >> 1; pushDown(rt); State sl, sr; bool fl = 0, fr = 0; if(mid >= L) sl = query(lson, L, R), fl = 1; if(mid + 1 <= R) sr = query(rson, L, R), fr = 1; if(!fr) return sl; if(!fl) return sr; return State(sl.left, sr.right, sl.kind + sr.kind - (sl.right == sr.left)); } }; Segment_Tree st; void change(int x, int y, int col) { while(top[belong[x]] != top[belong[y]]) { if(dep[top[belong[x]]] < dep[top[belong[y]]]) swap(x, y); st.update(1, n, 1, id[x], id[top[belong[x]]], col); x = father[top[belong[x]]]; } if(dep[x] < dep[y]) swap(x, y); st.update(1, n, 1, id[x], id[y], col); return; } State add(State sl, State sr) { return State(sl.left, sr.right, sl.kind + sr.kind - (sl.right == sr.left)); } State answer(int l, int r) { int rev = 0; State L, R; bool firl = 1, firr = 1; while(top[belong[l]] != top[belong[r]]) { if(dep[top[belong[l]]] < dep[top[belong[r]]]) rev ^= 1, swap(l, r); if(rev == 1)//L和R与原来是反的 { if(firl) firl = 0, L = st.query(1, n, 1, id[l], id[top[belong[l]]]); else L = add(L, st.query(1, n, 1, id[l], id[top[belong[l]]])); } else { if(firr) firr = 0, R = st.query(1, n, 1, id[l], id[top[belong[l]]]); else R = add(R, st.query(1, n, 1, id[l], id[top[belong[l]]])); } l = father[top[belong[l]]]; } if(dep[l] < dep[r]) swap(l, r), rev ^= 1; if(rev == 1) { if(firl) firl = 0, L = st.query(1, n, 1, id[l], id[r]); else L = add(L, st.query(1, n, 1, id[l], id[r])); } else { if(firr) firr = 0, R = st.query(1, n, 1, id[l], id[r]); else R = add(R, st.query(1, n, 1, id[l], id[r])); } if(firl) return R; if(firr) return L; return State(L.left, R.left, L.kind + R.kind - (L.right == R.right)); } int main() { int m; while(scanf("%d %d", &n, &m) != EOF) { for(int i = 1; i <= n; i++) scanf("%d", w + i); memset(head, -1, sizeof(head)); int tot = 0; int u, v, col; for(int i = 1; i < n; i++) { scanf("%d %d", &u, &v); add_Edge(u, v); add_Edge(v, u); } split(); st.build(1, n, 1); char op[4]; while(m--) { scanf("%s", op); switch(op[0]) { case 'C': scanf("%d %d %d", &u, &v, &col); change(u, v, col); break; case 'Q': scanf("%d %d", &u, &v); printf("%d\n", answer(u, v).kind); break; } } } return 0; } /* 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 */