题意

给定一棵无边权的树,最多只有一个点度数超过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. 蓝牙、WiFi、ZigBee三大无线通信技术协议模块哪一个是最好的?

    曾经,在2015年极客公园创新大会上,小米首次在非官方平台发布了新款产品小米智能家庭套装.自此,Zigbee便常出现在大众视野中. 如今,小米在IoT物联网应用开发者平台上明确说明,不再推广Zigbe ...

  2. 聊聊 C++ 和 C# 中的 lambda 玩法

    这几天在看 C++ 的 lambda 表达式,挺有意思,这个标准是在 C11标准 加进去的,也就是 2011 年,相比 C# 2007 还晚了个 4 年, Lambda 这东西非常好用,会上瘾,今天我 ...

  3. 第六章、PXE高效网络装机、Kickstart无人值守安装

    目录 一.部署PXE远程安装服务 1PXE定义 2PXE服务优点 3搭建网络体系前提条件 4PXE实现过程讲解 二.搭建PXE远程安装服务器 三.Kickstart无人值守安装 一.部署PXE远程安装 ...

  4. WPF开发随笔收录-报警闪烁效果实现

    一.前言 工作中目前经手的项目是医疗相关的监护软件,所以会涉及到一些报警效果的实现,今天在这里就简单分享一下实现方式 二.正文 1.实现的方式比较的简单,就是通过一个Border控件,然后搭配Data ...

  5. Android Studio 的初次使用

    记录我第一次使用Android Studio时遇到的问题以及一些简单的笔记. 我所使用的是Android Studio 2.2版本 遇到的问题 创建一个Hello World!项目无疑是相当简单的,我 ...

  6. Vue3 项目实战

    使用Vue3 开发一个小米商城 业务流程 登录---> 产品首页--->产品站--->产品详情 购物车--->订单确认--->订单支付--->订单列表 业务开发流程 ...

  7. 【小程序自动化Minium】三、元素定位- WXSS 选择器的使用

    最近更新略疲,主要是业余时间多了几个变化.比如忙活自己的模拟赛车驾舱升级.还跟朋友筹备一个小程序项目.另外早上的时间留给背单词了... 上一章中讲到Page接口的get_element()与get_e ...

  8. while循环--和do-while循环

    对于循环语句来说他会有一个回上去的箭头,这个回上去的箭头就形成了一个重复做的事情,那种重复做的事情我们就叫做循环 while循环 ~如果我们把while翻译作"当",那么一个whi ...

  9. 2 Zookeeper 单击安装

    (二)Zookeeper 本地模式安装 下载地址 镜像库地址:http://archive.apache.org/dist/zookeeper/ apache-zookeeper-3.6.0.tar. ...

  10. Solution -「2021.11.27」\Infty

    T1. 显然往 \(x < 0, y < 0\) 的点走一定不优. 根据转移式可发现 \(C(x, y)\) 即从 \((0, 0)\) 走到 \((x, y)\) 的方案数 \(\dbi ...