题目大意:
  有一棵n节点的树,根为1号节点。每个节点有两个权值ki,ti,初始值均为0。
  给出三种操作:
    1.Add(x,d)操作:将x到根的路径上所有点的ki←ki+d
    2.Mul(x,d)操作:将x到根的路径上所有点的ti←ti+d×ki
    3.Query(x)操作:询问点x的权值tx

思路:
  树链剖分以后用线段树维护。
  对于每个结点,我们可以维护3个数a,b,c,表示最后的t为a*b+c。
  对于操作1,需要修改a(修改后的ki)和c(修改的数再乘以b就多了,要从c中减去)。
  对于操作2,需要修改b。
  然后操作3就变成了单点查询。

 #include<cstdio>
#include<cctype>
#include<vector>
inline int getint() {
register char ch;
register bool neg=false;
while(!isdigit(ch=getchar())) if(ch=='-') neg=true;
register int x=ch^'';
while(isdigit(ch=getchar())) x=(((x<<)+x)<<)+(ch^'');
return neg?-x:x;
}
const int N=;
std::vector<int> e[N];
inline void add_edge(const int &u,const int &v) {
e[u].push_back(v);
e[v].push_back(u);
}
int n,par[N],son[N],size[N],dep[N],top[N],id[N];
void dfs1(const int &x,const int &par) {
size[x]=;
::par[x]=par;
dep[x]=dep[par]+;
for(unsigned i=;i<e[x].size();i++) {
const int &y=e[x][i];
if(y==par) continue;
dfs1(y,x);
size[x]+=size[y];
if(size[y]>size[son[x]]) {
son[x]=y;
}
}
}
void dfs2(const int &x) {
id[x]=++id[];
if(x==son[par[x]]) {
top[x]=top[par[x]];
} else {
top[x]=x;
}
if(son[x]) dfs2(son[x]);
for(unsigned i=;i<e[x].size();i++) {
const int &y=e[x][i];
if(y==par[x]||y==son[x]) continue;
dfs2(y);
}
}
struct Tag {
int a,b,c;
void operator += (const Tag &another) {
c+=another.c-another.a*b;
a+=another.a;
b+=another.b;
}
int calc() const {
return a*b+c;
}
};
class SegmentTree {
#define _left <<1
#define _right <<1|1
private:
Tag val[N<<];
void push_down(const int &p) {
val[p _left]+=val[p];
val[p _right]+=val[p];
val[p]=(Tag){,,};
}
public:
void modify(const int &p,const int &b,const int &e,const int &l,const int &r,const Tag &t) {
if(b==l&&e==r) {
val[p]+=t;
return;
}
push_down(p);
const int mid=(b+e)>>;
if(l<=mid) modify(p _left,b,mid,l,std::min(mid,r),t);
if(r>mid) modify(p _right,mid+,e,std::max(mid+,l),r,t);
}
int query(const int &p,const int &b,const int &e,const int &x) {
if(b==e) {
return val[p].calc();
}
push_down(p);
const int mid=(b+e)>>;
return x<=mid?query(p _left,b,mid,x):query(p _right,mid+,e,x);
}
#undef _left
#undef _right
};
SegmentTree t;
inline void modify(int x,const Tag &tag) {
for(;top[x];x=par[top[x]]) {
t.modify(,,n,id[top[x]],id[x],tag);
}
}
inline int query(const int &x) {
return t.query(,,n,id[x]);
}
int main() {
n=getint();
for(register int i=;i<n;i++) {
add_edge(getint(),getint());
}
dfs1(,);
dfs2();
for(register int m=getint();m;m--) {
const int opt=getint(),x=getint();
if(opt==) modify(x,(Tag){getint(),,});
if(opt==) modify(x,(Tag){,getint(),});
if(opt==) printf("%d\n",query(x));
}
return ;
}

[LOJ6208]树上询问的更多相关文章

  1. BZOJ 3221: [Codechef FEB13] Obserbing the tree树上询问( 可持久化线段树 + 树链剖分 )

    树链剖分+可持久化线段树....这个一眼可以看出来, 因为可持久化所以写了标记永久化(否则就是区间修改的线段树的持久化..不会), 结果就写挂了, T得飞起...和管理员拿数据调后才发现= = 做法: ...

  2. [BZOJ 3221][Codechef FEB13] Obserbing the tree树上询问

    [BZOJ 3221]Obserbing the tree树上询问 题目 小N最近在做关于树的题.今天她想了这样一道题,给定一棵N个节点的树,节点按1~N编号,一开始每个节点上的权值都是0,接下来有M ...

  3. bzoj 3221: Obserbing the tree树上询问 树链剖分+线段树

    题目大意: http://www.lydsy.com/JudgeOnline/problem.php?id=3221 题解 啊呀...这是昨天的考试题啊...直接就粘了.. 与4515: [Sdoi2 ...

  4. LibreOJ #6208. 树上询问

    内存限制:512 MiB 时间限制:500 ms 标准输入输出 题目类型:传统 评测方式:文本比较 上传者: 匿名 树链剖分+线段树 屠龙宝刀点击就送 #include <vector> ...

  5. dsu on tree 树上启发式合并 学习笔记

    近几天跟着dreagonm大佬学习了\(dsu\ on\ tree\),来总结一下: \(dsu\ on\ tree\),也就是树上启发式合并,是用来处理一类离线的树上询问问题(比如子树内的颜色种数) ...

  6. BZOJ3052/UOJ#58 [wc2013]糖果公园 莫队 带修莫队 树上莫队

    原文链接https://www.cnblogs.com/zhouzhendong/p/BZOJ3052.html 题目传送门 - BZOJ3052 题目传送门 - UOJ#58 题意 给定一棵树,有 ...

  7. NOIP2013

    DAY1 转圈游戏 列出式子(x+km)%n,快速幂. // codevs3285 #include<algorithm> #include<iostream> #includ ...

  8. 可持久化Trie & 可持久化平衡树 专题练习

    [xsy1629]可持久化序列 - 可持久化平衡树 http://www.cnblogs.com/Sdchr/p/6258827.html [bzoj4260]REBXOR - Trie 事实上只是一 ...

  9. 树链剖分入门-Hdu3966 Aragorn's Story

    AC通道:http://acm.hdu.edu.cn/showproblem.php?pid=3966 [题目大意] 一棵树上每个点有权值,每次支持三种操作:给[a,b]路径上的所有节点的权值加上k, ...

随机推荐

  1. NOIP2018 集训(三)

    A题 Tree 问题描述 给定一颗 \(n\) 个点的树,树边带权,试求一个排列 \(P\) ,使下式的值最大 \[\sum_{i=1}^{n-1} maxflow(P_i, P_{i+1}) \] ...

  2. 第一次玩博客 感觉自己特别low

    第一天来  来好激动第一天来  来好激动第一天来  来好激动第一天来  来好激动第一天来  来好激动第一天来  来好激动第一天来  来好激动第一天来  来好激动第一天来  来好激动第一天来  来好激动 ...

  3. jqury关于cooke的操作写入cookie后只显示一次的DIV提示框代码

    有时候当用户登录系统后,需要给用户弹出提示框,但是不需要总是弹出来,在这里加入访问cookie来判断是否弹出过提示框,如果弹出过那么保存cookie,下次根据cookie是否存在来判断是否弹出 < ...

  4. rem布局和hotcss原理分析

    rem布局的开源方案hotcss, 其原理个人理解如下: 手机px = (手机页面宽度/设计稿宽度) * 设计稿px 手机rem = 手机px / fontSize = (手机页面宽度/设计稿宽度) ...

  5. DHTMLX 表格组件(dhtmlxGrid )使用介绍

    String sql = select {s.*} from t_student s where s.age22; SQLQuery slqQuery = session.createSQLQuery ...

  6. C#实战Microsoft Messaging Queue(MSMQ)消息队列

    前言 在使用MSMQ之前,我们需要自行安装消息队列组件!(具体安装方法大家自己搜一下吧) 采用MSMQ带来的好处是:由于是异步通信,无论是发送方还是接收方都不用等待对方返回成功消息,就可以执行余下的代 ...

  7. TCP面试题之四次挥手过程

    TCP四次挥手过程: 1.第一次挥手:Client发送一个FIN,用来关闭Client到Server的数据传送,Client进入FIN_WAIT_1状态: 2.第二次挥手:Server收到FIN后,发 ...

  8. mac 安装 nodeJs&npm 配置

    前言:继续安装 nodeJS 下载安装:直接去官网,点击安装即可. 命令式安装: 1. 安装 homebrew 2. 安装 nodeJS,使用命令:brew install node 3. 测试 no ...

  9. 【Apache Nutch系列】Nutch2.0配置安装异常集锦

    1.java.lang.NoClassDefFoundError: org/apache/hadoop/hbase/HBaseConfiguration Exception in thread &qu ...

  10. Android四大组件:Service

    前言 Service作为Android四大组件之一,应用非常广泛 本文将介绍对Service进行全面介绍(基础认识.生命周期.使用和应用场景) 目录 目录 1. 基础知识 定义:服务,属于Androi ...