4129: Haruna’s Breakfast
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 835 Solved: 409
[Submit][Status][Discuss]
Description
Haruna每天都会给提督做早餐! 这天她发现早饭的食材被调皮的 Shimakaze放到了一棵
树上,每个结点都有一样食材,Shimakaze要考验一下她。
每个食材都有一个美味度,Shimakaze会进行两种操作:
1、修改某个结点的食材的美味度。
2、对于某条链,询问这条链的美味度集合中,最小的未出现的自然数是多少。即mex值。
请你帮帮Haruna吧。
Input
第一行包括两个整数n,m,代表树上的结点数(标号为1~n)和操作数。
第二行包括n个整数a1...an,代表每个结点的食材初始的美味度。
接下来n-1行,每行包括两个整数u,v,代表树上的一条边。
接下来m 行,每行包括三个整数
0 u x 代表将结点u的食材的美味度修改为 x。
1 u v 代表询问以u,v 为端点的链的mex值。
Output
对于每次询问,输出该链的mex值。
Sample Input
10 10
1 0 1 0 2 4 4 0 1 0
1 2
2 3
2 4
2 5
1 6
6 7
2 8
3 9
9 10
0 7 14
1 6 6
0 4 9
1 2 2
1 1 8
1 8 3
0 10 9
1 3 5
0 10 0
0 7 7
1 0 1 0 2 4 4 0 1 0
1 2
2 3
2 4
2 5
1 6
6 7
2 8
3 9
9 10
0 7 14
1 6 6
0 4 9
1 2 2
1 1 8
1 8 3
0 10 9
1 3 5
0 10 0
0 7 7
Sample Output
0
1
2
2
3
1
2
2
3
HINT
1<=n<=5*10^4
1<=m<=5*10^4
0<=ai<=10^9
Source
析:树上莫队,还是带修改的,和普通的带修莫队差不多,也就是在树上,对树上分块,然后对于mex,进行分块,因为最多出现n-1个整数也就是0-n-1,其他的都可以忽略。
代码如下:
#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(i,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-4; const int maxn = 5e4 + 10; const int maxm = 1e6 + 10; const int mod = 1e9 + 7; 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; } int SIZE, b, u, v; int pos[maxn], a[maxn], t[maxn]; struct Edge{ int to, next; }; Edge edges[maxn<<1]; int head[maxn], cnt; int p[20][maxn]; int dep[maxn], st[maxn], top; struct Query{ int id, u, v, tim; bool operator < (const Query &q) const{ if(pos[u] != pos[q.u]) return pos[u] < pos[q.u]; if(pos[v] != pos[q.v]) return pos[v] < pos[q.v]; return tim < q.tim; } }; struct Update{ int pos, now, last; }; Query q[maxn]; Update pp[maxn]; int cur; void addEdge(int u, int v){ edges[cnt].to = v; edges[cnt].next = head[u]; head[u] = cnt++; } void dfs(int u, int fa, int d){ p[0][u] = fa; dep[u] = d; int bot = top; for(int i = head[u]; ~i; i = edges[i].next){ int v = edges[i].to; if(v == fa) continue; dfs(v, u, d + 1); if(top - bot >= SIZE){ ++b; while(top != bot) pos[st[top--]] = b; } } st[++top] = u; } void init(){ dfs(1, 0, 0); for(int k = 0; k < 18; ++k) for(int v = 1; v <= n; ++v) p[k+1][v] = p[k][v] == 0 ? 0 : p[k][p[k][v]]; } inline int LCA(int u, int v){ if(dep[u] > dep[v]) swap(u, v); for(int k = 0; k < 18; ++k) if(dep[v] - dep[u] >> k & 1) v = p[k][v]; if(u == v) return u; for(int k = 17; k >= 0; --k) if(p[k][u] != p[k][v]){ u = p[k][u]; v = p[k][v]; } return p[0][u]; } struct Block{ int n, pos[maxn], SIZE; int cnt[maxn], sum[maxn/225+10]; void init(){ SIZE = 225; for(int i = 0; i <= n; ++i) pos[i] = i / SIZE; ms(sum, 0); ms(cnt, 0); } inline void add(int x){ if(x < n) sum[pos[x]] += ++cnt[x] == 1; } inline void del(int x){ if(x < n) sum[pos[x]] -= --cnt[x] == 0; } inline int mex(){ for(int i = 0; ; ++i) if(sum[i] != SIZE){ for(int j = i*SIZE; ; ++j) if(!cnt[j]) return j; } } }; Block B; bool vis[maxn]; int ans[maxn]; inline void update(int pos, int now){ if(vis[pos]) B.del(a[pos]), B.add(now); a[pos] = now; } inline void Xor(int u){ if(vis[u]) B.del(a[u]), vis[u] = 0; else B.add(a[u]), vis[u] = 1; } inline void mov(int u, int v){ if(dep[u] > dep[v]) swap(u, v); while(dep[u] < dep[v]) Xor(v), v = p[0][v]; while(u != v) Xor(u), Xor(v), u = p[0][u], v = p[0][v]; } int main(){ scanf("%d %d", &n, &m); ms(head, -1); for(int i = 1 ; i <= n; ++i){ scanf("%d", a + i); t[i] = a[i]; } SIZE = pow(n, 0.45); for(int i = 1; i < n; ++i){ int u, v; scanf("%d %d", &u, &v); addEdge(u, v); addEdge(v, u); } init(); while(top) pos[st[top--]] = b; int qcnt = 0, time = 0; for(int i = 0; i < m; ++i){ int op, l, r; scanf("%d %d %d", &op, &l, &r); if(op) q[++qcnt] = (Query){qcnt, l, r, time}; else pp[++time] = (Update){l, r, t[l]}, t[l] = r; } sort(q + 1, q + qcnt + 1); B.n = n; B.init(); u = v = 1; for(int i = 1; i <= qcnt; ++i){ while(cur < q[i].tim) ++cur, update(pp[cur].pos, pp[cur].now); while(cur > q[i].tim) update(pp[cur].pos, pp[cur].last), --cur; while(u != q[i].u) mov(u, q[i].u), u = q[i].u; while(v != q[i].v) mov(v, q[i].v), v = q[i].v; int lca = LCA(u, v); Xor(lca); ans[q[i].id] = B.mex(); Xor(lca); } for(int i = 1; i <= qcnt; ++i) printf("%d\n", ans[i]); return 0; }