传送门:easy problem

题意给定一棵n个节点以1为根的树,初始每个节点的值为0,现在我们要在树上进行一些操作,操作有两种类型。

1 x val 表示对以x为根的子树的每个点进行加权操作(我们定义每个节点的深度为每个节点到根1的距离),如果 y是以x为根的子树中的点那么 y节点的权值增加 ((dep[y]-dep[x])%k+1)*val 其中dep[y]表示y节点的深度,k为一个常数(1<=k<=5)

2 x 查询当前x节点的权值。

分析:这题用树链剖分有点大材小用,直接一个dfs将每点遍历完又回到该点重新标号映射到线段树上,然后每修改该点及它的子节点时在线段树上操作。由题意可发现,每隔k个深度权值增加是一样的,而k又很小,因此用k棵线段树分别维护整段区间内深度模k的点余x(0<x<k)的点,然后修改区间时分别给区间内模k为0,1...k-1的点修改,也就是给k棵线段树进行区间修改。

#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <stack>
#include <vector>
#include <set>
#include <map>
#define LL long long
#define mod 100000000
#define inf 0x3f3f3f3f
#define eps 1e-6
#define N 50010
#define FILL(a,b) (memset(a,b,sizeof(a)))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define PII pair<int,int>
using namespace std;
struct edge
{
int v,next;
edge(){}
edge(int v,int next):v(v),next(next){}
}e[N];
int head[N],vis[N],tot;
int col[][N<<],num;
int st[N],ed[N],dep[N];
int n,m,k;
void init()
{
FILL(head,-);
FILL(vis,);
tot=;
}
void addedge(int u,int v)
{
e[tot]=edge(v,head[u]);
head[u]=tot++;
}
void dfs(int u,int fa)
{
st[u]=++num;
for(int i=head[u];~i;i=e[i].next)
{
int v=e[i].v;
dep[v]=dep[u]+;
dfs(v,u);
}
ed[u]=num;
}
void build(int l,int r,int rt)
{
for(int i=;i<k;i++)col[i][rt]=;
if(l==r)return;
int m=(l+r)>>;
build(lson);
build(rson);
}
void Pushdown(int rt)
{
for(int i=;i<k;i++)
if(col[i][rt])
{
col[i][rt<<]+=col[i][rt];
col[i][rt<<|]+=col[i][rt];
col[i][rt]=;
}
}
void update(int L,int R,int s,int c,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
col[s][rt]+=c;
return;
}
Pushdown(rt);
int m=(l+r)>>;
if(L<=m)update(L,R,s,c,lson);
if(m<R)update(L,R,s,c,rson);
}
int query(int pos,int s,int l,int r,int rt)
{
if(l==r)
{
return col[s][rt];
}
Pushdown(rt);
int m=(l+r)>>;
if(pos<=m)return query(pos,s,lson);
else return query(pos,s,rson);
}
int main()
{ int t,a,b,op,cas=;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&m,&k);
init();
for(int i=;i<n;i++)
{
scanf("%d%d",&a,&b);
addedge(a,b);
}
num=;dep[]=;
dfs(,-);
build(,num,);
printf("Case#%d:\n",cas++);
while(m--)
{
scanf("%d",&op);
if(op==)
{
scanf("%d",&a);
printf("%d\n",query(st[a],(dep[a]+)%k,,num,));
}
else
{
scanf("%d%d",&a,&b);
for(int i=;i<k;i++)
{
int s=((dep[a]+)%k+i)%k;
update(st[a],ed[a],s,b*(i+),,num,);
}
}
}
}
}

FZU2176(二维线段树+dfs)的更多相关文章

  1. UVA 11297 线段树套线段树(二维线段树)

    题目大意: 就是在二维的空间内进行单个的修改,或者进行整块矩形区域的最大最小值查询 二维线段树树,要注意的是第一维上不是叶子形成的第二维线段树和叶子形成的第二维线段树要  不同的处理方式,非叶子形成的 ...

  2. POJ2155 Matrix二维线段树经典题

    题目链接 二维树状数组 #include<iostream> #include<math.h> #include<algorithm> #include<st ...

  3. HDU 1823 Luck and Love(二维线段树)

    之前只知道这个东西的大概概念,没具体去写,最近呵呵,今补上. 二维线段树 -- 点更段查 #include <cstdio> #include <cstring> #inclu ...

  4. poj 2155:Matrix(二维线段树,矩阵取反,好题)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 17880   Accepted: 6709 Descripti ...

  5. poj 1195:Mobile phones(二维线段树,矩阵求和)

    Mobile phones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 14391   Accepted: 6685 De ...

  6. POJ 2155 Matrix (二维线段树)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 17226   Accepted: 6461 Descripti ...

  7. HDU 4819 Mosaic (二维线段树)

    Mosaic Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)Total S ...

  8. HDU 4819 Mosaic --二维线段树(树套树)

    题意: 给一个矩阵,每次查询一个子矩阵内的最大最小值,然后更新子矩阵中心点为(Max+Min)/2. 解法: 由于是矩阵,且要求区间最大最小和更新单点,很容易想到二维的线段树,可是因为之前没写过二维的 ...

  9. HDU 4819 Mosaic(13年长春现场 二维线段树)

    HDU 4819 Mosaic 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4819 题意:给定一个n*n的矩阵,每次给定一个子矩阵区域(x,y,l) ...

随机推荐

  1. jQuery中对 input 控件的操作

    jquery radio取值,checkbox取值,select取值,radio选中,checkbox选中,select选中,及其相关 1.获取值 jquery取radio单选按钮的值 $(" ...

  2. 单元测试工具 SmokeTest

    .NET 程序集单元测试工具 SmokeTest Smoke Test(冒烟测试),也称Regression Test(回归测试),是对软件的安装和基本功能的测试.一般地我们使用脚本来实现Smoke ...

  3. vcredist_x86.exe 静默安装方法

    我们打包基于VC++开发的应用程序,我们会一同打包一个VC运行库,否则安装到一些非开发环境中,你的应用程序依然可以正确运行. Visual C++ 2008 Redistributable Packa ...

  4. 编译Boost 详细步骤 适用 VC6 VS2003 VS2005 VS2008 VS2010

    vs2008编译boost [一.Boost库的介绍] Boost库是一个经过千锤百炼.可移植.提供源代码的C++库,作为标准库的后备,是C++标准化进程的发动机之一.Boost库由C++标准委员会库 ...

  5. Salt Stack 官方文档翻译 - 一个想做dba的sa - 博客频道 - CSDN.NET

    OSNIT_百度百科 Salt Stack 官方文档翻译 - 一个想做dba的sa - 博客频道 - CSDN.NET Salt Stack 官方文档翻译 分类: 自动运维 2013-04-02 11 ...

  6. 待机状态下,服务类型 WCDMA Service type in Idle mode

    Services aredistinguished into categories defined in [7]; also the categorisation of cellsaccording ...

  7. CSS中常见的BUG调试

    1.布局--layout 布局是windows提出的概念,用于控制元素的尺寸和定位. 拥有布局的元素负责自身及其子元素的尺寸及定位,而没有布局的元素仅仅能依靠近期的祖先元素进行控制. 通常在IE6中出 ...

  8. perl的一些基本用法

    ReadLine support available (try 'install Bundle::CPAN')cpan>进入cpan的shell,好了,我为了安装spamassassin,需要安 ...

  9. Ext图表的精彩

    最近做了几个Ext的图表,觉得效果还不错,分享一下 1.Chart[饼图.折线图.柱状图.堆状图]: 2.Grid: 3.最大化:

  10. JSTL解析——002——core标签库01

    javaEE5之前的版本需要引用JSTL相关的jar包.tld文件等,JAEE5之后就不用这么麻烦了, 如果你的还是不能使用就去官网下载(jstl.jar和standard.jar)这两个jar包,将 ...