题意

一棵树

多次修改,每次修改一个点到根的所有边的颜色,并询问现在有哪些颜色染了恰好$m$条边

题解:

稍加思考可以知道,从某个点到根节点的颜色数,均摊复杂度很低,因此,可以考虑珂朵莉树维护重链剖分

这里也记录一下珂朵莉树的代码

代码:

#include<bits/stdc++.h>
#define endl '\n'
#define ll long long
#define ull unsigned long long
#define fi first
#define se second
#define pii pair<int,int>
#define all(x) x.begin(),x.end()
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define rep(ii,a,b) for(int ii=a;ii<=b;++ii)
#define per(ii,a,b) for(int ii=b;ii>=a;--ii)
#define forn(ii,x) for(int ii=head[x];ii;ii=edge[ii].next)
using namespace std;
const int maxn=2e5+20,maxm=2e6+10;
const int INF=0x3f3f3f3f;
const ll mod=1e9+7;
int casn,n,m,k,col[maxn],ans[maxn];
class odtree{public:
struct segnode{int l,r,c;bool operator <(const segnode &a)const{return r<a.r;}};
set<segnode> nodes;
void split(int pos){auto it=nodes.lower_bound({pos,pos});
if(it==nodes.end()||(it->l)>pos||(it->r)==pos) return ;
int l=it->l,r=it->r,c=it->c;
nodes.erase(it);nodes.insert({l,pos,c});nodes.insert({pos+1,r,c});
}
void update(int l,int r,int c){
split(l-1);split(r);
while(1){auto it=nodes.lower_bound({l,l});
if(it==nodes.end()||(it->l)>r) break;
if(it->c){--ans[col[it->c]];col[it->c]-=(it->r)-(it->l)+1;++ans[col[it->c]];}
nodes.erase(it);
}
--ans[col[c]];col[c]+=r-l+1;++ans[col[c]];
nodes.insert({l,r,c});
}
}tree;
namespace chain{
struct data_e{int to,next;}edge[maxn<<1];
int head[maxn],nume,mp[maxn];
int ltop[maxn],fa[maxn],deep[maxn];
int sz[maxn],remp[maxn],son[maxn],cnt;
inline void addedge(int a,int b){edge[++nume]={b,head[a]};head[a]=nume;}
void init(){rep(i,1,n) head[i]=0;cnt=nume=0;}
void dfs1(int now,int pre,int d){
deep[now]=d,fa[now]=pre,sz[now]=1,son[now]=0;
forn(i,now){int to=edge[i].to;
if(to!=pre) {
dfs1(to,now,d+1);sz[now]+=sz[to];
if(sz[to]>sz[son[now]]) son[now]=to;
}
}
}
void dfs2(int now,int pre,int sp){
ltop[now]=sp;mp[now]=++cnt;remp[cnt]=now;
if(son[now]) dfs2(son[now],now,sp);
forn(i,now){int to=edge[i].to;
if(to!=son[now]&&to!=pre) dfs2(to,now,to);
}
}
void gao(int st=1){dfs1(st,0,0);dfs2(st,0,st);}
void update(int now,int c){
while(now>1){
int l=max(mp[ltop[now]],2),r=mp[now];
if(l<=r) tree.update(l,r,c);
now=fa[ltop[now]];
}
}
};
int main() {IO;
cin>>n>>m>>k;
rep(i,1,n-1){int a,b;
cin>>a>>b;
chain::addedge(a,b);chain::addedge(b,a);
}
ans[0]=m;
chain::gao();
tree.nodes.insert({2,n,0});
while(k--){int a,b,c;
cin>>a>>b>>c;
chain::update(a,b);
cout<<ans[c]<<endl;
}
return 0;
}

gym 102059A 树链剖分后odt维护区间的更多相关文章

  1. HDU 3966 树链剖分后线段树维护

    题意: 一棵树, 操作1.$path(a,b)$之间的点权$+k$ 操作2.单点查询 题解: 树链剖分即可,注意代码细节,双向映射 主要是记录一下板子 #include <string.h> ...

  2. 2019 icpc南昌全国邀请赛-网络选拔赛J题 树链剖分+离线询问

    链接:https://nanti.jisuanke.com/t/38229 题意: 给一棵树,多次查询,每次查询两点之间权值<=k的边个数 题解: 离线询问,树链剖分后bit维护有贡献的位置即可 ...

  3. BZOJ 3672[NOI2014]购票(树链剖分+线段树维护凸包+斜率优化) + BZOJ 2402 陶陶的难题II (树链剖分+线段树维护凸包+分数规划+斜率优化)

    前言 刚开始看着两道题感觉头皮发麻,后来看看题解,发现挺好理解,只是代码有点长. BZOJ 3672[NOI2014]购票 中文题面,题意略: BZOJ 3672[NOI2014]购票 设f(i)f( ...

  4. HDU5840 (分块+树链剖分)

    Problem This world need more Zhu 题目大意 给一颗n个点的有点权的树,有m个询问,对于每个询问u,v,k,首先将点u到点v的最短路径上的所有点按顺序编号,u的编号为1, ...

  5. HDU5221 Occupation 树链剖分

    题意: 给出一棵树,root=1,树有点权,有一个人叫做M 有3种操作: 1 u v 把u到v路径上的所有点的点权都给M 2 u 若u的点权在M手上,拿走 3 u 把u为根的子树的所有点权都给M 每一 ...

  6. BZOJ4712洪水——动态DP+树链剖分+线段树

    题目描述 小A走到一个山脚下,准备给自己造一个小屋.这时候,小A的朋友(op,又叫管理员)打开了创造模式,然后飞到 山顶放了格水.于是小A面前出现了一个瀑布.作为平民的小A只好老实巴交地爬山堵水.那么 ...

  7. Luogu 2590 [ZJOI2008]树的统计 / HYSBZ 1036 [ZJOI2008]树的统计Count (树链剖分,LCA,线段树)

    Luogu 2590 [ZJOI2008]树的统计 / HYSBZ 1036 [ZJOI2008]树的统计Count (树链剖分,LCA,线段树) Description 一棵树上有n个节点,编号分别 ...

  8. BZOJ4732. [清华集训2016]数据交互(树链剖分+线段树+multiset)

    题目链接 https://www.lydsy.com/JudgeOnline/problem.php?id=4732 题解 首先,一个比较显然的结论是:对于一棵有根树上的两条链 \((x_1, y_1 ...

  9. BZOJ 2243:染色(树链剖分+区间合并线段树)

    [SDOI2011]染色Description给定一棵有n个节点的无根树和m个操作,操作有2类:1.将节点a到节点b路径上所有点都染成颜色c:2.询问节点a到节点b路径上的颜色段数量(连续相同颜色被认 ...

随机推荐

  1. Servlet开发笔记(一)

    一.Servlet简介 Servlet是sun公司提供的一门用于开发动态web资源的技术. Sun公司在其API中提供了一个servlet接口,用户若想用发一个动态web资源(即开发一个Java程序向 ...

  2. powershell 常用命令之取磁盘分区信息

    //查看mac 地址 PS C:\Users\yyy> get-wmiobject -class Win32_NetworkAdapterConfiguration -namespace &qu ...

  3. 几个不常用的C++关键字

    volatile 遇到这个关键字声明的变量,编译器对访问该变量的代码就不再进行优化,从而可以提供对特殊地址的稳定访问. 所以说volatile可以保证对特殊地址的稳定访问. mutable 如果需要在 ...

  4. MySQL——合并查询结果

    利用  UNION 关键字,可以给出多条  SELECT  语句,并将它们的结果组合成一个结果集.合并时,两个表对应的列数和数据类型必须相同.SELECT 语句之间使用  UNION  或  UNIO ...

  5. python正则匹配示例

    text="山东省临沂市兰山区 市委大院中区21号楼4单元 276002 奥特曼1号 18254998111" #匹配手机号 m=re.findall(r"1\d{10} ...

  6. LODOP获取打印成功,是否加入队列

    之前博文介绍过获取打印机状态码 LODOP获取打印机状态码和状态码含义测试,但是打印机种类千差万别,状态码不一定准确,特别是打印成功的状态码,获取任务不在队列,可以判断打印成功,删除任务也是任务不在队 ...

  7. codeforces510D

    Fox And Jumping CodeForces - 510D Fox Ciel is playing a game. In this game there is an infinite long ...

  8. k8s-jenkins 自动化1

    一个流水线例子: 设置参数化构建: 流水线指令: def label = "docker-${UUID.randomUUID().toString()}" podTemplate( ...

  9. mpvue——Error: Cannot find module 'escape-string-regexp'

    报错 $ cnpm run build > mpvue-qq@1.0.0 build D:\wamp\www\wxsmallsoft\mini-0212\mpvueQQ > node bu ...

  10. 为Jupyter只安装目录的扩展包

    项目地址:https://github.com/minrk/ipython_extensions/tree/master/nbextensions 一般都是让安装Nbextensions,而这些扩展我 ...