题意

给定一棵无边权的树,最多只有一个点度数超过2,有两种操作

1)(0 u x d)将距离u节点d距离之内的节点的值加上x

2)(1 u)询问u节点的值

n<=100000,q<=100000

题解

只有一个点度数超过2,那么把它当根,整棵树的形态就是从根开始向下延伸许多链,

将距离u节点d距离之内的节点的值加上x,放在u的子树内(应该叫子链吧?)就是区间修改,可以用线段树或树状数组维护这每一条链,

如果距离u节点d距离之内的节点包括根的话,就要先把u到根的路径加上x,然后把距离根节点d - dis(u,root) 距离之内的其它节点的值加上x,很麻烦

此时需要单独拿一个数据结构维护从根开始延伸的贡献,下标为 i 的数记录深度为 i 的点共同增加过的x之和,于是 “ 把距离根节点d - dis(u,root) 距离之内的其它节点的值加上x ” 就可以在 [1, d - dis(u,root) +1] 上增加x(设根的深度为1),然后在u所在的链上去掉重复的部分

当我们访问一个点的权值时,除了要加上它所在的链上的权值,还要加上对应深度的共同权值。

根要特判。

CODE

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<algorithm>
#define MAXN 100005
#define ENDL putchar('\n')
#define LL long long
#define DB double
#define lowbit(x) ((-x)&(x))
//#define int LL
//#pragma GCC optimize(2)
using namespace std;
inline LL read() {
LL f = 1,x = 0;char s = getchar();
while(s < '0' || s > '9') {if(s == '-')f = -1;s = getchar();}
while(s >= '0' && s <= '9') {x = x * 10 + (s - '0');s = getchar();}
return x * f;
}
const int jzm = 1000000007;
int n,m,i,j,s,o,k,root;
int ind[MAXN];
vector<int> g[MAXN];
int dfn[MAXN],d[MAXN],id[MAXN],tl[MAXN],cnt;
int c[MAXN],t[MAXN];
void addt(int x,int y) {while(x <= n) t[x] += y,x += lowbit(x);return ;}
void addc(int x,int y) {while(x <= n) c[x] += y,x += lowbit(x);return ;}
int sumt(int x) {int as=0;while(x>0) as += t[x],x -= lowbit(x);return as;}
int sumc(int x) {int as=0;while(x>0) as += c[x],x -= lowbit(x);return as;}
void dfs(int x,int fa) {
d[x] = d[fa] + 1;
dfn[x] = ++ cnt;
id[cnt] = x;
tl[x] = cnt;
for(int i = 0;i < g[x].size();i ++) {
if(g[x][i] != fa) {
dfs(g[x][i],x);
tl[x] = max(tl[x],tl[g[x][i]]);
}
}
return ;
}
int main() {
n = read();m = read();
root = 1;
for(int i = 1;i < n;i ++) {
s = read();o = read();
ind[s] ++;ind[o] ++;
g[s].push_back(o);
g[o].push_back(s);
if(ind[s] > 2) root = s;
if(ind[o] > 2) root = o;
}
dfs(root,0);
for(int i = 1;i <= m;i ++) {
k = read();
if(!k) {
s = read();k = read();o = read();
if(s == root) {
addc(1,k);
addc(2+o,-k);
// cout<<"ok"<<endl;
}
else {
bool flag = (d[s] <= o+1);
int dis = o - d[s] + 1;
int l,r;
if(flag) l = dfn[s] - d[s] + 2 + dis;
else l = dfn[s] - o;
r = min(tl[s],dfn[s] + o);
if(l <= r) addt(l,k),addt(r+1,-k);
if(flag) addc(1,k),addc(2+dis,-k);
// cout<<"OK"<<endl;
}
}
else {
s = read();
printf("%d\n",sumt(dfn[s]) + sumc(d[s]));
}
}
return 0;
}

Little Girl and Problem on Trees的更多相关文章

  1. Codeforces Round #169 (Div. 2) E. Little Girl and Problem on Trees dfs序+线段树

    E. Little Girl and Problem on Trees time limit per test 2 seconds memory limit per test 256 megabyte ...

  2. Trees on the level(指针法和非指针法构造二叉树)

    Trees on the level Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  3. hdu 1622 Trees on the level(二叉树的层次遍历)

    题目链接:https://vjudge.net/contest/209862#problem/B 题目大意: Trees on the level Time Limit: 2000/1000 MS ( ...

  4. Codeforces Round #169 (Div. 2)

    A. Lunch Rush 模拟. B. Little Girl and Game 因为可以打乱顺序,所以只关心每种数字打奇偶性. 若一开始就是回文,即奇数字母为0或1种,则先手获胜. 若奇数字母大于 ...

  5. CF959C Mahmoud and Ehab and the wrong algorithm 构造

    Mahmoud was trying to solve the vertex cover problem on trees. The problem statement is: Given an un ...

  6. Codeforces Round #473 (Div. 2)

    A. Mahmoud and Ehab and the even-odd game time limit per test 1 second memory limit per test 256 meg ...

  7. [LeetCode] 366. Find Leaves of Binary Tree 找二叉树的叶节点

    Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...

  8. HDU p1294 Rooted Trees Problem 解题报告

    http://www.cnblogs.com/keam37/p/3639294.html keam所有 转载请注明出处 Problem Description Give you two definit ...

  9. [LeetCode&Python] Problem 872. Leaf-Similar Trees

    Consider all the leaves of a binary tree.  From left to right order, the values of those leaves form ...

随机推荐

  1. AtCoder ABC 242 题解

    AtCoder ABC 242 题解 A T-shirt 排名前 \(A\) 可得 T-shirt 排名 \([A+1,B]\) 中随机选 \(C\) 个得 T-shirt 给出排名 \(X\) ,求 ...

  2. 2 万字 + 20张图| 细说 Redis 九种数据类型和应用场景

    作者:小林coding 计算机八股文网(操作系统.计算机网络.计算机组成.MySQL.Redis):https://xiaolincoding.com 大家好,我是小林. 我们都知道 Redis 提供 ...

  3. PHP时间轴函数

    PHP时间轴函数可以更好的去进行用户体验.让用户动态的知道最近是什么时候,而不是死板的datatime去转换成固定的时间. 后续版本会考虑添加这个功能,代码先贴出来. function tranTim ...

  4. .Net CLR GC动态获取函数头地址,C++的骚操作(慎入)

    前言: 太懒了,从没有在这里正儿八经的写过文章.看到一些人的高产,真是惭愧.决定稍微变得不那么懒.如有疏漏,请指正. .net的GC都谈的很多了,本篇主要是剑走偏锋,聊聊一些个人认为较为核心的细节方面 ...

  5. 25.MYsql数据库管理

    MYsql数据库管理 目录 MYsql数据库管理 数据库基本操作 库和表 常用的数据类型 查看数据表结构 查看当前服务器的数据库 查看数据库中包含的表 查看表的结构 SQL语句 创建及删除数据库和表 ...

  6. JS:相等判断

    1.=  赋值运算符 错误写法:a+b = c; 2.== :=== ==判断值是否相等 例: var a = 2; var b = 3; var c = a+b; var d = "2&q ...

  7. 《Java笔记——基础语法》

    Java笔记--基础语法       一.字符串的拼接: 例如: System.out.println(""+"");     二.换行语句: 例如: Syst ...

  8. Elasticsearch 在地理信息空间索引的探索和演进

    vivo 互联网服务器团队- Shuai Guangying 本文梳理了Elasticsearch对于数值索引实现方案的升级和优化思考,从2015年至今数值索引的方案经历了多个版本的迭代,实现思路从最 ...

  9. Linux YUM yum 命令详解

    Yum命令 常用yum命令列表 command is one of: * install package1 [package2] [...] * update [package1] [package2 ...

  10. vscode的安装、切换为中文简体、集成sass

    VScode设置中文 打开vscode ,按快捷键"Ctrl+Shift+P" 输入configure language,回车 选择安装其他语言 (默认是英文的) 选择简体中安装( ...