题目链接

  学了学动态开点的树链剖分,其实跟动态开点的线段树差不多啦

  查询的时候别ssbb地动态开点,如果没这个点果断返回0就行

  只要注意花的种类能到intmax就行qwq!!!!

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cctype>
#include<cstdlib>
#include<map>
#define mid ((l+r)>>1)
#define maxn 100010
#define check(x) if(x==0) x=++tot;
using namespace std;
inline long long read(){
long long num=,f=;
char ch=getchar();
while(!isdigit(ch)){
if(ch=='-') f=-;
ch=getchar();
}
while(isdigit(ch)){
num=num*+ch-'';
ch=getchar();
}
return num*f;
} int tot;
map<int,int> root;
int ls[maxn*];
int rs[maxn*];
int q[maxn];
int size[maxn];
int top[maxn];
int son[maxn];
int father[maxn];
int dfn[maxn];
int deep[maxn];
int back[maxn],cnt;
int n,m; struct Edge{
int next,to;
}edge[maxn*];
int head[maxn],num;
inline void add(int from,int to){
edge[++num]=(Edge){head[from],to};
head[from]=num;
} void find(int x,int fa){
size[x]=; deep[x]=deep[fa]+;
for(int i=head[x];i;i=edge[i].next){
int to=edge[i].to;
if(to==fa) continue;
father[to]=x;
find(to,x);
size[x]+=size[to];
if(son[x]==||size[son[x]]<size[to]) son[x]=to;
}
} void unionn(int x,int Top){
top[x]=Top; dfn[x]=++cnt; back[cnt]=x;
if(son[x]==) return;
unionn(son[x],Top);
for(int i=head[x];i;i=edge[i].next){
int to=edge[i].to;
if(to==father[x]||to==son[x]) continue;
unionn(to,to);
}
return;
} inline void pushup(int rt){ tree[rt]=tree[ls[rt]]+tree[rs[rt]]; } void update(int o,int num,int l,int r,int &rt){
check(rt);
if(l==r){
tree[rt]+=num;
return;
}
if(o<=mid) update(o,num,l,mid,ls[rt]);
else update(o,num,mid+,r,rs[rt]);
pushup(rt);
} int query(int from,int to,int l,int r,int &rt){
if(rt==) return ;
if(from<=l&&to>=r) return tree[rt];
int ans=;
if(from<=mid) ans+=query(from,to,l,mid,ls[rt]);
if(to>mid) ans+=query(from,to,mid+,r,rs[rt]);
return ans;
} inline void addcol(int pos,int val){
update(dfn[pos],-,,n,root[q[pos]]);
update(dfn[pos],,,n,root[val]);
q[pos]=val;
} int ask(int from,int to,int val){
int ans=;
while(top[from]!=top[to]){
if(deep[top[from]]<deep[top[to]]) swap(from,to);
ans+=query(dfn[top[from]],dfn[from],,n,root[val]);
from=father[top[from]];
}
if(deep[from]>deep[to]) swap(from,to);
ans+=query(dfn[from],dfn[to],,n,root[val]);
return ans;
} int main(){
n=read(),m=read();
for(int i=;i<=n;++i) q[i]=read();
for(int i=;i<n;++i){
int from=read(),to=read();
add(from,to);
add(to,from);
}
find(,);
unionn(,);
for(int i=;i<=n;++i) update(dfn[i],,,n,root[q[i]]);
int last=;
for(int i=;i<=m;++i){
char c[];int x,y;
scanf("%s",c);
if(c[]=='C'){
x=read()^last,y=read()^last;
addcol(x,y);
}
else{
x=read()^last,y=read()^last;
int z=read()^last;
last=ask(x,y,z);
printf("%d\n",last);
}
}
return ;
}

【Luogu】U16325小奇的花园(树链剖分)的更多相关文章

  1. 【Luogu】P3950部落冲突(树链剖分)

    题目链接 状态奇差无比,sbt都能错一遍. 不动笔光想没有想到怎么做,画图之后发现一个很明显的性质…… 那就是两个开战的部落,其中一个是另一个的父亲. 所以在儿子那里加个权值.查询的时候树链剖分查询链 ...

  2. Luogu P3384 【【模板】树链剖分】

    转载请注明出处,部分内容引自banananana大神的博客 ~~别说你不知道什么是树~~╮(─▽─)╭(帮你百度一下) 先来回顾两个问题:1,将树从x到y结点最短路径上所有节点的值都加上z 这也是个模 ...

  3. [luogu3676] 小清新数据结构题 [树链剖分+线段树]

    题面 传送门 思路 本来以为这道题可以LCT维护子树信息直接做的,后来发现这样会因为splay形态改变影响子树权值平方和,是splay本身的局限性导致的 所以只能另辟蹊径 首先,我们考虑询问点都在1的 ...

  4. Luogu P2146 软件包管理器(树链剖分+线段树)

    题意 给定\(n\)个软件包,每个软件包都有一个依赖软件包,安装一个软件包必须安装他的依赖软件包,卸载一个软件包必须先卸载所有依赖于它的软件包.给定\(m\)此操作,每次一个操作\(install/u ...

  5. [note]树链剖分

    树链剖分https://www.luogu.org/problemnew/show/P3384 概念 树链剖分,是一种将树剖分成多条不相交的链的算法,并通过其他的数据结构来维护这些链上的信息. 最简单 ...

  6. 【Luogu】P3313旅行(树链剖分)

    题目链接 动态开点的树链剖分qwq. 跟小奇的花园一模一样,不做过多讲解. #include<cstdio> #include<cstring> #include<cct ...

  7. Luogu 2680 NOIP 2015 运输计划(树链剖分,LCA,树状数组,树的重心,二分,差分)

    Luogu 2680 NOIP 2015 运输计划(树链剖分,LCA,树状数组,树的重心,二分,差分) Description L 国有 n 个星球,还有 n-1 条双向航道,每条航道建立在两个星球之 ...

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

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

  9. 【NOI省选模拟】小奇的花园

    「题目背景」 小奇在家中的花园漫步时,总是会思考一些奇怪的问题. 「问题描述」 小奇的花园有n个温室,标号为1到n,温室以及以及温室间的双向道路形成一棵树. 每个温室都种植着一种花,随着季节的变换,温 ...

随机推荐

  1. 11gR2 如何诊断节点重启问题

    本文对如何诊断11gR2 GI环境下的节点重启问题进行了一些介绍. 首先,像10g版本一样,我们首先介绍在GI中能够导致节点重启的进程.1.Ocssd.bin:这个进程的功能和10g版本的功能基本差不 ...

  2. 【转】Deactivating your reflector

    原文:http://blog.csdn.net/cxwl3sxl/article/details/8072195 背景: 因为想破解一个.net写的程序,需要在visual studio 2010中使 ...

  3. Python-OpenCV中的resize()函数

    改变图像大小意味着改变尺寸,无论是单独的高或宽,还是两者.也可以按比例调整图像大小. 这里将介绍resize()函数的语法及实例. 语法 函数原型 cv2.resize(src, dsize[, ds ...

  4. 最大长度回文子串(Manacher's algorithm)

    输出最大长度的回文子串. string longestPalindrome(string s) { int id, mx, i, j, len, maxlen; vector<char> ...

  5. word中在空白处加下划线不显示解决

    终极解决:Ctrl + Shift + Space Alt + 选择,竖向选择.和VS,其他一些编辑器一样

  6. 03_4_this关键字

    03_4_this关键字 1. this关键字 在类的方法定义中使用的this关键字代表使用该方法的对象的引用. 当必须指出当前使用方法的对象是谁时要使用this. 有时使用this可以处理方法中成员 ...

  7. pandas实践——美国人口分析

    1.导入文件,并查看数据样本 abbr = pd.read_csv("./state-abbrevs.csv")areas =pd.read_csv("./state-a ...

  8. 树莓派开发板入门学习笔记1:[转]资料收集及树莓派系统在Ubuntu安装

    参考教程(微雪课堂):http://www.waveshare.net/study/portal.php 树莓派实验室: http://shumeipai.nxez.com/2014/12/21/us ...

  9. spring+struts2+hibernate框架依赖pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  10. 在spring boot中使用webSocket组件(一)

    最近在项目中使用到了spring的webSocket组件,在这里和大家分享下,如有错误,欢迎大家指正. 在这里我使用的IDE工具是Intellij idea,框架是spring boot.spring ...