传送门: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. ASP.NET - Eval使用自定义的方法

    <asp:Repeater ID="rep_allnews" runat="server"> <ItemTemplate> <tr ...

  2. 网页WEB打印控件

    网页WEB打印控件制作 在WEB系统中,打印的确是比较烦人的问题,如果我们能制作一个属于自己的自定义的打印插件,那么我们在后续自定义打印的时候能随心所欲的控制打印,这样的效果对于程序员来说是非常开心的 ...

  3. Tesseract Ocr引擎

    Tesseract Ocr引擎 1.Tesseract介绍 tesseract 是一个google支持的开源ocr项目,其项目地址:https://github.com/tesseract-ocr/t ...

  4. 程序实现LayoutAnimationController

    在res/anim下新建anim_set.xml: <?xml version="1.0" encoding="utf-8"?> <set x ...

  5. Duanxx的C++学习: 使用类没有被定义 原因及解决方法

    今天,随着C++写的神经网络算法.我发现了一个很令人费解的问题,下面的描述一般地描述,例如: 我有两个类ClassA和ClassB,它们分别有成员变量a.b; ClassA有一个函数是这种:Funct ...

  6. IOS 轻量级数据持久化 DataLite

    开发的过程中我们经常要保存一些配置信息,一般简单的是用 NSUserDefaults [[NSUserDefaults standardUserDefaults] objectForKey:key]; ...

  7. H-index因素

    Problem Description Paper quality and quantity have long been used to measure a research's scientifi ...

  8. Swift语言Auto Layout入门教程:上篇

    原文:Beginning Auto Layout Tutorial in Swift: Part 1/2,译者:@TurtleFromMars 开始用自动布局约束的方式思考吧! 更新记录:该教程由Br ...

  9. Swift - 多行文本输入框(UITextView)的用法

    1,多行文本控件的创建 1 2 3 4 var textview=UITextView(frame:CGRectMake(10,100,200,100)) textview.layer.borderW ...

  10. CloseHandle(),TerminateThread(),ExitThread()的差别

    线程的handle用处: 线程的handle是指向"线程的内核对象"的,而不是指向线程本身.每一个内核对象仅仅是内核分配的一个内存块,而且仅仅能由内核訪问.该内存块是一种数据结构, ...