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 ...
随机推荐
- c# Use NAudio Library to Convert MP3 audio into WAV audio(将Mp3格式转换成Wav格式)
Have you been in need of converting mp3 audios to wav audios? If so, the skill in this article prov ...
- JFinal的启动源码解读
本文对Jfinal的启动源码做解释说明. PS:Jfinal启动容器可基于Tomcat/Jetty等web容器启动,本文基于Jetty的启动方式做启动源码的解读和分析,tomcat类似. 入口 JF ...
- 豆瓣API接口开发,结合angularJS来做,感觉爽歪歪!
第一次做还是先说下API 是什么鬼? API : application program interface 应用程序编程接口: 有那些常见的API: webAPI : 通过WEB方式提供结构叫 WE ...
- <select>里动态添加option
因为是转载文章 在此标明出处,以前有文章是转的没标明的请谅解,因为有些已经无法找到出处,或者与其它原因. 如有冒犯请联系本人,或删除,或标明出处. 因为好的文章,以前只想收藏,但连接有时候会失效,所以 ...
- Network of Schools---poj1236(强连通分量)
题目链接 题意:学校有一些单向网络,现在需要传一些文件 求:1,求最少需要向几个学校分发文件才能让每个学校都收到, 2,需要添加几条网络才能从任意一个学校分发都可以传遍所有学校. 解题思路(参考大神的 ...
- uchome 不从缓存中读取模板
/source/function_common.php中的代码 //模板调用 function template($name) { global $_SCONFIG, $_SGLOBAL; if($_ ...
- 002-spring cache 基于声明式注解的缓存-01-Cacheable annotation
一.简述 对于缓存声明,抽象提供了一组Java注解: @Cacheable触发缓存填充(这里一般放在创建和获取的方法上) @CacheEvict触发缓存驱逐(用于删除的方法上) @CachePut更新 ...
- Spark Streaming带状态更新
带状态的更新是使用的updateStateByKey方法,里面传入一个函数,函数要自己写,注意需要设置checkpoint import org.apache.spark.streaming.kafk ...
- POJ1995:Raising Modulo Numbers(快速幂取余)
题目:http://poj.org/problem?id=1995 题目解析:求(A1B1+A2B2+ ... +AHBH)mod M. 大水题. #include <iostream> ...
- Docker学习笔记(一):在本地安装和配置Docker
由于公司里测试服务器时常会有变动,每次变动之后都需要在新的服务器上配置一遍环境,实在是麻烦.后来我突然想到了在网上看到的资料中说Docker能快速部署可移植的容器,所以我就试着用Docker搭建了 ...