题目链接

  状态奇差无比,sbt都能错一遍。

  不动笔光想没有想到怎么做,画图之后发现一个很明显的性质……

  那就是两个开战的部落,其中一个是另一个的父亲。

  所以在儿子那里加个权值。查询的时候树链剖分查询链上点权和,减去lca的点权(因为lca那如果有点权,代表的是lca和lca的父亲之间的那条边)。

  

#include<cstdio>
#include<algorithm>
#include<cctype>
#include<cstring>
#include<cstdlib>
#define left (rt<<1)
#define right (rt<<1|1)
#define mid ((l+r)>>1)
#define lson l,mid,left
#define rson mid+1,r,right
#define maxn 300050
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;
} 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;
} int tree[maxn*];
int dfn[maxn];
int deep[maxn];
int father[maxn];
int size[maxn];
int top[maxn];
int son[maxn];
int ID,n,m; void unifnd(int x,int fa){
deep[x]=deep[fa]+; size[x]=;
for(int i=head[x];i;i=edge[i].next){
int to=edge[i].to;
if(to==fa) continue;
father[to]=x;
unifnd(to,x);
size[x]+=size[to];
if(son[x]==||size[son[x]]<size[to]) son[x]=to;
}
} void unionn(int x,int Top){
dfn[x]=++ID; top[x]=Top;
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);
}
} inline void pushup(int rt){
tree[rt]=tree[left]+tree[right];
} void update(int o,int num,int l,int r,int rt){
if(l==r){
tree[rt]+=num;
return;
}
if(o<=mid) update(o,num,lson);
else update(o,num,rson);
pushup(rt);
return;
} int query(int from,int to,int l,int r,int rt){
if(from<=l&&to>=r) return tree[rt];
int ans=;
if(from<=mid) ans+=query(from,to,lson);
if(to>mid) ans+=query(from,to,rson);
return ans;
} struct War{
int from,to;
}q[maxn];int cnt; inline void adds(int from,int to){
if(deep[from]<deep[to]) swap(from,to);
q[++cnt]=(War){from,to};
update(dfn[from],,,n,);
return;
} inline void del(int rnk){
int from=q[rnk].from;
update(dfn[from],-,,n,);
} inline int LCA(int from,int to){
while(top[from]!=top[to]){
if(deep[top[from]]<deep[top[to]]) swap(from,to);
from=father[top[from]];
}
if(deep[from]>deep[to]) swap(from,to);
return from;
} inline int ask(int from,int to){
int lca=LCA(from,to),ans=-query(dfn[lca],dfn[lca],,n,);
while(top[from]!=top[to]){
if(deep[top[from]]<deep[top[to]]) swap(from,to);
ans+=query(dfn[top[from]],dfn[from],,n,);
if(ans>) return ;
from=father[top[from]];
}
if(deep[from]>deep[to]) swap(from,to);
ans+=query(dfn[from],dfn[to],,n,);
if(ans>) return ;
return ;
} int main(){
n=read(),m=read();
for(int i=;i<n;++i){
int x=read(),y=read();
add(x,y);
add(y,x);
}
unifnd(,);
unionn(,);
for(int i=;i<=m;++i){
char c[];
scanf("%s",c);
if(c[]=='Q'){
int x=read(),y=read();
if(ask(x,y)) printf("Yes\n");
else printf("No\n");
}
else if(c[]=='C'){
int x=read(),y=read();
adds(x,y);
}
else{
int x=read();
del(x);
}
}
return ;
}

【Luogu】P3950部落冲突(树链剖分)的更多相关文章

  1. luogu题解 P3950部落冲突--树链剖分

    题目链接 https://www.luogu.org/problemnew/show/P3950 分析 大佬都用LCT,我太弱只会树链剖分 一个很裸的维护边权树链剖分题.按照套路,对于一条边\(< ...

  2. 洛谷 P3950 部落冲突 树链剖分

    目录 题面 题目链接 题目描述 输入输出格式 输入格式 输出格式 输入输出样例 输入样例1 输出样例1 输入样例2 输出样例2 输入样例3 输出样例3 说明 思路 AC代码 总结 题面 题目链接 P3 ...

  3. Luogu P2486 [SDOI2011]染色(树链剖分+线段树合并)

    Luogu P2486 [SDOI2011]染色 题面 题目描述 输入输出格式 输入格式: 输出格式: 对于每个询问操作,输出一行答案. 输入输出样例 输入样例: 6 5 2 2 1 2 1 1 1 ...

  4. Luogu P2486 染色(树链剖分+线段树)

    题解 不妨采取重链剖分的方式把路径剖成区间,然后用线段树维护,考虑如何合并一个区间 struct Node { int lf, rg, tot; }seg[N << 2]; int col ...

  5. 【luogu P3950 部落冲突】 题解

    题目连接:https://www.luogu.org/problemnew/show/P3950 1.像我这种学数据结构学傻了的 2.边权化点权 所有点权初始化0 3.对于战争 将深度较深的-1,对于 ...

  6. luogu P3950 部落冲突

    嘟嘟嘟 树剖板子题. #include<cstdio> #include<iostream> #include<algorithm> #include<cma ...

  7. 【题解】Luogu P3950 部落冲突

    原题传送门 这题用Link-Cut-Tree解决,Link-Cut-Tree详解 我们用Link-Cut-Tree维护连通性(十分无脑) 一开始先把树中每条边的两端连接 U操作:把u,v两个点连起来 ...

  8. lupgu P3950 部落冲突

    题目链接 luogu P3950 部落冲突 题解 树剖线段树可以 lct还行 代码 #include<cstdio> #include<algorithm> inline in ...

  9. 洛谷P2590 [ZJOI2008]树的统计 题解 树链剖分+线段树

    题目链接:https://www.luogu.org/problem/P2590 树链剖分模板题. 剖分过程要用到如下7个值: fa[u]:u的父节点编号: dep[u]:u的深度: size[u]: ...

随机推荐

  1. 重温Javascript(三)-继承

    继承 1.原型链继承 基本思想是利用原型让一个引用类型继承另一个引用类型的属性和方法.每个构造函数都有一个原型对象,原型对象都包含一个指向构造函数的指针,而实例都包含一个指向原型对象的内部指针.让原型 ...

  2. php 基本连接mysql数据库和查询数据

    连接数据库,有三种方法 1. 常规方式: $con=mysql_connect($dbhostip,$username,$userpassword) or die("Unable to co ...

  3. 用dfs求解八皇后问题

    相信大家都已经很熟悉八皇后问题了,就是指:在8X8格的国际象棋上摆放八个皇后,使其不能互相攻击,即任意两个皇后都不能处于同一行.同一列或同一斜线上,问有多少种摆法.主要思路:按行进行深度优先搜索,在该 ...

  4. IDEA设置每次打开重新选择项目

    通过这里,选择settings,或者进入之后的FILE->settings.搜索System 即可出现

  5. 03_4_this关键字

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

  6. 关于C与C++的区别

    笔者介绍:姜雪伟,IT公司技术合伙人,IT高级讲师,CSDN社区专家,特邀编辑,畅销书作者,已出版书籍:<手把手教你架构3D游戏引擎>电子工业出版社和<Unity3D实战核心技术详解 ...

  7. 将php数组转js数组,js如何接收PHP数组,json的用法

    首先下载下面这个文件(这是一段是别人写出来专门解析json的代码),然后引入这个文件! http://pan.baidu.com/s/1dD8qVr7 现在当我们需要用ajax与后台进行交互时,怎样将 ...

  8. python爬虫基础13-selenium大全7/8-异常

    Selenium笔记(7)异常 本文集链接:https://www.jianshu.com/nb/25338984 完整文档 Exceptions that may happen in all the ...

  9. sql执行过长,如何入手优化

    一条sql执行过长的时间,你如何优化,从哪些方面 1.查看sql是否涉及多表的联表或者子查询,如果有,看是否能进行业务拆分,相关字段冗余或者合并成临时表(业务和算法的优化)2.涉及链表的查询,是否能进 ...

  10. ACM-ICPC 2018 徐州赛区网络预赛 B. BE, GE or NE

    In a world where ordinary people cannot reach, a boy named "Koutarou" and a girl named &qu ...