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 ...
随机推荐
- Egret3D初步学习笔记三 (角色使用)
一 Unity中编辑角色 仍然使用unity4.7.1_Egret3D_Dll.unitypackage. 里面含有一个角色. 二 查看人物的动画 选中lingtong 属性面板里有个Animator ...
- AVG
AVG([ DISTINCT | ALL ] expr) [ OVER(analytic_clause) ] SELECT MANAGER_ID, LAST_NAME, ...
- fiddler 面板内显示IP地址
1.打开fiddler, 快捷键Ctrl+R (菜单->Rules->Customize Rules…) 然后在CustomRules.js文件里Ctrl+F查找字符串:static f ...
- pta 天梯地图 (Dijkstra)
本题要求你实现一个天梯赛专属在线地图,队员输入自己学校所在地和赛场地点后,该地图应该推荐两条路线:一条是最快到达路线:一条是最短距离的路线.题目保证对任意的查询请求,地图上都至少存在一条可达路线. 输 ...
- 洛谷P3809 后缀排序【后缀数组】【模板】
题目背景 这是一道模板题. 题目描述 读入一个长度为 nn 的由大小写英文字母或数字组成的字符串,请把这个字符串的所有非空后缀按字典序从小到大排序,然后按顺序输出后缀的第一个字符在原串中的位置.位置编 ...
- Oracle等待事件之db file sequential read/ db file parallel read
1.产生原因 db file sequential read这个是非常常见的I/O 相关的等待事件.表示发生了与索引扫描相关的等待.意味着I/O 出现了问题,通常表示I/O竞争或者I/O 需求太多. ...
- 广通软件荣获“2016年度ITSS优秀会员”称号
1月12日,为了表彰在IT服务标准研制和应用推广工作中所做出的贡献,中国电子工业标准化技术协会信息技术服务分会(以下称ITSS分会)在北京召开“2016年度ITSS优秀会员”专家评选活动,广通软件获得 ...
- hive引入jar包--HIVE.AUX.JARS.PATH和hive.aux.jars.path
hive需要引入包时?该怎么引入? 一.hive-site.xml中的hive.aux.jars.path 此配置项对于hive server有效,但是是不会作用到hive shell.也就是说即使你 ...
- 002-原始jpa以及基本加载过程,基本sql使用
一.概述 1.1.创建 public void createMethod() { EntityManagerFactory factory = Persistence.createEntityMana ...
- 第六章 优化服务器设置--高性能MySQL 施瓦茨--读书笔记
MySql的默认配置不适用于使用大量资源,因为其通用性很高. 不要期望改变配置文件会带来巨大的性能提升.提升大小取决于工作负载,通常可以通过选择适当的配置参数得到两到三倍的性能提升.在这时候,性能提升 ...