题目大意:

给一棵树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的更多相关文章

  1. Codechef Dynamic Trees and Queries

    Home » Practice(Hard) » Dynamic Trees and Queries Problem Code: ANUDTQSubmit https://www.codechef.co ...

  2. 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 ...

  3. codechef : TREDEG , Trees and Degrees

    其实有原题,生成树计数 然鹅这题里面是两道题, 50pts 可以用上面那题的做法直接过掉,另外 50pts 要推推式子,搞出 O n 的做法才行(毕竟多项式常数之大您是知道的) 虽说这道题里面是没有 ...

  4. 【CodeChef EDGEST】Edges in Spanning Trees(树链剖分+树上启发式合并)

    点此看题面 大致题意: 给你两棵\(n\)个点的树,对于第一棵树中的每条边\(e_1\),求存在多少条第二棵树中的边\(e_2\),使得第一棵树删掉\(e_1\)加上\(e_2\).第二棵树删掉\(e ...

  5. [C#] C# 知识回顾 - 表达式树 Expression Trees

    C# 知识回顾 - 表达式树 Expression Trees 目录 简介 Lambda 表达式创建表达式树 API 创建表达式树 解析表达式树 表达式树的永久性 编译表达式树 执行表达式树 修改表达 ...

  6. hdu2848 Visible Trees (容斥原理)

    题意: 给n*m个点(1 ≤ m, n ≤ 1e5),左下角的点为(1,1),右上角的点(n,m),一个人站在(0,0)看这些点.在一条直线上,只能看到最前面的一个点,后面的被档住看不到,求这个人能看 ...

  7. [LeetCode] Minimum Height Trees 最小高度树

    For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...

  8. [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  9. [LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

随机推荐

  1. monkeyrunner 简单用例编写

    monkeyrunnerfrom com.android.monkeyrunner import MonkeyRunner,MonkeyDevice,MonkeyImagedevice = Monke ...

  2. react基础语法(五) state和props区别和使用

    props的验证: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> < ...

  3. transform、transition 和 animation区别

    CSS3中和动画有关的属性有三个 transform.transition 和 animation.下面来一一说明:        transform   从字面来看transform的释义为改变,使 ...

  4. jq打印

    1.引入jQuery.print.min.js 2.将需要打印的东西用div包起来 3. $(".printDiv").print();

  5. vux安装

    1. 在项目里安装vux cnpm install vux --save 2. 安装vux-loader cnpm install vux-loader --save-dev 3. 安装less-lo ...

  6. 第1节 flume:7、flume的监控文件夹,实现数据收集到hdfs上

    1.2.2 采集案例 1.采集目录到HDFS 需求分析 结构示意图: 采集需求:某服务器的某特定目录下,会不断产生新的文件,每当有新文件出现,就需要把文件采集到HDFS中去 根据需求,首先定义以下3大 ...

  7. java 垃圾回收之标记算法

    对象被判定为垃圾的标准 1.没有被其他对象引用 判定对象是否为垃圾的算法 1.引用计数算法(不是主流垃圾回收机制) 1.1 判定对象的引用数量 1.1.1 通过判断对象的引用数量来决定对象是否可以被回 ...

  8. C++学习周记

    自开学到现在,原本可谓是对C++一无所知,也通过这几周的学习而渐渐有所了解. 最开始的编程任务虽然简单,但解决过程中却不乏磕绊,由一开始的中英文字符的不注意,到现在对一些函数的运用难免出错,出现bug ...

  9. Django中的Cookie、Session、Token

    Cookie : 指望着为了辨别用户身份.进行会话跟踪而存储在用户本地的数据(通常经过加密),是由服务端生成,发送给客户端浏览器,浏览器会将Cookie以key/value保存,下一请求同一网站是就发 ...

  10. [bzoj1022][SHOI2008]小约翰的游戏John (反Nim游戏)

    Description 小约翰经常和他的哥哥玩一个非常有趣的游戏:桌子上有n堆石子,小约翰和他的哥哥轮流取石子,每个人取 的时候,可以随意选择一堆石子,在这堆石子中取走任意多的石子,但不能一粒石子也不 ...