当前位置:网站首页>Luogu p3313 [sdoi2014] Travel Solution
Luogu p3313 [sdoi2014] Travel Solution
2022-07-21 19:33:00 【CofDoria】
Portal :[P3313 [SDOI2014] travel ]
1 0 5 10^5 105 Dynamic opening point of a segment tree + The tree chain splits
I'll write the detailed ideas later Attention to detail
–update–
Use the line segment tree writing method of dynamic open points to build the line segment tree of each belief . Press dfs \text{dfs} dfs Order to store ( Corresponding to dfn \text{dfn} dfn Array ).
use u p d a t e update update Function to update the value in the segment tree , Because it's a single point of modification , So no need p u s h d o w n pushdown pushdown operation , and p u s h u p pushup pushup it is desirable that .
When u p d a t e update update In the process, it is found that the direction that needs recursion has no child nodes , We'll start with sgt \text{sgt} sgt Apply for a child node in the array , Then continue to recurse downward .
- When you encounter the operation of modifying faith , take c x c_x cx On the tree of belief segment x x x The node is set to 0 0 0, And then y y y On the tree of belief segment x x x The node is updated to w x w_x wx.
- When it comes to modifying the rating , Direct will c x c_x cx On the line tree x x x The node is updated to w w w.
- When querying, directly insert the tree chain and divide it into dfs Use the segment tree to query the sequence .
Accepted code
#include <bits/stdc++.h>
using namespace std;
#define maxn 100005
#pragma gcc optimize(2)
int n,q;
string opt;
struct node{
int l,r,sum,max;
}sgt[maxn*32*32];
vector<int>mp[maxn];
int w[maxn],c[maxn];
int fa[maxn],sz[maxn],dep[maxn],dfn[maxn],son[maxn],top[maxn];
void dfs1(int u,int f,int d){
fa[u]=f;
dep[u]=d;
sz[u]=1;
son[u]=0;
sz[son[u]]=0;
for(auto v:mp[u]){
if(v==f) continue;
dfs1(v,u,d+1);
sz[u]+=sz[v];
if(sz[v]>sz[son[u]]) son[u]=v;
}
}
int tot=0;
void dfs2(int u,int f,int t){
top[u]=t;
dfn[u]=++tot;
if(son[u]){
dfs2(son[u],u,t);
for(auto v:mp[u]){
if(v==f) continue;
if(v==son[u]) continue;
dfs2(v,u,v);
}
}
}
void pushup(int d){
sgt[d].sum=0,sgt[d].max=0;
if(sgt[d].l){
sgt[d].max=max(sgt[d].max,sgt[sgt[d].l].max);
sgt[d].sum=sgt[d].sum+sgt[sgt[d].l].sum;
}
if(sgt[d].r){
sgt[d].max=max(sgt[d].max,sgt[sgt[d].r].max);
sgt[d].sum=sgt[d].sum+sgt[sgt[d].r].sum;
}
}
int cnt=maxn;
void update(int d,int pos,int val,int l,int r){
if(l==r){
sgt[d].sum=sgt[d].max=val;
return ;
}
int mid=l+r>>1;
if(pos<=mid){
if(!sgt[d].l) sgt[d].l=++cnt;
update(sgt[d].l,pos,val,l,mid);
}else{
if(!sgt[d].r) sgt[d].r=++cnt;
update(sgt[d].r,pos,val,mid+1,r);
}
pushup(d);
}
int query(int d,int x,int y,int l,int r,int ver){
if(x<=l&&r<=y) return ver?sgt[d].max:sgt[d].sum;
int mid=l+r>>1;
int ret=0;
if(x<=mid&&sgt[d].l) ret=(ver?max(ret,query(sgt[d].l,x,y,l,mid,ver)):ret+query(sgt[d].l,x,y,l,mid,ver));
if(y>mid&&sgt[d].r) ret=(ver?max(ret,query(sgt[d].r,x,y,mid+1,r,ver)):ret+query(sgt[d].r,x,y,mid+1,r,ver));
return ret;
}
void init(){
for(int i=1;i<=n;i++){
update(c[i],dfn[i],w[i],1,n);
}
}
int query(int x,int y,int ver){
int color=c[x];
int ret=0;
while(top[x]!=top[y]){
if(dep[top[x]]<dep[top[y]]) swap(x,y);
if(ver) ret=max(ret,query(color,dfn[top[x]],dfn[x],1,n,ver));
else ret=ret+query(color,dfn[top[x]],dfn[x],1,n,ver);
x=fa[top[x]];
}
if(dep[x]>dep[y]) swap(x,y);
if(ver) ret=max(ret,query(color,dfn[x],dfn[y],1,n,ver));
else ret=ret+query(color,dfn[x],dfn[y],1,n,ver);
return ret;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>q;
for(int i=1;i<=n;i++){
cin>>w[i]>>c[i];
}
for(int i=1,u,v;i<=n-1;i++){
cin>>u>>v;
mp[u].push_back(v);
mp[v].push_back(u);
}
dfs1(1,0,1);
dfs2(1,0,1);
init();
int x,y;
while(q--){
cin>>opt;
if(opt=="CC"){
cin>>x>>y;
update(c[x],dfn[x],0,1,n);
update(y,dfn[x],w[x],1,n);
c[x]=y;
}else if(opt=="CW"){
cin>>x>>y;
update(c[x],dfn[x],y,1,n);
w[x]=y;
}else if(opt=="QS"){
cin>>x>>y;
cout<<query(x,y,0)<<'\n';
}else if(opt=="QM"){
cin>>x>>y;
cout<<query(x,y,1)<<'\n';
}
}
}
边栏推荐
- MySQL installation prompts that the application cannot start normally (0xc000007b, how to solve it?
- ClickHouse深度揭秘
- Verilog grammar basics HDL bits training 03
- List container series operations (detailed)
- 分布式.常用架构和服务拆分
- Metahuman face shader summary
- Distributed load balancing
- MySQL进阶(B)
- Implementation method of SuperMap iclient for openlayers layer group control
- 其他正则验证规则web launch app
猜你喜欢
Detailed explanation of at mode principle of Seata (3)
分布式.常用架构和服务拆分
四种bean拷贝工具对比
C# 数字信号处理工具包 DSP-Core 重采样(Resample)输出点数是多少
Okaleido tiger NFT is about to log in to binance NFT platform, and the era of NFT rights and interests is about to start
JWT (JSON web token) authentication: the most popular cross domain authentication solution at present
Distributed High concurrency & high availability
解决报错:Uncaught TypeError: Cannot read properties of undefined (reading ‘install‘)
分布式.负载均衡
蚓激酶白蛋白纳米粒/红细胞膜定向包裹血红蛋白-白蛋白纳米粒的研究制备
随机推荐
蓝灯绿灯按时明灭,编程古鲁的密语
Loop structure -- while loop and do while loop
Hcip day 4
Distributed High availability and high scalability index
Lombok简化开发
好消息!PMP项目管理证书列入急需紧缺专业人才人员
MYSQL06_ Seven join operations and union all of sql99
Distributed High concurrency & high availability
Software testing interview question: what do you think are the advantages of testing?
Software test interview question: bug management tool tracking process (using bugzilla as an example)
读书笔记:《次第花开》
Preparation of hemoglobin albumin nanoparticles encapsulated by Lumbrokinase albumin nanoparticles / erythrocyte membrane
[introduction to QT] signal slot mechanism
Model模态框点击其他除了模态框区域不消失
除去不必要的字段
strcspn、strchr特殊字符校验
Installing redis in Linux
Web APIs DOM- 网页特效篇-元素大小和位置
L1-003 个位数统计
解决報錯:Uncaught TypeError: Cannot read properties of undefined (reading ‘install‘)