首页 > 其他分享 >Luogu P3833 [SHOI2012]魔法树

Luogu P3833 [SHOI2012]魔法树

时间:2022-10-25 14:06:29浏览次数:100  
标签:fr int Luogu ca tree dfn P3833 include SHOI2012


题目链接:​​传送门​

树剖板子
区间加,子树查询
树剖里查询的时候x和y地方小于号写反T了一会
a,b写成dfn[a],dfn[b]竟然还有50分又WA了一会
也就交了二十遍

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <complex>
#include <algorithm>
#include <climits>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define
#define

using namespace std;
typedef long long ll;
int n, m, cnt, a, b, q; ll c;
int fa[A], dfn[A], siz[A], dep[A], top[A], son[A];
namespace Seg {
struct node {
int l, r; ll w, f;
}tree[A << 2];
void build(int k, int l, int r) {
tree[k].l = l; tree[k].r = r;
if (l == r) {
tree[k].w = 0;
return;
}
int m = (l + r) >> 1;
build(k << 1, l, m);
build(k << 1 | 1, m + 1, r);
}
void down(int k) {
tree[k << 1].f += tree[k].f;
tree[k << 1 | 1].f += tree[k].f;
tree[k << 1].w += (tree[k << 1].r - tree[k << 1].l + 1) * tree[k].f;
tree[k << 1 | 1].w += (tree[k << 1 | 1].r - tree[k << 1 | 1].l + 1) * tree[k].f;
tree[k].f = 0;
}
void change(int k, int l, int r, ll add) {
if (tree[k].l >= l and tree[k].r <= r) {
tree[k].w += (tree[k].r - tree[k].l + 1) * add;
tree[k].f += add;
return;
}
if (tree[k].f) down(k);
int m = (tree[k].l + tree[k].r) >> 1;
if (l <= m) change(k << 1, l, r, add);
if (r > m) change(k << 1 | 1, l, r, add);
tree[k].w = tree[k << 1].w + tree[k << 1 | 1].w;
}
ll ask(int k, int l, int r) {
if (tree[k].l >= l and tree[k].r <= r) return tree[k].w;
if (tree[k].f) down(k);
int m = (tree[k].l + tree[k].r) >> 1; ll ans = 0;
if (l <= m) ans += ask(k << 1, l, r);
if (r > m) ans += ask(k << 1 | 1, l, r);
return ans;
}
}
namespace Cut {
struct node {
int next, to;
}e[A];
int head[A], num;
void add(int fr, int to) {
e[++num].next = head[fr];
e[num].to = to;
head[fr] = num;
}
void prepare(int fr) {
siz[fr] = 1;
for (int i = head[fr]; i; i = e[i].next) {
int ca = e[i].to;
if (ca == fa[fr]) continue;
fa[ca] = fr;
dep[ca] = dep[fr] + 1;
prepare(ca);
siz[fr] += siz[ca];
if (siz[ca] > siz[son[fr]]) son[fr] = ca;
}
}
void dfs(int fr, int tp) {
dfn[fr] = ++cnt, top[fr] = tp;
if (!son[fr]) return;
dfs(son[fr], tp);
for (int i = head[fr]; i; i = e[i].next) {
int ca = e[i].to;
if (ca == fa[fr] or ca == son[fr]) continue;
dfs(ca, ca);
}
}
void change(int x, int y, ll val) {
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]]) swap(x, y);
Seg::change(1, dfn[top[x]], dfn[x], val);
x = fa[top[x]];
}
if (dep[x] > dep[y]) swap(x, y);
Seg::change(1, dfn[x], dfn[y], val);
}
ll ask(int a) {
return Seg::ask(1, dfn[a], dfn[a] + siz[a] - 1);
}
}


int main(int argc, char const *argv[]) {
scanf("%d", &n);
for (int i = 2; i <= n; i++) {
scanf("%d%d", &a, &b); a++; b++;
Cut::add(a, b);
}
Cut::prepare(1); Cut::dfs(1, 1);
Seg::build(1, 1, n);
scanf("%d", &q);
while (q--) {
char opt = getchar();
if (opt == 'A') {
scanf("%d%d%lld", &a, &b, &c); a++; b++;
Cut::change(a, b, c);
}
else {
scanf("%d", &a); a++;
printf("%lld\n", Cut::ask(a));
}
}
}


标签:fr,int,Luogu,ca,tree,dfn,P3833,include,SHOI2012
From: https://blog.51cto.com/lyle/5794654

相关文章

  • Luogu P1438 无聊的数列
    题目链接:​​传送门​​还是区间加等差数列时隔8个月再写一遍这个题不会的来​这里#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include......
  • Luogu P3980 [NOI2008]志愿者招募
    题目链接:​​传送门​​别人家的建图~~~~好神奇很容易想到志愿者的起始时间和终止时间连边,费用就是他的费用但是每个点还有一个人数限制必须要有那么多个人也就是那么......
  • Luogu P2221 [HAOI2012]高速公路
    题目链接:​​传送门​​维护路径期望值,带区间修改看每条路径会被统计多少次贡献非常不显然是方案数就是上面的是分子下面的是分母现在要把上面的展开看怎么维护直接......
  • Luogu P2342 叠积木
    题目链接:​​传送门​​虽然看着很简单还是计较了好久!fa[]是这堆的最底下那块的编号siz[]是底块所在堆的sizeans[]就是ans,这块底下有多少块在find的时候沿路更新答案即......
  • Luogu P2150 [NOI2015]寿司晚宴
    题目链接:​​传送门​​太难了太难了题意就是问有多少种分案把一个到的排列分配为两组并使组间元素两两互质首先我们只需要考虑根号内的质因子对答案的影响,因为根号外的因......
  • Luogu P2515 [HAOI2010]软件安装
    题目链接:​​传送门​​很明显,如果图中有一个环那么这个环上的点必须都要选那我们一开始就直接缩点因为每个物品有价值有重量还有有重量限制所以是很明显的树上背包我......
  • Luogu P4421 [COCI2017-2018#1] Lozinke
    题目链接:​​传送门​​一开始直接AC自动机每个串暴力跳fail显然会T,44分#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<complex>#i......
  • Luogu P3182 [HAOI2016]放棋子
    题目链接:​​传送门​​题目说了每行有一个障碍两个障碍不在同一行也不在同一列那障碍放哪里就没关系了矩阵都不用输入或者这样理解:交换矩阵的某两行对答案是没有影响......
  • Luogu P1772 [ZJOI2006]物流运输
    题目链接:​​传送门​​很麻烦也很难想的一道题数据很小大胆yy详细解释在代码里#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<co......
  • Luogu P4149 [IOI2011]Race
    题目链接:​​传送门​​#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<complex>#include<algorithm>#include<climits>#include<......