pid=2176" target="_blank">题目链接~~>

做题感悟:感觉做多了树链剖分的题目,有很多是树链剖分 + 想法。。

解题思路:

这题非常明显的一点就是 k 非常小,那就是告诉你能够从 k 入手,如何入手呢 ? 观察能够发现无非最多是 k 类点 ,0 ~ k-1 ,分别表示与根的距离模 k .这样就能够把点分类加权值,可是每一个线段树里存的还是全部元素,查询的时候相应查找。

代码:

#include<iostream>
#include<sstream>
#include<map>
#include<cmath>
#include<fstream>
#include<queue>
#include<vector>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<stack>
#include<bitset>
#include<ctime>
#include<string>
#include<cctype>
#include<iomanip>
#include<algorithm>
using namespace std ;
#define INT long long int
#define L(x) (x * 2)
#define R(x) (x * 2 + 1)
const int INF = 0x3f3f3f3f ;
const double esp = 0.00000000001 ;
const double PI = acos(-1.0) ;
const INT mod = 1234567891 ;
const int MY = (1<<5) + 5 ;
const int MX = 50000 + 5 ;
const int S = 20 ;
int n ,m ,k ,num ,idx ;
int head[MX] ,top[MX] ,siz[MX] ,son[MX] ,dep[MX] ,ti[MX] ,to[MX] ,father[MX] ;
struct Edge
{
int v ,next ;
}E[MX*2] ;
void addedge(int u ,int v)
{
E[num].v = v ; E[num].next = head[u] ;head[u] = num++ ;
E[num].v = u ; E[num].next = head[v] ;head[v] = num++ ;
}
void dfs_find(int u ,int fa)
{
dep[u] = dep[fa] + 1 ;
siz[u] = 1 ;
son[u] = 0 ;
father[u] = fa ;
for(int i = head[u] ;i != -1 ;i = E[i].next)
{
int v = E[i].v ;
if(v == fa) continue ;
dfs_find(v ,u) ;
siz[u] += siz[v] ;
if(siz[son[u]] < siz[v]) son[u] = v ;
}
}
void dfs_time(int u ,int fa)
{
top[u] = fa ;
ti[u] = idx++ ;
if(son[u]) dfs_time(son[u] ,top[u]) ;
for(int i = head[u] ;i != -1 ;i = E[i].next)
{
int v = E[i].v ;
if(v == son[u] || v == father[u]) continue ;
dfs_time(v ,v) ;
}
to[u] = idx-1 ;
}
struct node
{
int le ,rt ,add ,c ;
}T[6][MX*4] ;
void push_down(int cnt ,int i)
{
int& temp = T[cnt][i].add ;
if(temp)
{
T[cnt][L(i)].add += temp ;
T[cnt][L(i)].c += temp ;
T[cnt][R(i)].add += temp ;
T[cnt][R(i)].c += temp ;
temp = 0 ;
}
}
void build(int cnt ,int i ,int le ,int rt)
{
T[cnt][i].le = le ; T[cnt][i].rt = rt ;
T[cnt][i].add = T[cnt][i].c = 0 ;
if(le == rt) return ;
int Mid = (le + rt)>>1 ;
build(cnt ,L(i) ,le ,Mid) ;
build(cnt ,R(i) ,Mid+1 ,rt) ;
}
void section(int cnt ,int i ,int le ,int rt ,int val)
{
if(T[cnt][i].le == le && T[cnt][i].rt == rt)
{
T[cnt][i].add += val ;
T[cnt][i].c += val ;
return ;
}
push_down(cnt ,i) ;
int Mid = (T[cnt][i].le + T[cnt][i].rt)>>1 ;
if(le > Mid) section(cnt ,R(i) ,le ,rt ,val) ;
else if(rt <= Mid) section(cnt ,L(i) ,le ,rt ,val) ;
else
{
section(cnt ,L(i) ,le ,Mid ,val) ;
section(cnt ,R(i) ,Mid+1 ,rt ,val) ;
}
}
int Query(int cnt ,int i ,int pos)
{
if(T[cnt][i].le == T[cnt][i].rt)
return T[cnt][i].c ;
push_down(cnt ,i) ;
int Mid = (T[cnt][i].le + T[cnt][i].rt)>>1 ;
if(pos <= Mid) return Query(cnt ,L(i) ,pos) ; // 在左边
else return Query(cnt ,R(i) ,pos) ;
}
int main()
{
//freopen("input.txt" ,"r" ,stdin) ;
int Tx ,u ,v ,cnt ,x ,dt ,pos ,val ,cse = 1 ;
scanf("%d" ,&Tx) ;
while(Tx--)
{
scanf("%d%d%d" ,&n ,&m ,&k) ;
num = 0 ;
memset(head ,-1 ,sizeof(head)) ;
for(int i = 1 ;i < n ; ++i)
{
scanf("%d%d" ,&u ,&v) ;
addedge(u ,v) ;
}
dep[1] = siz[0] = 0 ;
dfs_find(1 ,1) ;
idx = 1 ;
dfs_time(1 ,1) ;
for(int i = 0 ;i < k ; ++i)
build(i ,1 ,1 ,n) ;
printf("Case#%d:\n" ,cse++) ;
for(int i = 0 ;i < m ; ++i)
{
scanf("%d%d" ,&pos ,&x) ;
if(pos == 1)
{
scanf("%d" ,&val) ;
for(int j = 0 ;j < k ; ++j)
{
dt = (dep[x] - 1)%k ; // 根所在的集合
cnt = (dt + j)%k ;
section(cnt ,1 ,ti[x] ,to[x] ,(j+1)*val) ;
}
}
else
printf("%d\n" ,Query((dep[x]-1)%k ,1 ,ti[x])) ;
}
}
return 0 ;
}

FZOJ 2176 easy problem ( 树链剖分 )的更多相关文章

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

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

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

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

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

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

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

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

  5. FZU2176---easy problem (树链剖分)

    http://acm.fzu.edu.cn/problem.php?pid=2176 Problem 2176 easy problem Accept: 9    Submit: 32Time Lim ...

  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. HDU-1358 Period 字符串问题 KMP算法 求最小循环节

    题目链接:https://cn.vjudge.net/problem/HDU-1358 题意 给一个字符串,对下标大于2的元素,问有几个最小循环节 思路 对每个元素求一下minloop,模一下就好 提 ...

  2. LAMP环境搭建备忘 -- MariaDB 安装(三)

    因为 MySQL 的一些原因,在 Linux 平台上的开源数据库渐渐被 MariaDB 取代. MariaDB 安装命令如下图 安装成功后,接下来就启动这个数据库服务 我们还需要对数据库做一些初始化的 ...

  3. FastDFS图片服务器搭建

    *FastDFS图片服务器搭建准备:1.需要libfastcommon安装包 选择最新稳定版(libfastcommon-1.0.36.tar.gz)2.需要FastDFS安装包 选择最新稳定版(fa ...

  4. 洛谷P5160 WD与循环

    我们看这段代码 int cnt = 0; for (int a_1 = 0; a_1 <= m; a_1++) { for (int a_2 = 0; a_1 + a_2 <= m; a_ ...

  5. 【codeforces 257D】Sum

    [题目链接]:http://codeforces.com/problemset/problem/257/D [题意] 给你n个数字; 这n个数字组成的数组满足: a[i-1]<=a[i]< ...

  6. Qt之QImageReader

    简述 QImageReader类为从文件或设备读取图像提供了一个独立的接口. 读取图像最常用的方法是通过构造QImage和QPixmap,或通过调用QImage::load()和QPixmap::lo ...

  7. LeetCode——Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. 写一个函数找出字符串数组中 ...

  8. 养活一款APP要“烧”多少钱?

    Duang!又一款APP刷爆朋友圈.大片范儿的电影截图.意味深长的经典对白均出自一款名为“足记”的APP. 足记团队刚于去年8月完成天使期融资,投资方是光速创投和紫辉创投,目前正准备A轮融资.且近一周 ...

  9. libevent的使用(socket)

    这篇文章介绍下libevent在socket异步编程中的应用.在一些对性能要求较高的网络应用程序中,为了防止程序堵塞在socket I/O操作上造成程序性能的下降,须要使用异步编程,即程序准备好读写的 ...

  10. 整合大量开源库项目(五)跳动的TextView JumpingBeans,良好体验的滚动条ConvenientBanner

    转载请注明出处:王亟亟的大牛之路 时间过得非常快,这一系列已经写了第五篇了(感觉还要写好久).今天又引入了2个非常好用的库JumpingBeans,ConvenientBanner.首先.先看一下效果 ...