codechef FUN WITH TREES
题目大意:
给一棵树root=1的树:
给一些操作:u v 的路径所有节点的node + val;
最后m个询问:u 节点(包括u) sum%mod 是多少。
LCA + RMQ;
我们每次mark ,u , v, +=val;
fa=lca[u,v])-=val;
fa[fa]-=val
你会发现下次DFS一边的时候可以把所以答案更新出来。DFS 从下往上更新。
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
#define N 100001
#define LN 20
#define mod 1000000007 vector<int>mp[N];
int mark[N];
int dp[LN][N];
int dep[N];
int ans[N],tmp[N]; int lca(int l,int r)
{
if (dep[l]<dep[r]) swap(l,r);
int dif=dep[l]-dep[r];
for (int i=;i<LN;i++)
if ((dif>>i)&) l=dp[i][l]; if (l==r) return l; for (int i=LN-;i>=;i--)
if (dp[i][l]!=dp[i][r]) l=dp[i][l],r=dp[i][r];
return dp[][l];
} void dfs(int u,int fa,int level)
{
dp[][u]=fa;
dep[u]=level;
for (int i=;i<mp[u].size();i++)
{
int v=mp[u][i];
if (v==fa) continue;
dfs(v,u,level+);
}
} int dfs2(int u,int fa=)
{
int sum=mark[u];
for (int i=;i<mp[u].size();i++)
{
int v=mp[u][i];
if (v==fa) continue;
dfs2(v,u);
sum+=tmp[v];
sum%=mod;
ans[u]+=ans[v];
ans[u]%=mod;
}
tmp[u]=sum;
ans[u]+=tmp[u];
ans[u]%=mod;
} int main()
{
int n,u,q;
scanf("%d",&n);
scanf("%d%d",&u,&q);
// for (int i=0;i<=n;i++) mp[i].clear();
for (int i=;i<n;i++)
{
int a,b;
scanf("%d%d",&a,&b);
mp[a].push_back(b);
mp[b].push_back(a);
}
dfs(,,);
for (int j=;j<LN;j++)
for (int i=;i<=n;i++)
dp[j][i]=dp[j-][dp[j-][i]];
while (u--)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
mark[a]+=c;
mark[b]+=c;
mark[a]%=mod;
mark[b]%=mod;
int l=lca(a,b);
mark[l]-=c;
mark[l]%=mod;
mark[dp[][l]]-=c;
mark[dp[][l]]%=mod;
} dfs2();
while (q--)
{
int x;
scanf("%d",&x);
printf("%d\n",(ans[x]+mod)%mod);
} return ;
}
codechef FUN WITH TREES的更多相关文章
- Codechef Dynamic Trees and Queries
Home » Practice(Hard) » Dynamic Trees and Queries Problem Code: ANUDTQSubmit https://www.codechef.co ...
- Codechef December Challenge 2014 Chef and Apple Trees 水题
Chef and Apple Trees Chef loves to prepare delicious dishes. This time, Chef has decided to prepare ...
- codechef : TREDEG , Trees and Degrees
其实有原题,生成树计数 然鹅这题里面是两道题, 50pts 可以用上面那题的做法直接过掉,另外 50pts 要推推式子,搞出 O n 的做法才行(毕竟多项式常数之大您是知道的) 虽说这道题里面是没有 ...
- 【CodeChef EDGEST】Edges in Spanning Trees(树链剖分+树上启发式合并)
点此看题面 大致题意: 给你两棵\(n\)个点的树,对于第一棵树中的每条边\(e_1\),求存在多少条第二棵树中的边\(e_2\),使得第一棵树删掉\(e_1\)加上\(e_2\).第二棵树删掉\(e ...
- [C#] C# 知识回顾 - 表达式树 Expression Trees
C# 知识回顾 - 表达式树 Expression Trees 目录 简介 Lambda 表达式创建表达式树 API 创建表达式树 解析表达式树 表达式树的永久性 编译表达式树 执行表达式树 修改表达 ...
- hdu2848 Visible Trees (容斥原理)
题意: 给n*m个点(1 ≤ m, n ≤ 1e5),左下角的点为(1,1),右上角的点(n,m),一个人站在(0,0)看这些点.在一条直线上,只能看到最前面的一个点,后面的被档住看不到,求这个人能看 ...
- [LeetCode] Minimum Height Trees 最小高度树
For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...
- [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- [LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
随机推荐
- MATLAB学习总结(1)
MATLAB学习总结(1) path help path cd(current directory) savepath pathtool
- Oracle Recycle Bin
开启回收站RECYCLEBIN=ON,默认开启 ALTER SYSTEM SET RECYCLEBIN=OFF SCOPE=SPFILE; 一.从回收站还原表 还原删除的表和从属对象. 如果多个回收站 ...
- CSS3实现单行、多行文本溢出(省略号的形式出现)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Linux基础命令——查看进程命令
linux是一个 多进程 多用户的操作系统 ps(显示当前进程的状态) ps -ef 查看当前linux 进程 ps -ef | grep 'mysqld' 过滤mysql的进程 (grep ...
- Codeforces 1076D——最短路算法
题目 给你一个有n个顶点.m条边的无向带权图.需要擦除一些边使得剩余的边数不超过k,如果一个点在原始图到顶点1的最短距离为d,在删边后的图中到顶点的最短距离仍是d,则称这种点是 good.问如何删边, ...
- jquery--cookie应用
示例:发送手机验证码 防止页面刷新后,发送验证码按钮重置 注:橙色部分为后增加代码,为防止验证码等待期间用户退出或者切换到其他页面以至于很久之后回到当前页面倒计时还在的问题,加入时间对比,记录用户发送 ...
- input_shape { dim: 1 dim: 3 dim: 224 dim: 224 }
http://blog.csdn.net/u010417185/article/details/52619593
- Python---哈夫曼树---Huffman Tree
今天要讲的是天才哈夫曼的哈夫曼编码,这是树形数据结构的一个典型应用. !!!敲黑板!!!哈夫曼树的构建以及编码方式将是我们的学习重点. 老方式,代码+解释,手把手教你Python完成哈夫曼编码的全过程 ...
- VS2015提示:未安装Style的Visual Studio语言支持,代码编辑Intellisense将不可用。服务器控件的标记Intellisense可能不起作用
一.问题 最近在VS2015打开文件,提示未安装Style的Visual Studio语言支持,代码编辑Intellisense将不可用.服务器控件的标记Intellisense可能不起作用. Int ...
- 使用Auto Layout中的VFL(Visual format language)——代码实现自动布局
本文将通过简单的UI来说明如何用VFL来实现自动布局.在自动布局的时候避免不了使用代码来加以优化以及根据内容来实现不同的UI. 一:api介绍 1.NSLayoutConstraint API NSL ...