题面

传送门

分析

next_id = 1
id = array of length n filled with -1
visited = array of length n filled with false function dfs(v):
visited[v] = true
id[v] = next_id
next_id += 1
for to in neighbors of v in increasing order:
if not visited[to]:
dfs(to)

观察题目中的这段伪代码,发现实际上就是求出每个节点的DFS序,

注意for to in neighbors of v in increasing order:,要按编号从小到大访问每个节点,所以要对邻接表排序(可以用vector实现)

对询问离线,每个结点保存由该节点出发所有询问

第一次DFS,

求出每个点到根节点的距离,以及DFS序。顺便把每个节点的子树对应的DFS序范围求出,记为l[x],r[x]

用一棵线段树存储距离,第i个节点存储DFS序为i的树结点到当前询问节点的距离(初始询问节点为1)(注意到求的是到叶子节点的最近距离,所以把非叶子节点的值设为INF

第二次DFS,

当DFS到节点x时,线段树中存储的距离恰好是询问节点x到各节点的距离,

对于每个询问,直接在线段树上查询区间最小值即可

对从x到儿子y,我们需要更新线段树的值,将x到各节点的距离改成y到各节点的距离

设x到y的距离为len,发现对于y的子树中的节点,距离会减少len,而对于其他节点,距离会增加len

由于DFS序的性质,y子树中的节点的DFS序是连续的一段,所以我们只要在线段树上进行区间更新即可

更新完之后继续DFS y节点,回溯时记得把线段树恢复

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#define maxn 500005
#define INF 0x3f3f3f3f3f3f3f3f
using namespace std;
vector<pair<int,int> >E[maxn];
int n,t;
int cnt=0;
int l[maxn],r[maxn];
long long dis[maxn];
void dfs1(int x,int fa){
l[x]=++cnt;
for(int i=0;i<E[x].size();i++){
int y=E[x][i].first;
if(y!=fa){
dis[y]=dis[x]+E[x][i].second;
dfs1(y,x);
}
}
r[x]=cnt;
} struct node{
int l;
int r;
long long mark;
long long v;
}tree[maxn<<2];
void push_up(int pos){
tree[pos].v=min(tree[pos<<1].v,tree[pos<<1|1].v);
}
void build(int l,int r,int pos){
tree[pos].l=l;
tree[pos].r=r;
if(l==r){
tree[pos].mark=tree[pos].v=0;
return;
}
int mid=(l+r)>>1;
build(l,mid,pos<<1);
build(mid+1,r,pos<<1|1);
push_up(pos);
}
void push_down(int pos){
if(tree[pos].mark){
tree[pos<<1].mark+=tree[pos].mark;
tree[pos<<1|1].mark+=tree[pos].mark;
tree[pos<<1].v+=tree[pos].mark;
tree[pos<<1|1].v+=tree[pos].mark;
tree[pos].mark=0;
}
}
void update(int L,int R,long long v,int pos){
if(L<=tree[pos].l&&R>=tree[pos].r){
tree[pos].v+=v;
tree[pos].mark+=v;
return;
}
push_down(pos);
int mid=(tree[pos].l+tree[pos].r)>>1;
if(L<=mid) update(L,R,v,pos<<1);
if(R>mid) update(L,R,v,pos<<1|1);
push_up(pos);
}
long long query(int L,int R,int pos){
if(L<=tree[pos].l&&R>=tree[pos].r){
return tree[pos].v;
}
push_down(pos);
int mid=(tree[pos].l+tree[pos].r)>>1;
long long ans=INF;
if(L<=mid) ans=min(ans,query(L,R,pos<<1));
if(R>mid) ans=min(ans,query(L,R,pos<<1|1));
return ans;
} struct range{
int l;
int r;
long long ans;
int id;
range(){ }
range(int L,int R,int i){
l=L;
r=R;
id=i;
ans=0;
}
void debug(){
printf("[%d,%d]",l,r);
}
};
vector<range>q[maxn]; void dfs2(int x,int fa){
for(int i=0;i<q[x].size();i++){//处理询问
q[x][i].ans=query(q[x][i].l,q[x][i].r,1);
}
for(int i=0;i<E[x].size();i++){
int y=E[x][i].first;
int len=E[x][i].second;
if(y!=fa){
update(l[y],r[y],-len,1);//更新距离
if(l[y]>1) update(1,l[y]-1,len,1);//注意边界条件
if(r[y]<n) update(r[y]+1,n,len,1);
dfs2(y,x);
update(l[y],r[y],len,1);//记得把线段树恢复成原状
if(l[y]>1) update(1,l[y]-1,-len,1);
if(r[y]<n) update(r[y]+1,n,-len,1);
}
}
} long long ans[maxn];
int main(){
int u,v,w,x,ll,rr;
scanf("%d %d",&n,&t);
for(int i=2;i<=n;i++){
scanf("%d %d",&v,&w);
E[v].push_back(make_pair(i,w));
E[i].push_back(make_pair(v,w));
}
for(int i=1;i<=n;i++){
sort(E[i].begin(),E[i].end());
}
for(int i=1;i<=t;i++){
scanf("%d %d %d",&x,&ll,&rr);
q[x].push_back(range(ll,rr,i));
}
dfs1(1,0);
build(1,n,1);
for(int i=1;i<=n;i++){
if(l[i]==r[i]) update(l[i],l[i],dis[i],1);//如果不是叶子节点,距离要设为INF
else update(l[i],l[i],INF,1);
}
dfs2(1,0);
for(int i=1;i<=n;i++){
for(int j=0;j<q[i].size();j++){//按照输入顺序输出
ans[q[i][j].id]=q[i][j].ans;
}
}
for(int i=1;i<=t;i++){
printf("%I64d\n",ans[i]);
}
}

Codeforces 1110F(DFS序+线段树)的更多相关文章

  1. CodeForces 877E DFS序+线段树

    CodeForces 877E DFS序+线段树 题意 就是树上有n个点,然后每个点都有一盏灯,给出初始的状态,1表示亮,0表示不亮,然后有两种操作,第一种是get x,表示你需要输出x的子树和x本身 ...

  2. Codeforces 396C (DFS序+线段树)

    题面 传送门 题目大意: 给定一棵树,每个点都有权值,边的长度均为1,有两种操作 操作1:将节点u的值增加x,并且对于u的子树中的任意一个点v,将它的值增加x-dist(u,v)*k, dist(u, ...

  3. Codeforces 1132G(dfs序+线段树)

    题面 传送门 分析 对于每一个数a[i],找到它后面第一个大于它的数a[p],由p向i连边,最终我们就会得到一个森林,且p是i的父亲.为了方便操作,我们再增加一个虚拟节点n+1,把森林变成树. 由于序 ...

  4. Educational Codeforces Round 6 E dfs序+线段树

    题意:给出一颗有根树的构造和一开始每个点的颜色 有两种操作 1 : 给定点的子树群体涂色 2 : 求给定点的子树中有多少种颜色 比较容易想到dfs序+线段树去做 dfs序是很久以前看的bilibili ...

  5. Codeforces 343D Water Tree(DFS序 + 线段树)

    题目大概说给一棵树,进行以下3个操作:把某结点为根的子树中各个结点值设为1.把某结点以及其各个祖先值设为0.询问某结点的值. 对于第一个操作就是经典的DFS序+线段树了.而对于第二个操作,考虑再维护一 ...

  6. Codeforces Round #442 (Div. 2)A,B,C,D,E(STL,dp,贪心,bfs,dfs序+线段树)

    A. Alex and broken contest time limit per test 2 seconds memory limit per test 256 megabytes input s ...

  7. CodeForces 877E Danil and a Part-time Job(dfs序+线段树)

    Danil decided to earn some money, so he had found a part-time job. The interview have went well, so ...

  8. 【BZOJ-3252】攻略 DFS序 + 线段树 + 贪心

    3252: 攻略 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 339  Solved: 130[Submit][Status][Discuss] D ...

  9. BZOJ2434 [Noi2011]阿狸的打字机(AC自动机 + fail树 + DFS序 + 线段树)

    题目这么说的: 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机.打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母.经阿狸研究发现,这个打字机是这样工作的: 输入小 ...

随机推荐

  1. Java版基于SpringBoot+Vue.js实现自动创表自动定时采集(各个微信公众号商城产品进行采集)-爬虫篇

  2. 奇虎360的开源OpenResty Windows版本

    https://github.com/LomoX-Offical/nginx-openresty-windows

  3. Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) - B

    题目链接:http://codeforces.com/contest/831/problem/B 题意:给第2个26个字母并不重复的字符串(2个字符串对于一个映射),第1个字符串为key集合,第2个字 ...

  4. 树——binary-tree-maximum-path-sum(二叉树最大路径和)

    问题: Given a binary tree, find the maximum path sum. The path may start and end at any node in the tr ...

  5. c++函数参数或返回值为函数指针

    C++中函数指针的形式为:返回值类型 + 参数类型,函数没有值类型,但是却可以声明函数的指针,因为函数是可寻址的,存放在内存中的代码段,可以从指针访问. 函数指针可以声明为: void (*pF)(v ...

  6. [php代码审计] apache 后缀名解析“漏洞”

    不能说是漏洞,只是 apache 特性而已. 下面是apache  httpd.conf中截取的一段: <IfModule mime_module> # # TypesConfig poi ...

  7. Java文件处理之FileReader可输出中文字符

    import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public ...

  8. postman接口自动化测试之如何使用)

    postman 是一款强大网页调试工具的客户端,postman为用户提供强大的 Web API & HTTP 请求调试功能.postman能够发送任何类型的HTTP 请求 (GET, HEAD ...

  9. Xcode编辑器之快捷键的使用

    一,快捷键图标 图标 键盘 ⌘ Command ⌃ Control ⌥ Option ⇧ Shift 二, 常用快捷键 文件快捷键 快捷键 键盘  描述 ⌘N  command + N 新文件 ⇧⌘N ...

  10. 20180820-Java 抽象类

    Java 抽象类 在面向对象的概念中,所有的对象都是通过类来描绘的,但是反过来,并不是所有的类都是用来描绘对象的,如果一个类中没有包含足够的信息来描绘一个具体的对象,这样的类就是抽象类. 抽象类除了不 ...