当前位置:网站首页>Logu P2486 [sdoi2011] coloring (tree chain + segment tree + merging of intervals on the tree)
Logu P2486 [sdoi2011] coloring (tree chain + segment tree + merging of intervals on the tree)
2022-06-25 08:02:00 【Mfy's little brother 1】
Luogu P2486 [SDOI2011] dyeing ( Tree chain + Line segment tree + Interval merging on tree )
The question :
Given a tree n Rootless tree with nodes , share m Operations , There are two kinds of operations :
The nodes a To the node b All points on the path of ( Include a and b) All dyed in color c.
Ask nodes a To the node b Number of color segments on the path of .
A color segment is defined as an extremely long continuous segment of the same color . for example 112221 It consists of three sections :11、222、1
Ideas :
When merging intervals on a tree , Consider the last boundary
#include <bits/stdc++.h>
using namespace std;
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
typedef long long ll;
const int INF=0x3f3f3f3f;
const int maxn=2e5+100;
int n,m,k,RC,LC,tot,a[maxn],siz[maxn],fa[maxn],son[maxn],id[maxn],idd[maxn],top[maxn],deep[maxn],lazy[maxn<<2];
vector<int>mp[maxn];
struct NODE{
ll ans,sum,ml,mr,l,r;
}tree[maxn<<2];
void dfs1(int x,int f,int d){
siz[x]=1;
fa[x]=f;
deep[x]=d;
son[x]=0;
for(auto y:mp[x]){
if(y==f)continue;
dfs1(y,x,d+1);
siz[x]+=siz[y];
if(siz[son[x]]<siz[y])son[x]=y;
}
}
void dfs2(int x,int root){
id[x]=++tot;
top[x]=root;
idd[tot]=a[x];
if(son[x])dfs2(son[x],root);
for(auto y:mp[x]){
if(y!=fa[x]&&y!=son[x])dfs2(y,y);
}
}
void pushup(int rt){
tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum;
if(tree[rt<<1].mr==tree[rt<<1|1].ml)tree[rt].sum--;
tree[rt].ml=tree[rt<<1].ml,tree[rt].mr=tree[rt<<1|1].mr;
}
void pushdown(int rt){
if(lazy[rt]){
tree[rt<<1].ml=tree[rt<<1|1].mr=lazy[rt];
tree[rt<<1].mr=tree[rt<<1|1].ml=lazy[rt];
tree[rt<<1].sum=tree[rt<<1|1].sum=1;
lazy[rt<<1]=lazy[rt<<1|1]=lazy[rt];
lazy[rt]=0;
}
}
void build(int rt,int l,int r){
tree[rt].l=l,tree[rt].r=r;
if(l==r){
tree[rt].ml=tree[rt].mr=idd[l];
tree[rt].sum=1;
return;
}
int mid=(l+r)>>1;
build(rt<<1,l,mid);
build(rt<<1|1,mid+1,r);
pushup(rt);
}
void update(int rt,int x,int y,int c){
if(x<=tree[rt].l&&tree[rt].r<=y){
tree[rt].ml=tree[rt].mr=c;
tree[rt].sum=1;
lazy[rt]=c;
return;
}
pushdown(rt);
int mid=(tree[rt].l+tree[rt].r)>>1;
if(x<=mid)update(rt<<1,x,y,c);
if(y>mid)update(rt<<1|1,x,y,c);
pushup(rt);
}
NODE query(int rt,int x,int y,int xx,int yy){
if(x<=tree[rt].l&&tree[rt].r<=y){
if(tree[rt].l==xx)LC=tree[rt].ml;
if(tree[rt].r==yy)RC=tree[rt].mr;
return tree[rt];
}
int mid=(tree[rt].l+tree[rt].r)>>1;
pushdown(rt);
if(y<=mid)return query(rt<<1,x,y,xx,yy);
else if(x>mid)return query(rt<<1|1,x,y,xx,yy);
else{
NODE t,t1=query(rt<<1,x,mid,xx,yy),t2=query(rt<<1|1,mid+1,y,xx,yy);
t.ml=t1.ml;
t.mr=t2.mr;
t.sum=t1.sum+t2.sum;
if(t1.mr==t2.ml)t.sum--;
return t;
}
}
int getdis(int x,int y){
// The poem
int ans=0,pos1=0,pos2=0;//pos1 by x, Upper endpoint color of points with large depth
while(top[x]!=top[y]){
if(deep[top[x]]<deep[top[y]])swap(x,y),swap(pos1,pos2);
ans+=query(1,id[top[x]],id[x],id[top[x]],id[x]).sum;
if(RC==pos1)ans--;
pos1=LC;
x=fa[top[x]];
}
if(deep[x]>deep[y])swap(x,y),swap(pos1,pos2);
ans+=query(1,id[x],id[y],id[x],id[y]).sum;
if(LC==pos1)ans--;
if(RC==pos2)ans--;
return ans;
}
void upday(int x,int y,int c){
//x To y Interval modification
while(top[x]!=top[y]){
if(deep[top[x]]<deep[top[y]])swap(x,y);
update(1,id[top[x]],id[x],c);
x=fa[top[x]];
}
if(deep[x]>deep[y])swap(x,y);
update(1,id[x],id[y],c);
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
}
for(int i=1,x,y;i<n;i++){
scanf("%d%d",&x,&y);
mp[x].push_back(y),mp[y].push_back(x);
}
dfs1(1,0,1);
dfs2(1,1);
build(1,1,n);
for(int i=1,x,y,z;i<=m;i++){
char op[10];
scanf("%s%d%d",op,&x,&y);
if(op[0]=='Q')printf("%d\n",getdis(x,y));
else{
scanf("%d",&z);
upday(x,y,z);
}
}
}
边栏推荐
- 剑指offer刷题(简单等级)
- 新版USBCAN卡CAN分析仪的CAN&CANFD综合测试分析软件LKMaster主要功能介绍
- Electronics: Lesson 012 - Experiment 11: light and sound
- 一文了解 | 革兰氏阳性和阴性菌区别,致病差异,针对用药
- Analysis and utilization of Microsoft Office Word remote command execution vulnerability (cve-2022-30190)
- c#中设置lable控件的TextAlign属性控制文字居中的方法
- 产品经理专业知识50篇(四)-从问题到能力提升:AMDGF模型工具
- FM信号、调制信号和载波
- Electronics: Lesson 012 - Experiment 13: barbecue LED
- @The difference between resource and @autowired annotation, why recommend @resource?
猜你喜欢

使用Adobe Acrobat Pro调整PDF页面为统一大小

用函数的递归来解决几道有趣的题

Debugging mipi-dsi screen based on stm32mp157

电子学:第011课——实验 10:晶体管开关

將數據導入到MATLAB
![洛谷P1073 [NOIP2009 提高组] 最优贸易(分层图+最短路)](/img/cb/046fe4b47898fd6db86edc8a267c34.png)
洛谷P1073 [NOIP2009 提高组] 最优贸易(分层图+最短路)

Cifar-10 dataset application: quick start data enhancement method mixup significantly improves image recognition accuracy

CAN总线工作状况和信号质量“体检”

Basic use of ActiveMQ in Message Oriented Middleware

How much do you know about electronic components on PCB?
随机推荐
Drawing of clock dial
网络模型——OSI模型与TCP/IP模型
[Video] ffplay uses MJPEG format to play USB camera
C#控件刷新
socket问题记录
年后求职找B端产品经理?差点把自己坑惨了......
电子学:第010课——实验 8:继电振荡器
27. remove elements
Electronics: Lesson 013 - Experiment 14: Wearable pulsed luminaries
Electronics: Lesson 012 - Experiment 11: light and sound
电子学:第014课——实验 15:防入侵报警器(第一部分)
How to select lead-free and lead-free tin spraying for PCB? 2021-11-16
Knowledge sharing 𞓜 conventional laminated structure of six layer PCB
Atlassian confluence漏洞分析合集
C control refresh
Import data into Matlab
php数组函数大全
[daily training] 207 Class Schedule Card
【补题】2021牛客暑期多校训练营4-n
[deep learning lightweight backbone] 2022 edgevits CVPR