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. WordPress 自动草稿和文章修定版本

    写文章的时候发现 WordPress 有两个有意思的地方, WordPress 自动草稿和文章修定版本: 1.点击创建新文章的时候,会在数据库自动生成一条草稿数据: 2.修改数据的时候会将历史文章当做 ...

  2. C语言基础 (3) C语言介绍

    01回顾 02 语言介绍 语言是人和人交流,C语言是人和机器交流. 03_为什么学C语言 04_第一个C代码编译运行 #include <stdio.h> int main() { // ...

  3. 浅谈 MySQL的外键的作用

    MySQL中外键的介绍: MySQL外键必须使用存储引擎为  innDB  其中MySAM 和MEMORYH这两种引擎不支持 由数据库自身保证数据一致性,完整性,更可靠,因为程序很难100%保证数据的 ...

  4. 紫书 习题8-6 UVa 1611 (构造法)

    这道题和例题8-1相当的像. 例题8-1https://blog.csdn.net/qq_34416123/article/details/80112017 一开始我还以为用归并的思想, 用交换把局部 ...

  5. HTTP——学习笔记(1)

    名词解释: 协议: HTTP:HyperText Transfer Protocol,超文本传输协议,属于应用层的协议 FTP:File Transfer Protocol,文件传输协议,相比于HTT ...

  6. ZOJ 3369 Saving Princess

    Saving Princess Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on ZJU. Origina ...

  7. gdb与信号

    http://simohayha.iteye.com/blog/493091 gdb可以监测在你的程序中的任何信号. 主要靠的命令是: handle signal [keywords...] 这里的k ...

  8. JS在页面限制checkbox最大复选数

    应该是挺简单的代码, 记录一下分享. 首先最直接的想法就是使用循环, 用局部变量记录已选的checkbox, 达到最大值就将余下的checkbox都禁止选择, 例如以下: <!DOCTYPE h ...

  9. Ubuntu 10.04 右上角网络管理图标消失的解决的方法

           那个显示网络状态的那个图标.叫做:network-manager.假设是有线网络连接的话.是上下两个箭头,假设是无线网络的话.是一个发射信号的形状. sudo gedit /etc/Ne ...

  10. 代理server poll version

    poll和select一样,管理多个描写叙述符也是进行轮询,依据描写叙述符的状态进行处理,可是poll没有最大文件描写叙述符数量的限制,select is 1024/2048 #include &qu ...