Codeforces Round #169 (Div. 2) E. Little Girl and Problem on Trees dfs序+线段树
2 seconds
256 megabytes
standard input
standard output
A little girl loves problems on trees very much. Here's one of them.
A tree is an undirected connected graph, not containing cycles. The degree of node x in the tree is the number of nodes y of the tree, such that each of them is connected with node x by some edge of the tree.
Let's consider a tree that consists of n nodes. We'll consider the tree's nodes indexed from 1 to n. The cosidered tree has the following property: each node except for node number 1 has the degree of at most 2.
Initially, each node of the tree contains number 0. Your task is to quickly process the requests of two types:
- Request of form: 0 v x d. In reply to the request you should add x to all numbers that are written in the nodes that are located at the distance of at most d from node v. The distance between two nodes is the number of edges on the shortest path between them.
- Request of form: 1 v. In reply to the request you should print the current number that is written in node v.
The first line contains integers n (2 ≤ n ≤ 105) and q (1 ≤ q ≤ 105) — the number of tree nodes and the number of requests, correspondingly.
Each of the next n - 1 lines contains two integers ui and vi (1 ≤ ui, vi ≤ n, ui ≠ vi), that show that there is an edge between nodes uiand vi. Each edge's description occurs in the input exactly once. It is guaranteed that the given graph is a tree that has the property that is described in the statement.
Next q lines describe the requests.
- The request to add has the following format: 0 v x d (1 ≤ v ≤ n, 1 ≤ x ≤ 104, 1 ≤ d < n).
- The request to print the node value has the following format: 1 v (1 ≤ v ≤ n).
The numbers in the lines are separated by single spaces.
For each request to print the node value print an integer — the reply to the request.
3 6
1 2
1 3
0 3 1 2
0 2 3 1
0 1 5 2
1 1
1 2
1 3
9
9
6
6 11
1 2
2 5
5 4
1 6
1 3
0 3 1 3
0 3 4 5
0 2 1 4
0 1 5 5
0 4 6 2
1 1
1 2
1 3
1 4
1 5
1 6
11
17
11
16
17
11
题意:给你一棵树,除了点1以为的点最多的度数为2,0表示以某个点v为半径d内的点权值+x
1表示查询v的权值大小;
思路:这题有一个关键点,就是除了1以为的度数最大为2,则表示如果以1为根的话,每条都是直链,没有开叉;
对于直链的话,可以进行dfs序标号,更新一条链的话就可以区间更新;
对于半径范围内的话,表示到根的距离减去d,以1为中心半径进行求解;
不能一条一条的去暴力链,开两个线段树,一个暴力一条链,一个存半径;
=_=,详见代码;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
#define bug(x) cout<<"bug"<<x<<endl;
const int N=1e5+,M=1e6+,inf=;
const ll INF=1e18+,mod=;
struct is
{
int v,next;
} edge[N<<];
int head[N],edg,dep[N];
int in[N],out[N],tot;
void init()
{
memset(head,-,sizeof(head));
edg=;
tot=;
}
void add(int u,int v)
{
edg++;
edge[edg].v=v;
edge[edg].next=head[u];
head[u]=edg;
}
void dfs(int u,int fa,int deep)
{
dep[u]=deep;
tot++;
in[u]=tot;
for(int i=head[u]; i!=-; i=edge[i].next)
{
int v=edge[i].v;
if(v==fa)continue;
dfs(v,u,deep+);
}
out[u]=tot;
}
struct linetree
{
int maxx[N<<],lazy[N<<];
void pushup(int pos)
{
maxx[pos]=max(maxx[pos<<],maxx[pos<<|]);
}
void pushdown(int pos)
{
if(lazy[pos])
{
int x=lazy[pos];
lazy[pos<<]+=x;
lazy[pos<<|]+=x;
maxx[pos<<|]+=x;
maxx[pos<<]+=x;
lazy[pos]=;
}
}
void build(int l,int r,int pos)
{
if(l==r)
{
maxx[pos]=;
lazy[pos]=;
return;
}
int mid=(l+r)>>;
build(l,mid,pos<<);
build(mid+,r,pos<<|);
pushup(pos);
}
void update(int L,int R,int c,int l,int r,int pos)
{
if(L<=l&&r<=R)
{
maxx[pos]+=c;
lazy[pos]+=c;
return;
}
pushdown(pos);
int mid=(l+r)>>;
if(L<=mid)
update(L,R,c,l,mid,pos<<);
if(R>mid)
update(L,R,c,mid+,r,pos<<|);
pushup(pos);
}
int query(int p,int l,int r,int pos)
{
if(l==r)return maxx[pos];
pushdown(pos);
int mid=(l+r)>>;
if(p<=mid)
return query(p,l,mid,pos<<);
else return query(p,mid+,r,pos<<|);
}
};
linetree tree,tree2;
int main()
{
init();
int n,q;
scanf("%d%d",&n,&q);
for(int i=; i<n; i++)
{
int u,v;
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
dfs(,-,);
tree.build(,n,);
tree2.build(,n,);
while(q--)
{
int flag;
scanf("%d",&flag);
if(flag)
{
int x;
scanf("%d",&x);
if(x!=)
printf("%d\n",tree.query(in[x],,n,)+tree2.query(dep[x],,n,));
else
printf("%d\n",tree.query(in[],,n,));
}
else
{
int v,x,d;
scanf("%d%d%d",&v,&x,&d);
if(v==)
{
tree.update(,,x,,n,);
tree2.update(,d,x,,n,);
}
else if(dep[v]<d)
{
int L=d-dep[v];
int l=in[v]-dep[v]+L+;
int r=min(in[v]+d,out[v]);
tree2.update(,L,x,,n,);
tree.update(,,x,,n,);
if(l<=r)tree.update(l,r,x,,n,);
}
else if(dep[v]==d)
{
int L=in[v]-d+,R=min(out[v],in[v]+d);
tree.update(L,R,x,,n,);
tree.update(,,x,,n,);
}
else
{
int L=in[v]-d,R=min(out[v],in[v]+d);
tree.update(L,R,x,,n,);
}
}
}
return ;
}
Codeforces Round #169 (Div. 2) E. Little Girl and Problem on Trees dfs序+线段树的更多相关文章
- Codeforces Round #535 (Div. 3) E2. Array and Segments (Hard version) 【区间更新 线段树】
传送门:http://codeforces.com/contest/1108/problem/E2 E2. Array and Segments (Hard version) time limit p ...
- Codeforces Round #539 (Div. 1) C. Sasha and a Patient Friend 动态开点线段树
题解看这里 liouzhou_101的博客园 更简洁的代码看这里: #include <bits/stdc++.h> using namespace std; typedef long l ...
- Codeforces Round #539 (Div. 1) 1109F. Sasha and Algorithm of Silence's Sounds LCT+线段树 (two pointers)
题解请看 Felix-Lee的CSDN博客 写的很好,不过最后不用判断最小值是不是1,因为[i,i]只有一个点,一定满足条件,最小值一定是1. CODE 写完就A,刺激. #include <b ...
- codeforces 633G. Yash And Trees dfs序+线段树+bitset
题目链接 G. Yash And Trees time limit per test 4 seconds memory limit per test 512 megabytes input stand ...
- CodeForces 877E DFS序+线段树
CodeForces 877E DFS序+线段树 题意 就是树上有n个点,然后每个点都有一盏灯,给出初始的状态,1表示亮,0表示不亮,然后有两种操作,第一种是get x,表示你需要输出x的子树和x本身 ...
- Codeforces Round #622 (Div. 2) A. Fast Food Restaurant(全排列,DFS)
Codeforces Round #622 (Div. 2) A. Fast Food Restaurant 题意: 你是餐馆老板,虽然只会做三道菜,上菜时还有个怪癖:一位客人至少上一道菜,且一种菜最 ...
- Codeforces 838B - Diverging Directions - [DFS序+线段树]
题目链接:http://codeforces.com/problemset/problem/838/B You are given a directed weighted graph with n n ...
- Codeforces Round #169 (Div. 2)
A. Lunch Rush 模拟. B. Little Girl and Game 因为可以打乱顺序,所以只关心每种数字打奇偶性. 若一开始就是回文,即奇数字母为0或1种,则先手获胜. 若奇数字母大于 ...
- Codeforces Round #169 (Div. 2) A水 B C区间更新 D 思路
A. Lunch Rush time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
随机推荐
- JQuery操作Select标签
jQuery获取Select选择的Text和Value: 1. $("#select_id").change(function(){//code...}); //为Select添加 ...
- 【BZOJ4195】[Noi2015]程序自动分析 并查集
[BZOJ4195][Noi2015]程序自动分析 Description 在实现程序自动分析的过程中,常常需要判定一些约束条件是否能被同时满足. 考虑一个约束满足问题的简化版本:假设x1,x2,x3 ...
- ios8 gps定位不好用
这样让iOS8正常使用定位功能呢? <1>你需要在info.plist表里面添加两条变量 在Info.plist中加入两个缺省没有的字段 NSLocationAlwaysUsageDesc ...
- mysql 把表中某一列的内容合并为一行
1,把表中某一列的内容合并为一行 select province,CONCAT('[\"全部\",\"',GROUP_CONCAT(city ORDER BY cityI ...
- 从零打造在线网盘系统之Hibernate配置O/R映射
欢迎浏览Java工程师SSH教程从零打造在线网盘系统系列教程,本系列教程将会使用SSH(Struts2+Spring+Hibernate)打造一个在线网盘系统,本系列教程是从零开始,所以会详细以及着重 ...
- Centos7更改yum源与更新系统
[1] 首先备份 /etc/yum.repos.d/CentOS-Base.repo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/Cen ...
- Windows安装使用git
下载安装Windows安装文档Git-2.16.2-64-bit双击安装(安装过程不详述) 打开git客户端 新建代码命令 mkdir /c/code 进入该目录(对应windows的c盘下面的目录) ...
- thinkphp中 volist循环的 mod取值的问题
<ul> <volist name="data" id="arr" key="k" mod="2"&g ...
- WebSocket client for python
Project description websocket-client module is WebSocket client for python. This provide the low lev ...
- Word 2010文档自动生成目录和某页插入页码
一.Word 2010文档自动生成目录 关于Word文档自动生成目录一直是我身边同学们最为难的地方,尤其是毕业论文,经常因为目录问题,被要求修改,而且每次修改完正文后,目录的内容和页码可能都会发生变化 ...