传送门: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. C# 未能加载文件或程序集“MySQLDriverCS..." 错误解决

    在解决方案的属性里,生成,里面有个目标平台,网上说的 大概也就是64位和32位的不兼容问题..试着把目标平台改为X86后竟然神奇的正常了!

  2. Struts 2中的constant详解

    通过对这些属性的配置,可以改变Struts 2 框架的一些默认行为,这些配置可以在struts.xml文件中完成,也可以在struts.properties文件中完成. 1.<constant ...

  3. windows 2003 DNS服务的重建

    DNS的重建 网络上转载之文章,据说来自微软官方,留下一份自己用,曾经帮助我解决了DNS方面的困扰,希望能给大家带来帮助 1.打开dns管理器,删除domain.com区域,如果存在_msdcs.do ...

  4. sqlserver05 字符串拆分

    -- 规则:将 gs-abc-aa-aa 拆分为一下字符 -- gs-abc-aa-aa -- gs-abc-aa -- gs-abc -- gs select * from dbo.f_split( ...

  5. Mac 终端提示You have not agreed to the Xcode license agreements

    在mac 终端运行命令的时候会被提示没有同意xcode 证书 ,这个时候需要在Terminal中同意license 打开终端输入xcodebuild -license,会看到一大堆的license说明 ...

  6. 28.uva 10891 Game of Sum 记忆化dp

    这题和上次的通化邀请赛的那题一样,而且还是简化版本... 那题的题解      请戳这里 ... #include<cstdio> #include<algorithm> #i ...

  7. Swift - 列表项尾部附件点击响应(感叹号,箭头等)

    列表单元格尾部可以添加各种样式的附件,如感叹号,三角箭头等.而且点击内容区域与点击附件的这两个响应事件是不同的,这样可以方便我们实现不同的功能(比如点击内容则查看详情,点击感叹号则编辑) 1 2 3 ...

  8. Python - 定制pattern的string模板(template) 具体解释

    定制pattern的string模板(template) 具体解释 本文地址: http://blog.csdn.net/caroline_wendy/article/details/28625179 ...

  9. spring mvc 接受多对象的处置

    spring mvc 接受多对象的处理 spring mvc感觉非常好用,尤其是对接收对象參数的自己主动绑定非常简便,但对于同一时候传多个对象时有些困扰. 同一时候项目并没有直接使用spring的fo ...

  10. Eclipse在点击运行后不能自动保存的解决

    今天在eclipse上写程序调试时,发现当我点击运行按键之后,并不能在运行前帮我自动保存,也就是说每次修改代码之后, 运行的还是前一次运行之前的代码,并不是修改之后的代码,因此通过在网上搜索解决方案之 ...