Accept: 9    Submit: 32
Time Limit: 2000 mSec    Memory Limit : 32768 KB

 Problem Description

给定一棵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节点的权值。

 Input

首先输入一个整数T 表示数据的组数,每组数据 第一行有3个整数 n ,m, k(1<=n,m<=50000, 1<=k<=5) 。n表示节点数,m表示操作数,k表示如题目描述的常数。

接下去n-1行描述一棵树。接下去m行每行表示 一个操作 有两种类型1 x val 表示第一种类型 2 x 表示第二种类型。(1<=x<=n,1<=val<=100)

 Output

每组数据首先输出 Case#x: 表示第x组数据 x从1开始。接下去对于每组数据中的每个查询操作输出查询结果。(具体输出格式看样例)

 Sample Input

1
5 7 2
1 2
2 4
2 5
1 3
 
 
1 2 3
1 1 2
2 1
2 2
2 3
2 4
2 5

 Sample Output

Case#1:
2
7
4
8
8
大致思路:首先树链剖分一下,然后用树状数组或者线段树维护区间更新,但是((dep[y]-dep[x])%k+1)*val这种该怎么更新呢,,看到k很小最大只有5,可以想到用5棵线段树或者树状数组来维护。首先每个节点的深度dep确定后 每隔k个深度权值增加的是相等的。这样的话 就可以把所有的点对应到 k个树状数组 上去。然后就是树状数组经典的区间更新单点查询操作了。
 #include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const double eps = 1e-;
const int maxn = 5e4+;
struct
{
int to,next;
} e[maxn<<];
int tot,head[maxn];
void add_edge(int x,int y)
{
e[tot].to = y;
e[tot].next = head[x];
head[x] = tot++;
}
int siz[maxn],son[maxn],fa[maxn],dep[maxn];
void dfs(int root)
{
siz[root] = ;
son[root] = ;
for (int i = head[root]; ~i; i = e[i].next)
{
if (fa[root] != e[i].to)
{
fa[e[i].to] = root;
dep[e[i].to] = dep[root] + ;
dfs(e[i].to);
if (siz[e[i].to] > siz[son[root]])
son[root] = e[i].to;
siz[root] += siz[e[i].to];
}
}
}
int pos[maxn],top[maxn],L[maxn],R[maxn],bj1,bj2,idx;
void build (int root,int father)
{
pos[root] = ++idx;
top[root] = father;
L[root] = root;
R[root] = root;
if (son[root] > )
{
bj1 = son[root];
build(son[root],top[root]);
L[root] = bj1;
}
bool flag = ;
for (int i = head[root]; ~i; i = e[i].next)
{
flag = ;
if (fa[root] != e[i].to && son[root] != e[i].to)
bj2 = e[i].to,build(e[i].to,e[i].to);
else if (siz[root] == )
bj2 = root;
}
if (flag)
R[root] = bj2;
}
int lowbit(int x)
{
return x & -x;
}
int c[][maxn],n;
void add(int x,int d,int k)
{
while (x <= n)
{
c[k][x] += d;
x += lowbit(x);
}
}
int sum(int x,int k)
{
int ans = ;
while (x > )
{
ans += c[k][x];
x -= lowbit(x);
}
return ans;
}
void init()
{
int root = ;
tot = idx = ;
dep[root] = fa[root] = ;
memset(head,-,sizeof(head));
memset(siz,,sizeof(siz));
memset(c,,sizeof(c));
for (int i = ; i < n; i++)
{
int u,v;
scanf ("%d%d",&u,&v);
add_edge(u,v);
add_edge(v,u);
}
dfs(root);
build(root,root);
}
int main(void)
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
int t,cas = ;
scanf ("%d",&t);
while (t--)
{
printf("Case#%d:\n",cas++);
int k,m;
scanf ("%d%d%d",&n,&m,&k);
init();
for (int i = ; i < m; i++)
{
int op,x,val;
scanf ("%d%d",&op,&x);
if (op == )
{
scanf ("%d",&val);
for (int j = ; j < k; j++)
{
int tmp = (j + ) * val;
int l = pos[x];
int r = pos[R[x]];
add(pos[x], tmp,((dep[x] + )%k + j)%k);
add(pos[R[x]] + , -tmp,((dep[x] + )%k + j)%k);
}
}
if (op == )
{
printf("%d\n",sum(pos[x],(dep[x]-dep[] + ) % k));
}
}
}
return ;
}

FZU2176---easy problem (树链剖分)的更多相关文章

  1. FZOJ 2176 easy problem ( 树链剖分 )

    pid=2176" target="_blank">题目链接~~> 做题感悟:感觉做多了树链剖分的题目,有很多是树链剖分 + 想法.. 解题思路: 这题非常明 ...

  2. P1001 A+B Problem (树链剖分)

    这题考验我们构造模型的能力. 考虑构造一棵树,树上有3个节点,节点1和节点2连一条权值为a的边,节点1和节点3连一条权值为b的边,显然答案就是节点2到节点3的最短路径. 但这样还不够.考虑加法的性质, ...

  3. HDU 5293 Train chain Problem - 树链剖分(树状数组) + 线段树+ 树型dp

    传送门 题目大意: 一颗n个点的树,给出m条链,第i条链的权值是\(w_i\),可以选择若干条不相交的链,求最大权值和. 题目分析: 树型dp: dp[u][0]表示不经过u节点,其子树的最优值,dp ...

  4. Hdu 5052 Yaoge’s maximum profit(树链剖分)

    题目大意: 给出一棵树.每一个点有商店.每一个商店都有一个价格,Yaoge每次从x走到y都能够在一个倒卖商品,从中得取利益.当然,买一顶要在卖之前.可是没次走过一条路,这条路上的全部商品都会添加一个v ...

  5. HDU 4729 An Easy Problem for Elfness(树链剖分边权+二分)

    题意 链接:https://cn.vjudge.net/problem/HDU-4729 给你n个点,然你求两个点s和t之间的最大流.而且你有一定的钱k,可以进行两种操作 1.在任意连个点之间建立一个 ...

  6. (中等) HDU 5293 Tree chain problem,树链剖分+树形DP。

    Problem Description   Coco has a tree, whose vertices are conveniently labeled by 1,2,…,n.There are ...

  7. Codeforces Round #425 (Div. 2) Problem D Misha, Grisha and Underground (Codeforces 832D) - 树链剖分 - 树状数组

    Misha and Grisha are funny boys, so they like to use new underground. The underground has n stations ...

  8. [HDU 5293]Tree chain problem(树形dp+树链剖分)

    [HDU 5293]Tree chain problem(树形dp+树链剖分) 题面 在一棵树中,给出若干条链和链的权值,求选取不相交的链使得权值和最大. 分析 考虑树形dp,dp[x]表示以x为子树 ...

  9. USACO 2015 December Contest, Platinum Problem Max Flow【树链剖分】

    题意比较难理解,就是给你n个点的树,然后给你m个修改操作,每一次修改包括一个点对(x, y),意味着将x到y所有的点权值加一,最后问你整个树上的点权最大是多少. 比较裸的树链剖分了,感谢Haild的讲 ...

随机推荐

  1. duilib DirectUI库里面的一个简单的例子RichListDemo

    1.首先来看这里的CRichListWnd 已经不再是从CWindowWnd继承了 classCRichListWnd:publicWindowImplBase 从WindowImplBase中,可以 ...

  2. LVS+Keepalived实现MySQL从库读操作负载均衡

    http://www.osyunwei.com/archives/7464.html (学习运维知识好站) 说明: 操作系统:CentOS 5.X 64位 MySQL主服务器:192.168.21.1 ...

  3. [CSS3] Interactive Pseudo-Classes :link :visited :hover :active

    The interactive pseudo-classes for links (and buttons) allow us to make sure the user knows what ele ...

  4. 【翻译】A (very) short introduction to R R的简短介绍

    [前言] 本文翻译自Paul Torfs & Claudia Brauer的文章A (very) short introduction to R.其中比较简单的地方没有翻译,不好用中文描述的地 ...

  5. 属性动画 LayoutTransition AnimatorInflater Keyframe 新特性

    LayoutTransition设置动画 使用LayoutTransition可为布局的容器设置动画,当容器中的视图层次发生变化时产生相应的过渡的动画效果 过渡的类型一共有四种: LayoutTran ...

  6. ZOJ2099

    题意:给多个点,连成折线,求一个矩形可以包含这条折线. 输入: 多组测试数据 多个点的坐标 每组测试数据以0,0结束 以0,0结束程序 输出: 矩形左下角和右上角的坐标 思路:水题,注意输入那里有点坑 ...

  7. maven 配置报错 JAVA_HOME not found in your environment

    最近比较空,想研究下spring mvc,于是编按照教程一步一步配置开发环境.配置maven完成后,运行命令mvn -v的时候,竟然报错.错误信息如下: Error: JAVA_HOME not fo ...

  8. zookeeper管理solr的配置文件

    zookeeper可以管理solr和其他软件的配置文件.配置文件还是保存在linux服务器的磁盘上,但是不是改变solr读取solr/home配置的配置文件的位置. 现在solr/home配置文件的位 ...

  9. BOM和DOM的联系和区别

    BOM中的对象 Window对象: 是整个BOM的核心,所有对象和集合都以某种方式回接到window对象.Window对象表示整个浏览器窗口,但不必表示其中包含的内容. Document对象: 实际上 ...

  10. mysql数据表如何导入MSSQL中

    本案例演示所用系统是windows server 2012.其它版本windows操作系统类似. 1,首先需要下载mysql odbc安装包. http://dev.mysql.com/downloa ...