ACM-ICPC 2018 沈阳赛区网络预赛 J. Ka Chang(树状数组+分块)
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(树状数组+分块)的更多相关文章
- ACM-ICPC 2018 沈阳赛区网络预赛 J. Ka Chang (树分块)
题意:一个树,支持两种操作:1.将深度为L的节点权置加上X;2.求以x为根节点的子树上节点权置之和.根节点深度为0. 分析:考虑用树状数组维护节点权置,按dfs序下标查询.记录每个深度节点的个数.如果 ...
- ACM-ICPC 2018 沈阳赛区网络预赛 J. Ka Chang (分块思想)
题目链接:https://nanti.jisuanke.com/t/31451 题意: 给你一颗树,树上各点有初始权值,你有两种操作: 1. 给树中深度为l的点全部+x,(根节点为1,深度为0) 2. ...
- ACM-ICPC 2018 沈阳赛区网络预赛 J Ka Chang
Ka Chang 思路: dfs序+树状数组+分块 先dfs处理好每个节点的时间戳 对于每一层,如果这一层的节点数小于sqrt(n),那么直接按照时间戳在树状数组上更新 如果这一层节点个数大于sqrt ...
- ACM-ICPC 2018 沈阳赛区网络预赛 J. Ka Chang(树上分块+dfs序+线段树)
题意 链接:https://nanti.jisuanke.com/t/A1998 给出一个有根树(根是1),有n个结点.初始的时候每个结点的值都是0.下面有q个操作,操作有两种,操作1.将深度为L(根 ...
- ACM-ICPC 2018 徐州赛区网络预赛 G. Trace【树状数组维护区间最大值】
任意门:https://nanti.jisuanke.com/t/31459 There's a beach in the first quadrant. And from time to time, ...
- 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 ...
- ACM-ICPC 2018 徐州赛区网络预赛 J. Maze Designer (最大生成树+LCA求节点距离)
ACM-ICPC 2018 徐州赛区网络预赛 J. Maze Designer J. Maze Designer After the long vacation, the maze designer ...
- ACM-ICPC 2018 沈阳赛区网络预赛 K Supreme Number(规律)
https://nanti.jisuanke.com/t/31452 题意 给出一个n (2 ≤ N ≤ 10100 ),找到最接近且小于n的一个数,这个数需要满足每位上的数字构成的集合的每个非空子集 ...
- 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.cv2.matchTemplate(进行模板匹配) 2.cv2.minMaxLoc(找出矩阵最大值和最小值的位置(x,y)) 3.cv2.rectangle(在图像上画矩形)
1. cv2.matchTemplate(src, template, method) # 用于进行模板匹配 参数说明: src目标图像, template模板,method使用什么指标做模板的匹配 ...
- AS2在FLASH中调用EXE文件方法详细说明 已测试可行
熟悉FLASH功能的朋友都知道fscommand在FLASH中是一个经常用来控制窗口全屏或退出的命令,同时它也是FLASH调用外部可执行程序的一种方法,使用fscommand命令格式如下: fscom ...
- jsfl 导出指定名称的swf
var _openDOC = fl.openDocument("file:///E|TE/dt.fla"); var _exName = _openDOC.pathURI.spli ...
- vector 内存释放相关
在使用vector 存储char 类型时 new的数据 在vecto 执行rerase的时候或者 单项执行clear 内存是不能被释放的. 比如有下面的一个测试: class SnapshotInfo ...
- js删除dom节点时候索引出错问题
我们知道删除一个dom节点的时候索引就会发生了改变,甚至是错误,就算jq的ecah也无能为力,所以我们只能自己写个功能了 直接上代码把,不多说 <!DOCTYPE html> <ht ...
- KVM虚拟化技术(五)虚拟机管理
一.为了提高内存.硬盘.网络的性能,需要支持半虚拟化:virtio半虚拟化驱动 二.对虚拟机的管理都是通过libvirt:所有必须要启用一个守护程序libvirtd. 三.virt-manager ① ...
- 18.4 运行脚本 sudo ./launcher.sh 必须先给他权限 才能使用
我们发现我们每次启动服务都要打一大堆命令,ng build --watch ,nodemon server.js ,等等 凡是重复的工作,我们要可以代码完成 要想运行它呢 你必须将你运行的上述(脚本中 ...
- js字符串和控制语句
1.js的字符串 * 字符串* 字符串是js数据类型中的一种*字符串拼接:+,加号有两层含义* 1.数学中的加法运算;* 2.字符串连接,当加号的任意一边是一个字符串,那就是字符串连接的意思; < ...
- VS2012/VS2013配色方案
VS的配色方案下载地址 http://www.hanselman.com/blog/VisualStudioProgrammerThemesGallery.aspx 或者 http://studios ...
- 吴裕雄 11-MySQL查询数据
以下为在MySQL数据库中查询数据通用的 SELECT 语法:SELECT column_name,column_nameFROM table_name[WHERE Clause][LIMIT N][ ...