Given a rooted tree ( the root is node 1 ) of N nodes. Initially, each node has zero point.

Then, you need to handle Q operations. There're two types:

1 L X: Increase points by X of all nodes whose depth equals L ( the depth of the root is zero ). (x≤108)

2 X: Output sum of all points in the subtree whose root is X.

Input
Just one case.

The first lines contain two integer, N,Q(N≤105,Q≤105).

The next n−1 lines: Each line has two integer aaa,bbb, means that node aaa is the father of node b. It's guaranteed that the input data forms a rooted tree and node 1 is the root of it.

The next Q lines are queries.

Output
For each query 2, you should output a number means answer.

样例输入
3 3
1 2
2 3
1 1 1
2 1
2 3

样例输出
1
0

题意

给你一颗以1为根节点的数,有两个操作

1.层数为L的节点增加X

2.查询以X为根节点的子树总权和

题解

操作2很容易想到dfs维护树状数组

对于操作1,可以把层数按sqrt(n)分块,对于层数点数<=block的直接暴力更新,对于>block先保存进数组ans,查询的时候二分左右区间可以知道根的节点数,再乘ans,这样可以把单次操作的复杂度固定在sqrt(n)范围内

代码

 #include<bits/stdc++.h>
using namespace std; #define LL long long const int maxn=1e5+;
int s[maxn],e[maxn],tot,n;
LL sum[maxn],ans[maxn];
vector<int>G[maxn],deep[maxn],q;
void dfs(int u,int fa,int d)
{
s[u]=++tot;
deep[d].push_back(s[u]);
for(auto v:G[u])
{
if(v==fa)continue;
dfs(v,u,d+);
}
e[u]=tot;
}
int lowbit(int x)
{
return x&(-x);
}
void update(int x,int add)
{
for(int i=x;i<=n;i+=lowbit(i))
sum[i]+=add;
}
LL query(int x)
{
LL ans=;
for(int i=x;i>;i-=lowbit(i))
ans+=sum[i];
return ans;
}
int main()
{
int Q,op,L,x,u,v;
scanf("%d%d",&n,&Q);
int block=sqrt(n);
for(int i=;i<n;i++)
{
scanf("%d%d",&u,&v);
G[u].push_back(v);
G[v].push_back(u);
}
dfs(,-,);
for(int i=;i<n;i++)
if(deep[i].size()>block)q.push_back(i);
for(int i=;i<Q;i++)
{
scanf("%d",&op);
if(op==)
{
scanf("%d%d",&L,&x);
if(deep[L].size()>block)ans[L]+=x;
else
{
for(auto X:deep[L])
update(X,x);
}
}
else
{
scanf("%d",&x);
LL res=query(e[x])-query(s[x]-);
for(auto pos:q)
res+=ans[pos]*(upper_bound(deep[pos].begin(),deep[pos].end(),e[x])-lower_bound(deep[pos].begin(),deep[pos].end(),s[x]));
printf("%lld\n",res);
}
}
return ;
}

ACM-ICPC 2018 沈阳赛区网络预赛 J. Ka Chang(树状数组+分块)的更多相关文章

  1. ACM-ICPC 2018 沈阳赛区网络预赛 J. Ka Chang (树分块)

    题意:一个树,支持两种操作:1.将深度为L的节点权置加上X;2.求以x为根节点的子树上节点权置之和.根节点深度为0. 分析:考虑用树状数组维护节点权置,按dfs序下标查询.记录每个深度节点的个数.如果 ...

  2. ACM-ICPC 2018 沈阳赛区网络预赛 J. Ka Chang (分块思想)

    题目链接:https://nanti.jisuanke.com/t/31451 题意: 给你一颗树,树上各点有初始权值,你有两种操作: 1. 给树中深度为l的点全部+x,(根节点为1,深度为0) 2. ...

  3. ACM-ICPC 2018 沈阳赛区网络预赛 J Ka Chang

    Ka Chang 思路: dfs序+树状数组+分块 先dfs处理好每个节点的时间戳 对于每一层,如果这一层的节点数小于sqrt(n),那么直接按照时间戳在树状数组上更新 如果这一层节点个数大于sqrt ...

  4. ACM-ICPC 2018 沈阳赛区网络预赛 J. Ka Chang(树上分块+dfs序+线段树)

    题意 链接:https://nanti.jisuanke.com/t/A1998 给出一个有根树(根是1),有n个结点.初始的时候每个结点的值都是0.下面有q个操作,操作有两种,操作1.将深度为L(根 ...

  5. ACM-ICPC 2018 徐州赛区网络预赛 G. Trace【树状数组维护区间最大值】

    任意门:https://nanti.jisuanke.com/t/31459 There's a beach in the first quadrant. And from time to time, ...

  6. ACM-ICPC 2018 沈阳赛区网络预赛 J树分块

    J. Ka Chang Given a rooted tree ( the root is node 11 ) of NN nodes. Initially, each node has zero p ...

  7. ACM-ICPC 2018 徐州赛区网络预赛 J. Maze Designer (最大生成树+LCA求节点距离)

    ACM-ICPC 2018 徐州赛区网络预赛 J. Maze Designer J. Maze Designer After the long vacation, the maze designer ...

  8. ACM-ICPC 2018 沈阳赛区网络预赛 K Supreme Number(规律)

    https://nanti.jisuanke.com/t/31452 题意 给出一个n (2 ≤ N ≤ 10100 ),找到最接近且小于n的一个数,这个数需要满足每位上的数字构成的集合的每个非空子集 ...

  9. ACM-ICPC 2018 沈阳赛区网络预赛-K:Supreme Number

    Supreme Number A prime number (or a prime) is a natural number greater than 11 that cannot be formed ...

随机推荐

  1. elasticsearch-ik

    因lucene默认采用英文且英文通过空格就可以断句.而中文则是词组,如果不加载中文词库或插件则会变为一个一个字而非词组,因此需要加载中文词库. 不加分词库所看到的中文分词效果. post _analy ...

  2. linux系统基础入门

    使用工具:源码管理,自动部署,web服务器(linux) linux系统基础入门 1.下载地址 2.本文介绍的是一个基于Ubuntu的开源操作系统 下载优麒麟 Ubuntu是一个广泛应用于个人电脑,云 ...

  3. oracle 修改字符集 为ZHS16GBK

    一.oracle server 端 字符集查询 select userenv('language') from dual 其中NLS_CHARACTERSET 为server端字符集 NLS_LANG ...

  4. jquery_ajax 地址三级联动

    jquery 的三级地址联动,原理与javascript类似,只是在触发请求的时候,使用封装好的 $.get ,$post,$.ajax 方法去执行,其余的都是一样的,后台服务器请求文件是一样的,前台 ...

  5. C++中类的多继承

    在写这一主题的文章之前,在网上找到一篇很非常好的文章C++之继承与多态.就没有必要做重复造轮子的事件了,那就从这篇文章开始吧! 在c++中一个类可以从多个基类中派生(即可以有多个父类),这就是多继承. ...

  6. 何谓domReady

    我的博客已经写过好几篇如何实现domReady的文章,最近做培训,面向新手们,需要彻彻底底向他们说明这个东西,于是就有了这篇文章. 我们经常看人们用 document.getElementById(& ...

  7. 浅谈MySQL事务及隔离级别

    目录 1.什么是事务 2.事务的ACID属性 2-1.原子性(Atomicity) 2-2.一致性(Consistency) 2-3.隔离性(Isolation) 2-4.持久性(Durability ...

  8. Android构建项目时出现的小bug们(2018年5月19日19:31:20)

    问题详情 Error:Execution failed for task ':app:preDebugAndroidTestBuild'. > Conflict with dependency ...

  9. 我理解的BFC(块级格式化上下文)

    BFC(Block formatting context) 直译为"块级格式化上下文". BFC它是一个独立的渲染区域,只有Block-level box参与, 它规定了内部的Bl ...

  10. C++ 将数据转为字符串的几种方法

    收集一下: 1\将int 转为 LPCTSTR 其实LPCTSTR可以直接使用CString直接代替,无需类型强制转换 CString str; ; //str="15" str. ...