ACM-ICPC 2018 沈阳赛区网络预赛 J树分块
J. Ka Chang
Given a rooted tree ( the root is node 11 ) of NN nodes. Initially, each node has zero point.
Then, you need to handle QQ operations. There're two types:
1\ L\ X1 L X: Increase points by XX of all nodes whose depth equals LL ( the depth of the root is zero ). (x \leq 10^8)(x≤108)
2\ X2 X: Output sum of all points in the subtree whose root is XX.
Input
Just one case.
The first lines contain two integer, N,QN,Q. (N \leq 10^5, Q \leq 10^5)(N≤105,Q≤105).
The next n-1n−1 lines: Each line has two integer aa,bb, means that node aa is the father of node bb. It's guaranteed that the input data forms a rooted tree and node 11 is the root of it.
The next QQ lines are queries.
Output
For each query 22, you should output a number means answer.
样例输入复制
3 3
1 2
2 3
1 1 1
2 1
2 3
样例输出复制
1
0
题目来源
首先这是一个原题,你需要更新深度为x的值,每次仅仅查询子树就可以,用线段树的话,需要更新的太多的
要树分块,打上time标记,每个数据太多不能暴力更新,单独列出来
#include<bits/stdc++.h>
using namespace std;
const int N=;
vector<int>G[N];
vector<int>pos[N];
vector<int>vec;
int L[N],R[N];
int tot,n,m,lim=;
long long s[N],c[N];
void dfs(int now,int deep)
{
L[now]=++tot;//一块的左标记
pos[deep].push_back(L[now]);
for(auto X:G[now])dfs(X,deep+);
R[now]=tot;//右标记,这个内全是其子树
return;
}
void add(int x,int d)
{
for(;x<=n;x+=x&-x)c[x]+=d;
}
long long sum(int x)
{
long long ans=;
for(;x>;x-=x&-x)ans+=c[x];
return ans;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=,u,v;i<n;i++)
scanf("%d%d",&u,&v),G[u].push_back(v);
dfs(,);
for(int i=;i<=n;i++)
if(pos[i].size()>lim)vec.push_back(i);
for(int i=,op,x,y;i<m;i++)
{
scanf("%d%d",&op,&x);
if(op==)
{
scanf("%d",&y);
if(pos[x].size()<=lim)
for(auto X:pos[x])add(X,y);//小于所给块大小,直接树状数组更新
else s[x]+=y;//大于的直接扔进去等更新
}
else
{
long long ans=sum(R[x])-sum(L[x]-);//查询所有小块的值,多余的进行下面的更新
for(auto X:vec)
ans+=(upper_bound(pos[X].begin(), pos[X].end(),R[x])-lower_bound(pos[X].begin(),pos[X].end(),L[x]))*s[X];
//vec的元素不会太多,顶多1e5,直接搞进去更新
printf("%lld\n",ans);
}
}
}
4765: 普通计算姬
Time Limit: 30 Sec Memory Limit: 256 MB
Submit: 1481 Solved: 318
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
Sample Output
#include<iostream>
#include<vector>
#include<math.h>
using namespace std;
#define N 100005
typedef unsigned long long ll;
int n,m,q,lim,root,belong[N];
int L[N],R[N],tot,cnt[N];
int g[N][];
ll c[N+N],sum[N],a[N],tag[N];
vector<int>G[N];
void add(int x,int d)
{
for(; x<=n; x+=x&-x)c[x]+=d;
}
ll Sum(int x)
{
ll ans=;
for(; x>; x-=x&-x)ans+=c[x];
return ans;
}
void dfs(int x,int fa)
{
L[x]=++tot,add(L[x],a[x]),++cnt[belong[x]],sum[x]=a[x];
for(int i=; i<=lim; i++)g[x][i]=cnt[i];
int l=G[x].size();
for(int i=,X;i<l;i++)
{
X=G[x][i];
if(X==fa)continue;
dfs(X,x);
sum[x]+=sum[X];
}
R[x]=tot,--cnt[belong[x]],tag[belong[x]]+=sum[x];
}
ll query(int l,int r)
{
ll ans=;
for(int i=l;i<=min(r,belong[l]*m);i++)ans+=Sum(R[i])-Sum(L[i]-);
if(belong[l]!=belong[r])
{
for(int i=(belong[r]-)*m+;i<=r;i++)ans+=Sum(R[i])-Sum(L[i]-);
}
for(int i=belong[l]+;i<=belong[r]-;i++)ans+=tag[i];
return ans;
}
int main()
{
cin>>n>>q;
m=sqrt(n+0.5);//一块有几个
for(int i=; i<=n; i++)cin>>a[i],belong[i]=(i-)/m+;
lim=belong[n];
for(int i=,u,v; i<=n; ++i)
{
cin>>u>>v;
if(!u)root=v;
else G[u].push_back(v),G[v].push_back(u);
}
dfs(root,-);
for(int i=,op,u,v; i<q; i++)
{
cin>>op>>u>>v;
if(op==)
{
for(int i=; i<=lim; i++)tag[i]+=g[u][i]*1LL*(v-a[u]);
add(L[u],v-a[u]),a[u]=v;
}
else cout<<query(u,v)<<"\n";
}
return ;
}
ACM-ICPC 2018 沈阳赛区网络预赛 J树分块的更多相关文章
- 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(树状数组+分块)
Given a rooted tree ( the root is node 1 ) of N nodes. Initially, each node has zero point. Then, yo ...
- ACM-ICPC 2018 沈阳赛区网络预赛 J. Ka Chang (树分块)
题意:一个树,支持两种操作:1.将深度为L的节点权置加上X;2.求以x为根节点的子树上节点权置之和.根节点深度为0. 分析:考虑用树状数组维护节点权置,按dfs序下标查询.记录每个深度节点的个数.如果 ...
- ACM-ICPC 2018 沈阳赛区网络预赛 J. Ka Chang(树上分块+dfs序+线段树)
题意 链接:https://nanti.jisuanke.com/t/A1998 给出一个有根树(根是1),有n个结点.初始的时候每个结点的值都是0.下面有q个操作,操作有两种,操作1.将深度为L(根 ...
- 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 ...
- ACM-ICPC 2018 沈阳赛区网络预赛-D:Made In Heaven(K短路+A*模板)
Made In Heaven One day in the jail, F·F invites Jolyne Kujo (JOJO in brief) to play tennis with her. ...
随机推荐
- 【虚拟机-网关】如何在使用应用程序网关和 Nginx 的环境下实现强制 HTTPS 跳转
背景介绍 大家在使用 Nginx 部署网站时,实现 HTTP 到 HTTPS 的强制跳转是非常容易的事情,一般可以使用rewrite 命令或者使用返回自定义 301 页面的方法对 HTTP 请求进行 ...
- IT的学习部落(持续更新)
1.易百教程 - 专注于IT教程和实例 http://www.yiibai.com/ 2.站长特效 - js特效 http://www.zzjs.net/ 3.酷站-享受编程和技术所 ...
- .net后台使用post方式对指定地址的方法传值并且获取结果的方法
/// <summary> /// .net 后台 post http地址请求 /// </summary> /// <param name="uri" ...
- 一些常用的HTML标签
由于本人目前没有系统学习,日常碰见哪个有用就记下来. pre标签 可定义预格式化的文本,在pre元素中的文本会保留空格和换行符.比如我们展示源代码的时候,只要放一个pre标签,然后把代码直接复制.粘贴 ...
- 2018.2.09 php学习(二)
1.用索引提高效率: 索引是表的一个概念部分,用来提高检索数据的效率,ORACLE使用了一个复杂的自平衡B-tree结构. 通常,通过索引查询数据比全表扫描要快. 当ORACLE找出执行查询和Upda ...
- mtDNA|ctDNA|cpDNA|
5.9细胞器基因组是编码细胞器蛋白质的环状DNA分子 细胞器中除真核细胞线粒体DNA(mtDNA)是线性的外,都是环状分子,比如叶绿体DNA(ctDNA,cpDNA).因为单个细胞器有几套不同拷贝的细 ...
- Element-ui tree组件自定义节点使用方法
工作上使用到element-ui tree 组件,主要功能是要实现节点拖拽和置顶,通过自定义内容方法(render-content)渲染树代码如下~ <template> <di ...
- 【贪心 哈夫曼树】bzoj2923: [Poi1998]The lightest language
失去了以前用STL乱搞的能力…… 题目描述 语言也是数学上经常研究的一种数据. 给出数学上关于语言的如下定义: 字母表:大小为 K 的字母表是一个由 K 不同的字符组成的集合. 单词:长度为 m 的单 ...
- 在Linux系统中重现黑客帝国经典画面
我们需要一个叫cmatrix的小程序,下面写出步骤 1 :依赖环境 yum -y install gcc ncurses-devel 2 :下载程序 wget https://files.cnbl ...
- Linux中nginx的常见指令
1.启动cd /usr/local/nginxsbin/nginx 版权声明:本文为博主原创文章,未经博主允许不得转载. 原文地址: https://www.cnblogs.com/poterliu/ ...