HYSBZ 2243 染色 (线段树+树链剖分)
Posted dwtfukgv
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HYSBZ 2243 染色 (线段树+树链剖分)相关的知识,希望对你有一定的参考价值。
题意:中文题。
析:真是一个好题,但是我TLE了两天,就是因为输入那个询问数,我当作边数了,结果就是一个TLE。。。
大体思路,就是先进行用树链剖分,然后用线段树来维护,维护每个区间的不同数的个数,和每个数的值,在求的时候,在两个端点进行判断,是不是同一种,如果是就减去1,不是则不变。
而且发现一个问题,就是网上的代码所以输出的和我的不一样,但是都AC了,不知道是不是数据水。也不知道谁的对。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include <queue> #include <algorithm> #include <vector> #include <map> #include <cctype> #include <cmath> #include <stack> #include <sstream> #include <list> #include <assert.h> #include <bitset> #define debug() puts("++++"); #define gcd(a, b) __gcd(a, b) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define fi first #define se second #define pb push_back #define sqr(x) ((x)*(x)) #define ms(a,b) memset(a, b, sizeof a) #define sz size() #define pu push_up #define pd push_down #define cl clear() #define all 1,n,1 #define FOR(x,n) for(int i = (x); i < (n); ++i) #define freopenr freopen("in.txt", "r", stdin) #define freopenw freopen("out.txt", "w", stdout) using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair<int, int> P; const int INF = 0x3f3f3f3f; const double inf = 1e20; const double PI = acos(-1.0); const double eps = 1e-8; const int maxn = 1e5 + 50; const int mod = 1000; const int dr[] = {-1, 0, 1, 0}; const int dc[] = {0, 1, 0, -1}; const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"}; int n, m; const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; inline bool is_in(int r, int c) { return r > 0 && r <= n && c > 0 && c <= m; } struct Edge{ int to, next; }; Edge edge[maxn<<1]; int head[maxn], tot, top[maxn], dep[maxn], a[maxn], fp[maxn]; int son[maxn], fa[maxn], num[maxn], p[maxn], pos; int sum[maxn<<2], setv[maxn<<2], lx[maxn<<2], rx[maxn<<2], value[maxn<<2]; void init(){ tot = pos = 0; ms(head, -1); ms(son, -1); ms(setv, -1); } void addEdge(int u, int v){ edge[tot].to = v; edge[tot].next = head[u]; head[u] = tot++; } void dfs1(int u, int f, int d){ dep[u] = d; fa[u] = f; num[u] = 1; for(int i = head[u]; ~i; i = edge[i].next){ int v = edge[i].to; if(v == f) continue; dfs1(v, u, d+1); num[u] += num[v]; if(son[u] == -1 || num[son[u]] < num[v]) son[u] = v; } } void dfs2(int u, int sp){ top[u] = sp; p[u] = ++pos; fp[pos] = u; if(son[u] == -1) return ; dfs2(son[u], sp); for(int i = head[u]; ~i; i = edge[i].next){ int v = edge[i].to; if(v == son[u] || v == fa[u]) continue; dfs2(v, v); } } void push_up(int rt){ int l = rt<<1, r = rt<<1|1; sum[rt] = sum[l] + sum[r]; if(rx[l] == lx[r]) --sum[rt]; lx[rt] = lx[l]; rx[rt] = rx[r]; } void build(int l, int r, int rt){ if(l == r){ value[rt] = a[fp[l]]; sum[rt] = 1; lx[rt] = rx[rt] = value[rt]; return ; } int m = l + r >> 1; build(lson); build(rson); pu(rt); } void push_down(int rt){ if(setv[rt] < 0) return ; int l = rt<<1, r = rt<<1|1; sum[l] = sum[r] = 1; value[l] = value[r] = setv[rt]; lx[l] = rx[l] = lx[r] = rx[r] = setv[rt]; setv[l] = setv[r] = setv[rt]; setv[rt] = -1; } void update(int L, int R, int val, int l, int r, int rt){ if(L <= l && r <= R){ sum[rt] = 1; value[rt] = setv[rt] = val; lx[rt] = rx[rt] = val; return ; } pd(rt); int m = l + r >> 1; if(L <= m) update(L, R, val, lson); if(R > m) update(L, R, val, rson); pu(rt); } int queryVal(int M, int l, int r, int rt){ if(l == r) return value[rt]; pd(rt); int m = l + r >> 1; if(M <= m) return queryVal(M, lson); return queryVal(M, rson); } int query(int L, int R, int l, int r, int rt){ if(L <= l && r <= R) return sum[rt]; pd(rt); int m = l + r >> 1; if(L <= m && R > m){ int ans = query(L, R, lson) + query(L, R, rson); if(rx[rt<<1] == lx[rt<<1|1]) --ans; return ans; } else if(L <= m) return query(L, R, lson); else return query(L, R, rson); } void update(int u, int v, int c){ int f1 = top[u], f2 = top[v]; while(f1 != f2){ if(dep[f1] < dep[f2]){ swap(f1, f2); swap(u, v); } update(p[f1], p[u], c, all); u = fa[f1]; f1 = top[u]; } if(dep[u] > dep[v]) swap(u, v); update(p[u], p[v], c, all); } int query(int u, int v){ int f1 = top[u], f2 = top[v]; int ans = 0; while(f1 != f2){ if(dep[f1] < dep[f2]){ swap(u, v); swap(f1, f2); } ans += query(p[f1], p[u], all); if(queryVal(p[f1], all) == queryVal(p[fa[f1]], all)) --ans; u = fa[f1]; f1 = top[u]; } if(dep[u] > dep[v]) swap(u, v); ans += query(p[u], p[v], all); return ans; } int main(){ scanf("%d %d", &n, &m); init(); for(int i = 1; i <= n; ++i) scanf("%d", a+i); for(int i = 1; i < n; ++i){ int u, v; scanf("%d %d", &u, &v); addEdge(u, v); addEdge(v, u); } dfs1(1, 0, 0); dfs2(1, 1); build(all); char op[5]; while(m--){ scanf("%s", op); int a, b, c; scanf("%d %d", &a, &b); if(op[0] == ‘C‘){ scanf("%d", &c); update(a, b, c); } else printf("%d\n", query(a, b)); } return 0; }
以上是关于HYSBZ 2243 染色 (线段树+树链剖分)的主要内容,如果未能解决你的问题,请参考以下文章
HYSBZ - 2243 树链剖分 + 线段树 处理树上颜色段数
bzoj2243: [SDOI2011]染色--线段树+树链剖分