传送门

题意:给一颗树,每个节点有个初始值,要求支持将i节点的值改为x或询问i节点到j节点的路径上有多少个值为x的节点。


思路:

考虑对每种颜色动态开点,然后用树剖+线段树维护就完了。

代码:

#include<bits/stdc++.h>
#define ri register int
using namespace std;
inline int read(){
    int ans=0;
    char ch=getchar();
    while(!isdigit(ch))ch=getchar();
    while(isdigit(ch))ans=(ans<<3)+(ans<<1)+(ch^48),ch=getchar();
    return ans;
}
const int N=1e5+5,M=2e5+5;
int n,m,id=0,tot=0,siz[N],num[N],col[N],rt[N+M],fa[N],dep[N],hson[N],top[N];
vector<int>e[N];
map<int,int>S;
void dfs1(int p){
    siz[p]=1;
    for(ri i=0,v;i<e[p].size();++i){
        if((v=e[p][i])==fa[p])continue;
        fa[v]=p,dep[v]=dep[p]+1,dfs1(v),siz[p]+=siz[v];
        if(siz[v]>siz[hson[p]])hson[p]=v;
    }
}
void dfs2(int p,int tp){
    top[p]=tp,num[p]=++tot;
    if(!hson[p])return;
    dfs2(hson[p],tp);
    for(ri i=0,v;i<e[p].size();++i)if((v=e[p][i])!=fa[p]&&v!=hson[p])dfs2(v,v);
}
namespace SGT{
    #define lc (son[p][0])
    #define rc (son[p][1])
    #define mid (l+r>>1)
    int siz[(N+M)*30],son[(N+M)*30][2],tot=0;
    inline void update(int&p,int l,int r,int k,int v){
        if(!p)p=++tot;
        siz[p]+=v;
        if(l==r)return;
        k<=mid?update(lc,l,mid,k,v):update(rc,mid+1,r,k,v);
    }
    inline int query(int p,int l,int r,int ql,int qr){
        if(!siz[p])return 0;
        if(ql<=l&&r<=qr)return siz[p];
        if(qr<=mid)return query(lc,l,mid,ql,qr);
        if(ql>mid)return query(rc,mid+1,r,ql,qr);
        return query(lc,l,mid,ql,mid)+query(rc,mid+1,r,mid+1,qr);
    }
    #undef lc
    #undef rc
    #undef mid
}
inline int query(int x,int y,int t){
    if(!t)return 0;
    int ret=0;
    while(top[x]^top[y]){
        if(dep[top[x]]<dep[top[y]])swap(x,y);
        ret+=SGT::query(rt[t],1,300000,num[top[x]],num[x]),x=fa[top[x]];
    }
    if(dep[x]<dep[y])swap(x,y);
    return ret+SGT::query(rt[t],1,300000,num[y],num[x]);
}
int main(){
    n=read(),m=read();
    for(ri i=1;i<=n;++i)if(!S[col[i]=read()])S[col[i]]=++id;
    for(ri i=1,u,v;i<n;++i)u=read(),v=read(),e[u].push_back(v),m,e[v].push_back(u);
    dfs1(1),dfs2(1,1);
    for(ri i=1;i<=n;++i)SGT::update(rt[S[col[i]]],1,300000,num[i],1);
    char s[2];
    for(ri x,y;m;--m){
        scanf("%s",s);
        if(s[0]=='Q')cout<<(x=read(),y=read(),query(x,y,S[read()]))<<'\n';
        else{
            x=read(),y=read();
            if(!S[y])S[y]=++id;
            SGT::update(rt[S[col[x]]],1,300000,num[x],-1);
            col[x]=y;
            SGT::update(rt[S[col[x]]],1,300000,num[x],1);
        }
    }
    return 0;
}

2019.03.09 bzoj4999: This Problem Is Too Simple!(树链剖分+线段树动态开点)的更多相关文章

  1. 2019.03.09 bzoj4491: 我也不知道题目名字是什么(线段树)

    传送门 题意:给定一个序列A[i],每次询问l,r,求[l,r]内最长子串,使得该子串为不上升子串或不下降子串. 思路: 注意要求的是子串而不是子序列!!! 然后直接用线段树维护最大子段和的方式合并一 ...

  2. 2019西北工业大学程序设计创新实践基地春季选拔赛 I Chino with Rewrite (并查集+树链剖分+线段树)

    链接:https://ac.nowcoder.com/acm/contest/553/I 思路:离线整棵树,用并查集维护下联通的情况,因为值只有60个,用2的x(1<=x<=60)次方表示 ...

  3. 2019 ACM-ICPC 西安全国邀请赛 E-Tree 树链剖分+线段树

    题意 给一颗带点权的树,三种操作 \(1~s~t\) 修改从1到s的路径上的所有点,\(a[i]=a[i]|t\) \(2~s~t\) 修改从1到s的路径上的所有点,\(a[i]=a[i]\& ...

  4. 2019.01.21 bzoj1758: [Wc2010]重建计划(01分数规划+长链剖分+线段树)

    传送门 长链剖分好题. 题意简述:给一棵树,问边数在[L,R][L,R][L,R]之间的路径权值和与边数之比的最大值. 思路: 用脚指头想都知道要01分数规划. 考虑怎么checkcheckcheck ...

  5. 2019年ICPC南昌网络赛 J. Distance on the tree 树链剖分+主席树

    边权转点权,每次遍历到下一个点,把走个这条边的权值加入主席树中即可. #include<iostream> #include<algorithm> #include<st ...

  6. FZU2176---easy problem (树链剖分)

    http://acm.fzu.edu.cn/problem.php?pid=2176 Problem 2176 easy problem Accept: 9    Submit: 32Time Lim ...

  7. (中等) HDU 5293 Tree chain problem,树链剖分+树形DP。

    Problem Description   Coco has a tree, whose vertices are conveniently labeled by 1,2,…,n.There are ...

  8. 主席树||可持久化线段树+离散化 || 莫队+分块 ||BZOJ 3585: mex || Luogu P4137 Rmq Problem / mex

    题面:Rmq Problem / mex 题解: 先离散化,然后插一堆空白,大体就是如果(对于以a.data<b.data排序后的A)A[i-1].data+1!=A[i].data,则插一个空 ...

  9. Codeforces Round #425 (Div. 2) Problem D Misha, Grisha and Underground (Codeforces 832D) - 树链剖分 - 树状数组

    Misha and Grisha are funny boys, so they like to use new underground. The underground has n stations ...

随机推荐

  1. python1.返回一个字符串中出现次数第二多的单词 2.字符串中可能有英文单词、标点、空格 3.字符串中的英文字符全部是小写

    import re from collections import Counter def second_count_word(s): # # 利用正则按标点和空格切割,有其他标点可以添加到[]内 # ...

  2. go调查内存泄漏

    curl x.x.x.x/debug/pprof/heap > base.heap 过段时间 curl x.x.x.x/debug/pprof/heap > current.heap go ...

  3. java后台动态生成导出excel

    p ublic void export(List<WechatUser> wechatUserList, HttpServletResponse response) throws IOEx ...

  4. C#移除URL上指定的参数

    /// <summary>        /// 移除URL上指定的参数,不区分参数大小写        /// </summary>        public static ...

  5. IDEA中添加javap反编译

  6. layui---十分适用于PC端后台的框架

    1.关闭当前页面: top.$(".layui-tab-title").find("li.layui-this>i").click(); 2.调用指定ID ...

  7. IIS7/8下提示 HTTP 错误 404.13 - Not Found 请求筛选模块被配置为拒绝超过请求内容长度的请求

    IIS7的设置和IIS6有很多不同之处,这里提到的的是一个上传附件大小设置的问题. HTTP 错误 404.13 - Not Found 请求筛选模块被配置为拒绝超过请求内容长度的请求. 原因:Web ...

  8. 部署代码review和CI

    公司原先搭了一个代码Review的服务器,由于历史原因,装的是一个32bit的Ubuntu系统,后来由于需要,需要安装gitlab,由于gitlab需要64位系统,所以临时凑合了个vagrant,本质 ...

  9. js 14位字符串 转日期

    const pattern = /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/; const newDate = new Date(timeEnd.repla ...

  10. 分析easyswoole3.0源码,服务启动为例(二)

    以下内容需要结合es的源码,不然可能会觉得跳跃.先描述下es启动的大致流程.es启动的时候注册异常处理函数以及加载配置文件.根据位置文件的设置选择启动哪种swoole服务.然后用一个事件注册类,注册s ...