【bzoj3531】 [SDOI2014]旅行 树链剖分+动态开点线段树

题目描述 

S国有N个城市,编号从1到N。城市间用N-1条双向道路连接,满足从一个城市出发可以到达其它所有城市。每个城市信仰不同的宗教,如飞天面条神教、隐形独角兽教、绝地教都是常见的信仰。为了方便,我们用不同的正整数代表各种宗教,  S国的居民常常旅行。旅行时他们总会走最短路,并且为了避免麻烦,只在信仰和他们相同的城市留宿。当然旅程的终点也是信仰与他相同的城市。S国政府为每个城市标定了不同的旅行评级,旅行者们常会记下途中(包括起点和终点)留宿过的城市的评级总和或最大值。

在S国的历史上常会发生以下几种事件:
”CC x c”:城市x的居民全体改信了c教;
”CW x w”:城市x的评级调整为w;
”QS x y”:一位旅行者从城市x出发,到城市y,并记下了途中留宿过的城市的评级总和;
”QM x y”:一位旅行者从城市x出发,到城市y,并记下了途中留宿过
的城市的评级最大值。
由于年代久远,旅行者记下的数字已经遗失了,但记录开始之前每座城市的信仰与评级,还有事件记录本身是完好的。请根据这些信息,还原旅行者记下的数字。    为了方便,我们认为事件之间的间隔足够长,以致在任意一次旅行中,所有城市的评级和信仰保持不变。

输入

输入的第一行包含整数N,Q依次表示城市数和事件数。接下来N行,第i+l行两个整数Wi,Ci依次表示记录开始之前,城市i的

评级和信仰。
接下来N-1行每行两个整数x,y表示一条双向道路。
接下来Q行,每行一个操作,格式如上所述。

输出

对每个QS和QM事件,输出一行,表示旅行者记下的数字。

样例输入

5 6
3 1
2 3
1 2
3 3
5 1
1 2
1 3
3 4
3 5
QS 1 5
CC 3 1
QS 1 5
CW 3 3
QS 1 5
QM 2 4

样例输出

8
9
11
3

提示

N,Q < =10^5    , C < =10^5

数据保证对所有QS和QM事件,起点和终点城市的信仰相同;

在任意时刻,城市的评级总是不大于10^4的正整数,且宗教值不大于C。


题解

树链剖分+动态开点线段树

对每种信仰建立一个线段树,分别处理。

第一次知道线段树还可以动态开点。

一般的线段树的左节点都是x<<1,右节点都是x<<1|1,

这样无疑浪费了很多空间,会mle。

所以采用动态开点,每使用到一个节点就开一个节点,减少了浪费。

当然由于线段树不能删点,所以程序很慢,但2s的时间绰绰有余。

注意空间要开得大一点。

#include <stdio.h>
#include <algorithm>
#define N 3000001
#define lson l , mid , lp[x]
#define rson mid + 1 , r , rp[x]
using namespace std;
int fa[N] , deep[N] , bl[N] , si[N] , root[N] , pos[N] , tot , w[N] , c[N];
int head[N] , to[N << 1] , next[N << 1] , cnt;
int sum[N << 2] , maxn[N << 2] , lp[N << 2] , rp[N << 2] , n , sizen;
char str[10];
void add(int x , int y)
{
    to[++cnt] = y;
    next[cnt] = head[x];
    head[x] = cnt;
}
void dfs1(int x)
{
    int y , i;
    si[x] = 1;
    for(i = head[x] ; i ; i = next[i])
    {
        y = to[i];
        if(y != fa[x])
        {
            fa[y] = x;
            deep[y] = deep[x] + 1;
            dfs1(y);
            si[x] += si[y];
        }
    }
}
void dfs2(int x , int c)
{
    int y , i , k = 0;
    bl[x] = c;
    pos[x] = ++tot;
    for(i = head[x] ; i ; i = next[i])
    {
        y = to[i];
        if(y != fa[x] && si[y] > si[k])
            k = y;
    }
    if(k)
    {
        dfs2(k , c);
        for(i = head[x] ; i ; i = next[i])
        {
            y = to[i];
            if(y != fa[x] && y != k)
                dfs2(y , y);
        }
    }
}
void pushup(int x)
{
    sum[x] = sum[lp[x]] + sum[rp[x]];
    maxn[x] = max(maxn[lp[x]] , maxn[rp[x]]);
}
void update(int p , int v , int l , int r , int &x)
{
    if(!x)
        x = ++sizen;
    if(l == r)
    {
        sum[x] = maxn[x] = v;
        return;
    }
    int mid = (l + r) >> 1;
    if(p <= mid)
        update(p , v , lson);
    else
        update(p , v , rson);
    pushup(x);
}
int querysum(int b , int e , int l , int r, int x)
{
    if(b <= l && r <= e)
        return sum[x];
    int mid = (l + r) >> 1 , ans = 0;
    if(b <= mid)
        ans += querysum(b , e , lson);
    if(e > mid)
        ans += querysum(b , e , rson);
    return ans;
}
int querymax(int b , int e , int l , int r , int x)
{
    if(b <= l && r <= e)
        return maxn[x];
    int mid = (l + r) >> 1 , ans = 0;
    if(b <= mid)
        ans = max(ans , querymax(b , e , lson));
    if(e > mid)
        ans = max(ans , querymax(b , e , rson));
    return ans;
}
int solvesum(int x , int f , int c)
{
    int ans = 0;
    while(bl[x] != bl[f])
    {
        ans += querysum(pos[bl[x]] , pos[x] , 1 , n , root[c]);
        x = fa[bl[x]];
    }
    ans += querysum(pos[f] , pos[x] , 1 , n , root[c]);
    return ans;
}
int solvemax(int x , int f , int c)
{
    int ans = 0;
    while(bl[x] != bl[f])
    {
        ans = max(ans , querymax(pos[bl[x]] , pos[x] , 1 , n , root[c]));
        x = fa[bl[x]];
    }
    ans = max(ans , querymax(pos[f] , pos[x] , 1 , n , root[c]));
    return ans;
}
int lca(int x , int y)
{
    while(bl[x] != bl[y])
    {
        if(deep[bl[x]] < deep[bl[y]])
            swap(x , y);
        x = fa[bl[x]];
    }
    if(deep[x] < deep[y])
        return x;
    return y;
}
int main()
{
    int q , i , x , y , f;
    scanf("%d%d" , &n , &q);
    for(i = 1 ; i <= n ; i ++ )
        scanf("%d%d" , &w[i] , &c[i]);
    for(i = 1 ; i < n ; i ++ )
    {
        scanf("%d%d" , &x , &y);
        add(x , y);
        add(y , x);
    }
    dfs1(1);
    dfs2(1 , 1);
    for(i = 1 ; i <= n ; i ++ )
        update(pos[i] , w[i] , 1 , n , root[c[i]]);
    while(q -- )
    {
        scanf("%s%d%d" , str , &x , &y);
        switch(str[1])
        {
            case 'C': update(pos[x] , 0 , 1 , n , root[c[x]]); c[x] = y; update(pos[x] , w[x] , 1 , n , root[c[x]]); break;
            case 'W': w[x] = y; update(pos[x] , w[x] , 1 , n , root[c[x]]); break;
            case 'S': f = lca(x , y); printf("%d\n" , solvesum(x , f , c[x]) + solvesum(y , f , c[y]) - querysum(pos[f] , pos[f] , 1 , n , root[c[x]])); break;
            default: f = lca(x , y); printf("%d\n" , max(solvemax(x , f , c[x]) , solvemax(y , f , c[y])));
        }
    }
    return 0;
}

转载于:https://www.cnblogs.com/GXZlegend/p/6189247.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值