【Luogu】U16325小奇的花园(树链剖分)
学了学动态开点的树链剖分,其实跟动态开点的线段树差不多啦
查询的时候别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小奇的花园(树链剖分)的更多相关文章
- 【Luogu】P3950部落冲突(树链剖分)
题目链接 状态奇差无比,sbt都能错一遍. 不动笔光想没有想到怎么做,画图之后发现一个很明显的性质…… 那就是两个开战的部落,其中一个是另一个的父亲. 所以在儿子那里加个权值.查询的时候树链剖分查询链 ...
- Luogu P3384 【【模板】树链剖分】
转载请注明出处,部分内容引自banananana大神的博客 ~~别说你不知道什么是树~~╮(─▽─)╭(帮你百度一下) 先来回顾两个问题:1,将树从x到y结点最短路径上所有节点的值都加上z 这也是个模 ...
- [luogu3676] 小清新数据结构题 [树链剖分+线段树]
题面 传送门 思路 本来以为这道题可以LCT维护子树信息直接做的,后来发现这样会因为splay形态改变影响子树权值平方和,是splay本身的局限性导致的 所以只能另辟蹊径 首先,我们考虑询问点都在1的 ...
- Luogu P2146 软件包管理器(树链剖分+线段树)
题意 给定\(n\)个软件包,每个软件包都有一个依赖软件包,安装一个软件包必须安装他的依赖软件包,卸载一个软件包必须先卸载所有依赖于它的软件包.给定\(m\)此操作,每次一个操作\(install/u ...
- [note]树链剖分
树链剖分https://www.luogu.org/problemnew/show/P3384 概念 树链剖分,是一种将树剖分成多条不相交的链的算法,并通过其他的数据结构来维护这些链上的信息. 最简单 ...
- 【Luogu】P3313旅行(树链剖分)
题目链接 动态开点的树链剖分qwq. 跟小奇的花园一模一样,不做过多讲解. #include<cstdio> #include<cstring> #include<cct ...
- Luogu 2680 NOIP 2015 运输计划(树链剖分,LCA,树状数组,树的重心,二分,差分)
Luogu 2680 NOIP 2015 运输计划(树链剖分,LCA,树状数组,树的重心,二分,差分) Description L 国有 n 个星球,还有 n-1 条双向航道,每条航道建立在两个星球之 ...
- Luogu 2590 [ZJOI2008]树的统计 / HYSBZ 1036 [ZJOI2008]树的统计Count (树链剖分,LCA,线段树)
Luogu 2590 [ZJOI2008]树的统计 / HYSBZ 1036 [ZJOI2008]树的统计Count (树链剖分,LCA,线段树) Description 一棵树上有n个节点,编号分别 ...
- 【NOI省选模拟】小奇的花园
「题目背景」 小奇在家中的花园漫步时,总是会思考一些奇怪的问题. 「问题描述」 小奇的花园有n个温室,标号为1到n,温室以及以及温室间的双向道路形成一棵树. 每个温室都种植着一种花,随着季节的变换,温 ...
随机推荐
- (四)SpringMVC之使用cookie实现记住密码的功能
注意:因为实现记住密码的功能需要用到json,所以需要加上这条语句: <script type="text/javascript" src="scripts/jqu ...
- ABAP system landscape和vue项目webpack构建的最佳实践
基于Netweaver的ABAP transport route一般都有dev,test和prod三种类型的系统. 而Vue前端项目的webpack build设置也类似. 以SAP成都研究院数字创新 ...
- OpenLayers 3 的 图层控制控件
openlayers3的control中没有提供默认的图层控制控件. 但是git上已经有造好的轮子,直接拿来用就可以了.地址 https://github.com/walkermatt/ol3-lay ...
- 用户输入和while循环
函数input()的工作原理 message=input('Tell me something,and I will repeat it back to you:') print(message) 编 ...
- DateTime与long互转
DateTime转long: public static long GetDateLong(object time) { DateTime epoc = TimeZone.CurrentTimeZon ...
- iOS开发遇到的坑之五--解决工程已存在plist表,数据却不能存入的问题
想写这篇博客其实在一两个月前开发遇见的时候就想把这个问题写成博客的,奈何自己一直懒外加一直没有时间,就把这个事情给耽搁了,好在当时知道下自己一定要把这个问题给描述出来,免得以后其他人遇到这个问题会纠结 ...
- poj3335 Rotating Scoreboard
题目描述: vjudge POJ 题解: 半平面交判核的存在性. 重点在于一个点的核也算核. 这样的话普通的求多边形的版本就要加一个特判. 就是把剩下的一个节点暴力带回所有直线重判,这时判叉积是否$\ ...
- Springboot @Autowired 无法注入问题
特别提醒:一定要注意文件结构 WebappApplication 一定要在包的最外层,否则Spring无法对所有的类进行托管,会造成@Autowired 无法注入. 1. 添加工具类获取在 Sprin ...
- 使用jquery清除select中的所有option
html代码 <select id="search"> <option>baidu</option> <option>sogou&l ...
- crond定时操作 crontab
* * * * * 分别表示 分钟 小时 日 月 星期(0-6) 30 17,28,19 * * * 或 30 17-19 * * * 在每天的17-19小时半点时刻执行 30 8-18 ...