洛谷p3384【模板】树链剖分题解
洛谷p3384 【模板】树链剖分错误记录
首先感谢\(lfd\)在课上调了出来\(Orz\)
\(1\).以后少写全局变量
\(2\).线段树递归的时候最好把左右区间一起传
\(3\).写\(dfs\)的时候不要写错名字
\(4\).使用线段树的操作的时候才要用到\(dfs\)序
\(5\).需要开一个数组来记录在\(dfs\)序下的节点是什么也方便线段树的赋值
\(6\).注意\(down\)函数内怎样更新
\(7\).在查询的时候并不需要向上更新
由于\(yxj\)看了\(lfd\)敲的树链剖分感觉压完行之后非常的好看,由此\(yxj\)也踏上了疯狂压行的不归路
Code:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define lson k << 1
#define rson k << 1 | 1
using namespace std;
const int N = 1e5+7;
int n, m, R, p, dfn[N], top[N], son[N], dep[N], fa[N], siz[N], tot, head[N << 1], cnt, num, x, y, z, w[N], l, r, ans, pre[N];
struct node {int l, r, f, w;}tr[N << 2];
struct Node {int nxt, to;}e[N << 1];
int read() {
int s = 0, w = 1;
char ch = getchar();
while(!isdigit(ch)) {if(ch == '-') w = -1; ch = getchar();}
while(isdigit(ch)) {s = s * 10 + ch - '0'; ch = getchar();}
return s * w;
}
void build(int k, int l, int r) {
tr[k].l = l, tr[k].r = r;
if(l == r) {tr[k].w = w[pre[l]]; return;}
int mid = (l + r) >> 1;
build(lson, l, mid);
build(rson, mid + 1, r);
tr[k].w = (tr[lson].w + tr[rson].w) % p;
}
void add(int x, int y) {
e[++cnt].nxt = head[x];
e[cnt].to = y;
head[x] = cnt;
}
void dfs(int x) {
siz[x] = 1;
dep[x] = dep[fa[x]] + 1;
for(int i = head[x]; i; i = e[i].nxt) {
if(e[i].to == fa[x]) continue;
fa[e[i].to] = x, dfs(e[i].to), siz[x] += siz[e[i].to];
if(siz[e[i].to] > siz[son[x]]) son[x] = e[i].to;
}
}
void dfs1(int x) {
dfn[x] = ++tot; pre[tot] = x;
if(!top[x]) top[x] = x;
if(son[x]) top[son[x]] = top[x], dfs1(son[x]);
for(int i = head[x]; i; i = e[i].nxt)
if(e[i].to != fa[x] && e[i].to != son[x]) dfs1(e[i].to);
}
void down(int k) {
tr[lson].f += tr[k].f; tr[rson].f += tr[k].f;
tr[lson].w += (tr[lson].r - tr[lson].l + 1) * tr[k].f;
tr[rson].w += (tr[rson].r - tr[rson].l + 1) * tr[k].f;
tr[k].f = 0;
}
void change_query(int k) {
if(tr[k].l >= l && tr[k].r <= r) {tr[k].w += (tr[k].r - tr[k].l + 1) * z;tr[k].f += z; return;}
if(tr[k].f) down(k);
int mid = (tr[k].l + tr[k].r) >> 1;
if(l <= mid) change_query(lson);
if(r > mid) change_query(rson);
tr[k].w = (tr[lson].w + tr[rson].w) % p;
}
void work(int x, int y) {
z %= p;
while(top[x] != top[y]) {
if(dep[top[x]] < dep[top[y]]) swap(x, y);
l = dfn[top[x]], r = dfn[x], change_query(1);
x = fa[top[x]];
}
if(dep[x] > dep[y]) swap(x, y);
l = dfn[x], r = dfn[y]; change_query(1);
}
void ask_query(int k) {
if(tr[k].l >= l && tr[k].r <= r) {ans = (ans + tr[k].w) % p; return;}
if(tr[k].f) down(k);
int mid = (tr[k].l + tr[k].r) >> 1;
if(l <= mid) ask_query(lson);
if(r > mid) ask_query(rson);
tr[k].w = (tr[lson].w + tr[rson].w) % p;
}
void work1(int x, int y) {
while(top[x] != top[y]) {
if(dep[top[x]] < dep[top[y]]) swap(x, y);
l = dfn[top[x]], r = dfn[x]; ask_query(1);
x = fa[top[x]];
}
if(dep[x] > dep[y]) swap(x, y);
l = dfn[x]; r = dfn[y]; ask_query(1);
}
int main() {
n = read(), m = read(), R = read(), p = read();
for(int i = 1; i <= n; i++) w[i] = read();
for(int i = 1; i < n; i++) x = read(), y = read(), add(x, y), add(y, x);
dfs(R); dfs1(R); build(1, 1, n);
while(m--) {
num = read();
if(num == 1) cin >> x >> y >> z, work(x, y);
if(num == 2) ans = 0, cin >> x >> y, work1(x, y), cout << ans << endl;
if(num == 3) cin >> x >> z, l = dfn[x], r = dfn[x] + siz[x] - 1, change_query(1);
if(num == 4) ans = 0, cin >> x, l = dfn[x], r = dfn[x] + siz[x] - 1, ask_query(1), cout << ans << endl;
}
return 0;
}
谢谢收看,祝身体健康!
洛谷p3384【模板】树链剖分题解的更多相关文章
- [洛谷P3384] [模板] 树链剖分
题目传送门 显然是一道模板题. 然而索引出现了错误,狂wa不止. 感谢神犇Dr_J指正.%%%orz. 建线段树的时候,第44行. 把sum[p]=bv[pos[l]]%mod;打成了sum[p]=b ...
- [luogu P3384] [模板]树链剖分
[luogu P3384] [模板]树链剖分 题目描述 如题,已知一棵包含N个结点的树(连通且无环),每个节点上包含一个数值,需要支持以下操作: 操作1: 格式: 1 x y z 表示将树从x到y结点 ...
- 洛谷P3979 遥远的国度 树链剖分+分类讨论
题意:给出一棵树,这棵树每个点有权值,然后有3种操作.操作一:修改树根为rt,操作二:修改u到v路径上点权值为w,操作三:询问以rt为根x子树的最小权值. 解法:如果没有修改树根操作那么这题就是树链剖 ...
- 洛谷 P4114 Qtree1 树链剖分
目录 题面 题目链接 题目描述 输入输出格式 输入格式: 输出格式: 输入输出样例 输入样例: 输出样例: 说明 说明 思路 Change Query AC代码 总结 题面 题目链接 P4114 Qt ...
- 洛谷.4114.Qtree1(树链剖分)
题目链接 模板题都错了这么多次.. //边权赋到点上 树剖模板 //注意LCA.链的顶端不能统计到答案! #include <cstdio> #include <cctype> ...
- 洛谷3384&bzoj1036树链剖分
值得注意的是: 一个点的子树是存在一起的...也就是说我们修改子树的时候只用... /********************************************************* ...
- P3384 [模板] 树链剖分
#include <bits/stdc++.h> using namespace std; typedef long long ll; int n, m, rt, mod, cnt, to ...
- luoguP3384 [模板]树链剖分
luogu P3384 [模板]树链剖分 题目 #include<iostream> #include<cstdlib> #include<cstdio> #inc ...
- 洛谷P2590 [ZJOI2008]树的统计 题解 树链剖分+线段树
题目链接:https://www.luogu.org/problem/P2590 树链剖分模板题. 剖分过程要用到如下7个值: fa[u]:u的父节点编号: dep[u]:u的深度: size[u]: ...
随机推荐
- 【12月13日】A股ROE最高排名
个股滚动ROE = 最近4个季度的归母净利润 / ((期初归母净资产 + 期末归母净资产) / 2). 查看更多个股ROE最高排名 中公教育(SZ002607) - ROE_TTM:92.66% - ...
- Java连载20-复习、switch语句
一.复习 1.标识符(自己定义的,下划线.美元符号) 2.驼峰命名(变量名,方法名首字母小写) 3.关键字(就是固定的那几个) 4.字面值(数据.有类型.八种基本类型从小到大,byte\char=sh ...
- 小程序开发笔记(八)—Js数组按日期分组显示数据
数据分组展示有两种方式,一种是后端直接传入分组格式的Json数据,另一种是我们在前端自己转换格式,这里我们在前端处理转换按日期分组的数据格式 1.例如后端返回数据格式为: [{createtime:' ...
- C# 刷遍 Leetcode 面试题系列连载(3): No.728 - 自除数
前文传送门: C#刷遍Leetcode面试题系列连载(1) - 入门与工具简介 C#刷遍Leetcode面试题系列连载(2): No.38 - 报数 系列教程索引 传送门:https://enjoy2 ...
- 使用SolrJ(即java客户端)开发Solr。
1.什么是SolrJ呢? 答:Solrj是访问Solr服务的java客户端,提供索引和搜索的请求方法,SolrJ通常在嵌入在业务系统中,通过SolrJ的API接口操作Solr服务.开始配置schema ...
- error: open(".vs/ConsoleApp349/v16/Server/sqlite3/db.lock"): Permission denied error: unable to index file
第一种1.git add --ignore-errors . 特别注意 git add --ignore-errors . errors后面有一个空格再加一个点' .' 第二种: 1.touch .g ...
- NET 已知excel表格前面26个是a到z,27是aa28是ab,以此类推,N是多少
问题: 已知excel表格前面26个是a到z,27是aa28是ab,以此类推,N是多少 代码: /// <summary> /// 已知excel表格前面26个是a到z,27是aa28是a ...
- Python - 输入和输出 - 第十七天
Python 输入和输出 在前面几个章节中,我们其实已经接触了 Python 的输入输出的功能.本章节我们将具体介绍 Python 的输入输出. 输出格式美化 Python两种输出值的方式: 表达式语 ...
- 关于前端ajax请求url为何添加一个随机数
一.起因 我在做爬虫的时候发现很多网站上都在url上加一个随机数或者时间戳一开始我以为是啥加密后面发现其实他在后台解析的时候也不需要不排除有些网站他反爬就需要一个时间戳,那他加个随机数是做啥子 二.查 ...
- Java构造函数执行顺序
首先执行基类的构造函数 然后执行派生类的构造函数之外的初始化语句 最后执行派生类的构造函数 在Java中,如果派生类构造函数需要调用基类的构造函数,那么基类构造函数必须作为派生类构造函数的第一句话.在 ...