FZU2176---easy problem (树链剖分)
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
Sample Output
#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 (树链剖分)的更多相关文章
- FZOJ 2176 easy problem ( 树链剖分 )
pid=2176" target="_blank">题目链接~~> 做题感悟:感觉做多了树链剖分的题目,有很多是树链剖分 + 想法.. 解题思路: 这题非常明 ...
- P1001 A+B Problem (树链剖分)
这题考验我们构造模型的能力. 考虑构造一棵树,树上有3个节点,节点1和节点2连一条权值为a的边,节点1和节点3连一条权值为b的边,显然答案就是节点2到节点3的最短路径. 但这样还不够.考虑加法的性质, ...
- HDU 5293 Train chain Problem - 树链剖分(树状数组) + 线段树+ 树型dp
传送门 题目大意: 一颗n个点的树,给出m条链,第i条链的权值是\(w_i\),可以选择若干条不相交的链,求最大权值和. 题目分析: 树型dp: dp[u][0]表示不经过u节点,其子树的最优值,dp ...
- Hdu 5052 Yaoge’s maximum profit(树链剖分)
题目大意: 给出一棵树.每一个点有商店.每一个商店都有一个价格,Yaoge每次从x走到y都能够在一个倒卖商品,从中得取利益.当然,买一顶要在卖之前.可是没次走过一条路,这条路上的全部商品都会添加一个v ...
- HDU 4729 An Easy Problem for Elfness(树链剖分边权+二分)
题意 链接:https://cn.vjudge.net/problem/HDU-4729 给你n个点,然你求两个点s和t之间的最大流.而且你有一定的钱k,可以进行两种操作 1.在任意连个点之间建立一个 ...
- (中等) HDU 5293 Tree chain problem,树链剖分+树形DP。
Problem Description Coco has a tree, whose vertices are conveniently labeled by 1,2,…,n.There are ...
- 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 ...
- [HDU 5293]Tree chain problem(树形dp+树链剖分)
[HDU 5293]Tree chain problem(树形dp+树链剖分) 题面 在一棵树中,给出若干条链和链的权值,求选取不相交的链使得权值和最大. 分析 考虑树形dp,dp[x]表示以x为子树 ...
- USACO 2015 December Contest, Platinum Problem Max Flow【树链剖分】
题意比较难理解,就是给你n个点的树,然后给你m个修改操作,每一次修改包括一个点对(x, y),意味着将x到y所有的点权值加一,最后问你整个树上的点权最大是多少. 比较裸的树链剖分了,感谢Haild的讲 ...
随机推荐
- TN2151:崩溃报告
understanding and analyzing ios application crashreports 这个TN涉及了与崩溃相关的 内存耗尽信息,堆栈信息 以及 异常编号 等信息 内存耗尽 ...
- 大数据笔记01:大数据之Hadoop简介
1. 背景 随着大数据时代来临,人们发现数据越来越多.但是如何对大数据进行存储与分析呢? 单机PC存储和分析数据存在很多瓶颈,包括存储容量.读写速率.计算效率等等,这些单机PC无法满足要求. 2. ...
- yii中常用路径<转>
调用YII框架中jquery:Yii::app()->clientScript->registerCoreScript(‘jquery’); framework/web/js/source ...
- Linux下python升级
Centos即使用Yum更新也是Python2.6.6所以需要升级到Python2.7.8 1.先下载源码包 1 wget https://www.python.org/ftp/python/2.7. ...
- mysql慢查优化总结
1.优化sql语句结构 or改成union,使用start,limit 先只查询出所有的id,然后再排序.如果查询出所有的id仍然很慢,就要仔细考虑了. 2.添加索引 mysql每次查询只能使用一个索 ...
- (转)用eclipse创建一个j2ee的web工程后,左面projects窗口中的项目如何没有显示webRoot文件夹,除了src的文件夹,其他都不显示
左边目录窗口的右上方,有个向下的箭头,点里面的filters,把所有的勾都去掉看看
- C++中getline函数的使用
代码: #include <iostream> #include <cstdio> using namespace std; int main(){ char* s; s = ...
- cocos2dx之触摸事件
要使精灵能够接收到触摸事件,无非要做三件事. 注册触摸事件; 接收触摸事件; 处理触摸事件. 下面就从这三点出发,来了解一下精灵如何响应触摸事件. 1.注册触摸事件 精灵类Poker继承Sprite和 ...
- jsp之间url传值出现中文乱码
示例: T1.jsp http://localhost:8080/test/Test.action?site=北京 T2.jsp ..... <%@ page language="ja ...
- Java解析和生成XML
1.Jaxb处理java对象和xml之间转换常用的annotation有: @XmlType @XmlElement @XmlRootElement @XmlAttribute @XmlAccesso ...