• bzoj1036 [ZJOI2008]树的统计Count


    题目

      bzoj1036

    代码

    #include <bits/stdc++.h>
    using namespace std;
    const int N = 100005;
    const int inf = 1e9;
    
    inline int read()
    {
    	int x = 0, f = 1; char ch = getchar();
    	while (ch < '0' || ch > '9') {if (ch == '-') f = -1; ch = getchar();}
    	while (ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
    	return x * f;
    }
    
    int n, Q, w[N];
    vector<int> to[N];
    
    int fa[N], deep[N], sz[N], son[N];
    void dfs1(int x)
    {
    	sz[x] = 1;
    	for(int i = 0; i < to[x].size(); i++)
    	{
    		int k = to[x][i];
    		if(k == fa[x]) continue;
    		fa[k] = x; deep[k] = deep[x] + 1;
    		dfs1(k); sz[x] += sz[k];
    		if(sz[k] > sz[son[x]]) son[x] = k;
    	}
    }
    
    int tot, id[N], dfn[N], top[N];
    void dfs2(int x, int tp)
    {
    	dfn[x] = ++tot; id[tot] = x; top[x] = tp;
    	if(son[x]) dfs2(son[x], tp);
    	for(int i = 0; i < to[x].size(); i++)
    	{
    		int k = to[x][i];
    		if(k != fa[x] && k != son[x]) dfs2(k, k);		
    	}
    }
    
    struct node
    {
    	int l, r, sum, mx;
    }T[N << 3];
    
    void pushup(int p)
    {
    	T[p].sum = T[p << 1].sum + T[p << 1 | 1].sum;
    	T[p].mx = max(T[p << 1].mx, T[p << 1 | 1].mx);
    }
    
    void build(int p, int x, int y)
    {
    	T[p].l = x; T[p].r = y;
    	if(x == y) {T[p].sum = T[p].mx = w[id[x]]; return;}
    	int mid = (x + y) >> 1;
    	build(p << 1, x, mid); build(p << 1 | 1, mid + 1, y);
    	pushup(p);
    }
    
    void change(int p, int x, int v)
    {
    	int pl = T[p].l, pr = T[p].r;
    	if(pl == x && pr == x)
    	{
    		T[p].sum = T[p].mx = v;
    		return;
    	}
    	int mid = (pl + pr) >> 1;
    	if(x <= mid) change(p << 1, x, v);
    	else change(p << 1 | 1, x, v);
    	pushup(p);
    }
    
    int query_max(int p, int x, int y)
    {
    	int pl = T[p].l, pr = T[p].r;
    	if(pl == x && pr == y) return T[p].mx;
    	int mid = (pl + pr) >> 1;
    	if(y <= mid) return query_max(p << 1, x, y);
    	else if(x > mid) return query_max(p << 1 | 1, x, y);
    	else return max(query_max(p << 1, x, mid), query_max(p << 1 | 1, mid + 1, y));
    }
    
    int query_sum(int p, int x, int y)
    {
    	int pl = T[p].l, pr = T[p].r;
    	if(pl == x && pr == y) return T[p].sum;
    	int mid = (pl + pr) >> 1;
    	if(y <= mid) return query_sum(p << 1, x, y);
    	else if(x > mid) return query_sum(p << 1 | 1, x, y); 
    	return query_sum(p << 1, x, mid) + query_sum(p << 1 | 1, mid + 1, y);
    }
    
    int QMAX(int x, int y)
    {
    	int cnt = -inf;
    	while(top[x] != top[y])
    	{
    		if(deep[top[x]] < deep[top[y]]) swap(x, y);
    		cnt = max(cnt, query_max(1, dfn[top[x]], dfn[x]));
    		x = fa[top[x]];
    	}
    	if(deep[x] < deep[y]) swap(x, y);
    	cnt = max(cnt, query_max(1, dfn[y], dfn[x]));
    	return cnt;
    }
    
    int QSUM(int x, int y)
    {
    	int cnt = 0;
    	while(top[x] != top[y])
    	{
    		if(deep[top[x]] < deep[top[y]]) swap(x, y);
    		cnt += query_sum(1, dfn[top[x]], dfn[x]);
    		x = fa[top[x]];
    	}
    	if(deep[x] < deep[y]) swap(x, y);
    	cnt += query_sum(1, dfn[y], dfn[x]);
    	return cnt;
    }
    
    int main()
    {	
    	n = read();
    	for(int i = 1; i < n; i++)
    	{
    		int x = read(), y = read();
    		to[x].push_back(y); to[y].push_back(x);
    	}
    	for(int i = 1; i <= n; i++) w[i] = read();
    	dfs1(1);
    	dfs2(1, 0);
    	build(1, 1, n);
    	Q = read();
    	while(Q--)
    	{
    		char op[10]; scanf("%s", op);
    		int x = read(), y = read();
    		if(op[0] == 'C') change(1, dfn[x], y);
    		else if(op[1] == 'M') printf("%d
    ", QMAX(x, y));
    		else printf("%d
    ", QSUM(x, y));
    	}
    	return 0;
    }
    
  • 相关阅读:
    indexOf & lastIndexOf
    document.referrer
    函数组合
    v-model绑定vuex中的数据
    图片懒加载
    flex布局问题
    js常见错误类型
    es5/es6继承的区别
    合并两个有序数组
    lettcode 90 子集II
  • 原文地址:https://www.cnblogs.com/XYZinc/p/9187028.html
Copyright © 2020-2023  润新知