CF383C Propagating tree
题目
见链接。
题解
知识点:DFS序,树状数组。
我们需要对子树的不同奇偶层加减,用dfn序可以解决子树问题,但是并不能直接分奇偶。
一种比较麻烦的思路是,将dfn序分成两个序列,一个是偶数层点序,一个是奇数层点序列,处理两个序列对于某个点作为子树根节点时,开始和结束节点,然后就可以用线段树分别处理差分。
但实际上,我们不需要对dfn序分离,只需要用两个完整的dfn序,分别统计对奇偶层的改变,每次修改同时修改两个序列的完整子树的差分,但加减不同即可。
其中,两个序列会出现不应该存在的点,比如对于统计偶数层的dfn序出现的奇数层点,那么直接对一个序列做完整子树的修改,会对它们产生错误的修改,但这是无关紧要的。因为我们查询的是单点权值,只要确定我们查询的那个点的奇偶性,去应该出现他的dfn序里查询即可,不存在的点是无法影响答案的。
如果问题改成求一个子树的权值和,那么这种方法就不行了,第一是没法方便的求出一个子树的权值和,因为奇偶层没有分离;第二是对不存在的点做了错误修改,只能用第一种麻烦的方法。
时间复杂度 \(O(n+m\log n)\)
空间复杂度 \(O(n)\)
代码
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct Graph {
struct edge {
int v, nxt;
};
int idx;
vector<int> h;
vector<edge> e;
Graph(int n = 0, int m = 0) { init(n, m); }
void init(int n, int m) {
idx = 0;
h.assign(n + 1, 0);
e.assign(m + 1, {});
}
void add(int u, int v) {
e[++idx] = { v,h[u] };
h[u] = idx;
}
};
struct T {
int sum;
static T e() { return { 0 }; }
T &operator+=(const T &x) { return sum += x.sum, *this; }
};
template<class T>
struct Fenwick {
int n;
vector<T> node;
public:
Fenwick(int _n = 0) { init(_n); }
void init(int _n) {
n = _n;
node.assign(n + 1, T::e());
}
void update(int x, T val) { for (int i = x;i <= n;i += i & -i) node[i] += val; }
T query(int x) {
T ans = T::e();
for (int i = x;i >= 1;i -= i & -i) ans += node[i];
return ans;
}
};
const int N = 200007;
Graph g;
int a[N];
int dfncnt;
int L[N], R[N], dep[N];
void dfs(int u, int fa) {
L[u] = ++dfncnt;
dep[u] = dep[fa] + 1;
for (int i = g.h[u];i;i = g.e[i].nxt) {
int v = g.e[i].v;
if (v == fa) continue;
dfs(v, u);
}
R[u] = dfncnt;
}
int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n, m;
cin >> n >> m;
g.init(n, n << 1);
for (int i = 1;i <= n;i++) cin >> a[i];
for (int i = 1;i <= n - 1;i++) {
int u, v;
cin >> u >> v;
g.add(u, v);
g.add(v, u);
}
dfs(1, 0);
Fenwick<T> fw[2] = { Fenwick<T>(n),Fenwick<T>(n) };
while (m--) {
int op, x;
cin >> op >> x;
if (op == 1) {
int val;
cin >> val;
bool odd = dep[x] & 1;
fw[odd].update(L[x], { val });
fw[odd].update(R[x] + 1, { -val });
fw[odd ^ 1].update(L[x], { -val });
fw[odd ^ 1].update(R[x] + 1, { val });
//! 单点查询可以[L,R]区间加,因为不存在的点不会影响答案
//! 但如果求子树权值和,那只能一开始就把dfs序按深度奇偶性分开,用线段树维护,操作很复杂
}
else cout << a[x] + fw[dep[x] & 1].query(L[x]).sum << '\n';
}
return 0;
}
CF383C Propagating tree的更多相关文章
- 「CF383C Propagating tree」
这应该属于一个比较麻烦的数据结构处理树上问题. 题目大意 给出一颗根节点编号为 \(1\) 的树,对于一个节点修改时在它的子树中对于深度奇偶性相同的节点加上这个权值,不同则减去这个值,单点查询. 分析 ...
- CF383C Propagating tree (线段树,欧拉序)
\(tag\)没开够\(WA\)了一发... 求出\(dfs\)序,然后按深度分类更新与查询. #include <iostream> #include <cstdio> #i ...
- Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+树状数组
C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...
- Codeforces Round #225 (Div. 2) E. Propagating tree dfs序+-线段树
题目链接:点击传送 E. Propagating tree time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
- AC日记——Propagating tree Codeforces 383c
C. Propagating tree time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- CodeForces 383C Propagating tree
Propagating tree Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces ...
- Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+ 树状数组或线段树
C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...
- C. Propagating tree
C. Propagating tree time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 题解 CF383C 【Propagating tree】
这道题明明没有省选难度啊,为什么就成紫题了QAQ 另:在CF上A了但是洛谷Remote Judge玄学爆零. 思路是DFS序+线段树. 首先这道题直观上可以对于每一次修改用DFS暴力O(n),然后对于 ...
- codeforces 383C Propagating tree 线段树
http://codeforces.com/problemset/problem/383/C 题目就是说, 给一棵树,将一个节点的值+val, 那么它的子节点都会-val, 子节点的子节点+val. ...
随机推荐
- 打 multi-fidelity RL 旗号,但是幼稚监督学习 + 迁移学习
文章名称:Multi-fidelity reinforcement learning framework for shape optimization 链接:https://www.sciencedi ...
- Ubuntu安装jdk的步骤
.markdown-body { line-height: 1.75; font-weight: 400; font-size: 16px; overflow-x: hidden; color: rg ...
- SD Host控制器的系统集成
控制器集成需求 SD Host可以从外部读取数据存储到SRAM和eFlash或者可以从内部读取数据输出到外部存储 AHB Bus slave接口用于配置 master接口作为主设备,DMA可以控制总线 ...
- cout对象在全局只能拥有一个
1.问题 在学习符号重载的过程中,有一个想法 std::ostream& operator<<(std::ostream &cout, Person &p); 中s ...
- [转帖]Web技术(五):HTTP/2 是如何解决HTTP/1.1 性能瓶颈的?
文章目录 一.HTTP/2 概览 二.HTTP/2 协议原理 2.1 Binary frame layer 2.1.1 DATA帧定义 2.1.2 HEADERS帧定义 2.2 Streams and ...
- [转帖]MOUNTING AN S3 BUCKET ON WINDOWS AND LINUX
https://blog.spikeseed.cloud/mount-s3-as-a-disk/#mounting-an-s3-bucket-on-windows-server-2016 Wouldn ...
- [转帖]Jmeter_jmeter-plugins插件的安装使用
一.安装JMter Plugins 1.官网下载 JMeter Plugins 的jar包 2. 将下载的jar包复制到 %JMETER_HOME%\lib\ext 目录下 3. 启动 JMeter ...
- NativeMemoryTracking的再学习
摘要 最近一段时间学习jvm比较多. 为了能够更加深入的进行一些调优和峰值性能的配置. 看了很多像是NMT,inline,堆区方法区以及分层编译等知识. 但是看到华为毕昇社区说的codecache相关 ...
- 【JS 逆向百例】某音 X-Bogus 逆向分析,JSVMP 纯算法还原
声明 本文章中所有内容仅供学习交流使用,不用于其他任何目的,不提供完整代码,抓包内容.敏感网址.数据接口等均已做脱敏处理,严禁用于商业用途和非法用途,否则由此产生的一切后果均与作者无关! 本文章未经许 ...
- RC4Drop加密:提升数据保护的新选择
摘要:RC4Drop是一种基于RC4算法的加密技术,通过将明文数据分成多个部分并进行加密,实现了对数据的高效保护.本文将对RC4Drop加密技术的优缺点进行详细分析,并给出一个Java完整demo示例 ...