Codeforces1076E. Vasya and a Tree(dfs+离线+动态维护前缀和)
题目链接:传送门
题目:
- E. Vasya and a Tree
- time limit per test
- seconds
- memory limit per test
- megabytes
- input
- standard input
- output
- standard output
- Vasya has a tree consisting of n
- vertices with root in vertex . At first all vertices has
- written on it.
- Let d(i,j)
- be the distance between vertices i and j, i.e. number of edges in the shortest path from i to j. Also, let's denote k-subtree of vertex x — set of vertices y
- such that next two conditions are met:
- x
- is the ancestor of y
- (each vertex is the ancestor of itself);
- d(x,y)≤k
- .
- Vasya needs you to process m
- queries. The i-th query is a triple vi, di and xi. For each query Vasya adds value xi to each vertex from di-subtree of vi
- .
- Report to Vasya all values, written on vertices of the tree after processing all queries.
- Input
- The first line contains single integer n
- (≤n≤⋅
- ) — number of vertices in the tree.
- Each of next n−
- lines contains two integers x and y (≤x,y≤n) — edge between vertices x and y
- . It is guarantied that given graph is a tree.
- Next line contains single integer m
- (≤m≤⋅
- ) — number of queries.
- Each of next m
- lines contains three integers vi, di, xi (≤vi≤n, ≤di≤, ≤xi≤) — description of the i
- -th query.
- Output
- Print n
- integers. The i-th integers is the value, written in the i
- -th vertex after processing all queries.
- Examples
- Input
- Copy
- Output
- Copy
- Input
- Copy
- Output
- Copy
- Note
- In the first exapmle initial values in vertices are ,,,,
- . After the first query values will be equal to ,,,,. After the second query values will be equal to ,,,,. After the third query values will be equal to ,,,,.
题目大意:
给定一棵有N个节点的有向树,根节点为1。
有M次操作,对以vi为根的深度为di的子树上的所有节点权值加xi。
1≤n,m≤3⋅105,1 ≤ vi ≤ n,0 ≤ di ≤ 109,1 ≤ xi ≤ 109
思路:
离线处理,把每个更新都放到对应的vi上,然后从节点1开始dfs。
dfs时遇到一个节点后,把这个节点所有的“操作”都拿出来:
对于每个“操作”,更新当前深度到本次操作影响的最大深度[dep, dep+di]区间内的值加上xi,回溯的时候再减掉xi。这里可以用树状数组维护。
每个节点的答案就是被搜索到之后,“操作”结束之后的当前深度的值。
到这里就已经可以AC了。
但是树状数组的logn的复杂度还可以继续优化,用一个前缀和数组sum维护,更新[dep, dep+di]区间时,只要让sum[dep] += xi,sum[dep+di+1] -= xi,就可以实现整个区间的加减了。
有人问(就是我):“中间的明明没有加上去啊???”
“。。。对,但是你是一个个跑过来的啊,把之前的前缀加过来不就好了?”(w神)
period。
UPDATE:
其实就是dfs的时候顺便维护前缀和,遇到当前深度的时候把当前深度的权值(可能是负的)都捡起来,然后进入下一层,回溯的时候再把捡起来的权值都丢掉。
代码:
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- const int MAX_NM = ;
- int n, m, t, act;
- string opt[];
- string acts[];
- ll F[MAX_NM], A[][MAX_NM][MAX_NM], AAA[MAX_NM][MAX_NM];
- inline int num(int i, int j) {
- return (i-)*m + j;
- }
- void mul(ll f[MAX_NM], ll a[MAX_NM][MAX_NM]) {
- ll c[MAX_NM];
- memset(c, , sizeof c);
- for (int j = ; j < MAX_NM; j++)
- for (int k = ; k < MAX_NM; k++)
- c[j] += f[k] * a[k][j];
- memcpy(f, c, sizeof c);
- }
- void mulb(ll a[MAX_NM][MAX_NM], ll b[MAX_NM][MAX_NM]) {
- ll c[MAX_NM][MAX_NM];
- memset(c, , sizeof c);
- for (int i = ; i < MAX_NM; i++)
- for (int j = ; j < MAX_NM; j++)
- for (int k = ; k < MAX_NM; k++)
- c[i][j] += a[i][k]*b[k][j];
- memcpy(a, c, sizeof c);
- }
- void mulself(ll a[MAX_NM][MAX_NM]) {
- ll c[MAX_NM][MAX_NM];
- memset(c, , sizeof c);
- for (int i = ; i < MAX_NM; i++)
- for (int j = ; j < MAX_NM; j++)
- for (int k = ; k < MAX_NM; k++)
- c[i][j] += a[i][k]*a[k][j];
- memcpy(a, c, sizeof c);
- }
- void init()
- {
- memset(A, , sizeof A);
- memset(F, , sizeof F);
- F[] = ;
- for (int k = ; k < ; k++) {
- A[k][][] = ;
- for (int i = ; i <= n; i++) {
- for (int j = ; j <= m; j++) {
- int index = opt[i-][j-] - '';
- int indey = k % acts[index].size();
- char ch = acts[index][indey];
- if (isupper(ch)) {
- switch (ch) {
- case 'N':
- if (i- >= )
- A[k][num(i, j)][num(i-, j)] = ; break;
- case 'S':
- if (i+ <= n)
- A[k][num(i, j)][num(i+, j)] = ; break;
- case 'W':
- if (j- >= )
- A[k][num(i, j)][num(i, j-)] = ; break;
- case 'E':
- if (j+ <= m)
- A[k][num(i, j)][num(i, j+)] = ; break;
- case 'D':
- A[k][num(i, j)][num(i, j)] = ;
- }
- }
- if (isdigit(ch)) {
- A[k][num(i, j)][num(i, j)] = ;
- A[k][][num(i, j)] = ch - '';
- }
- }
- }
- }
- for (int i = ; i < MAX_NM; i++)
- AAA[i][i] = ;
- for (int k = ; k < ; k++)
- mulb(AAA, A[k]);
- }
- int main()
- {
- cin >> n >> m >> t >> act;
- for (int i = ; i < n; i++)
- cin >> opt[i];
- for (int i = ; i < act; i++)
- cin >> acts[i];
- init();
- int q = t/;
- int r = t%;
- // t = q*60 + r;
- for (; q; q >>= ) {
- if (q&)
- mul(F, AAA);
- mulself(AAA);
- }
- for (int i = ; i < r; i++)
- mul(F, A[i]);
- ll ans = ;
- for (int i = ; i < MAX_NM; i++)
- ans = max(ans, F[i]);
- cout << ans << endl;
- return ;
- }
参考博客:
Codeforces1076E. Vasya and a Tree(dfs+离线+动态维护前缀和)的更多相关文章
- CF Edu54 E. Vasya and a Tree DFS+树状数组
Vasya and a Tree 题意: 给定一棵树,对树有3e5的操作,每次操作为,把树上某个节点的不超过d的子节点都加上值x; 思路: 多开一个vector记录每个点上的操作.dfs这颗树,同时以 ...
- CodeForces-1076E Vasya and a Tree
CodeForces - 1076E Problem Description: Vasya has a tree consisting of n vertices with root in verte ...
- HH的项链 树状数组动态维护前缀
#include<cstdio> #include<algorithm> #include<cstring> using namespace std; const ...
- BZOJ2690: 字符串游戏(平衡树动态维护Dfs序)
Description 给定N个仅有a~z组成的字符串ai,每个字符串都有一个权值vi,有M次操作,操作分三种: Cv x v':把第x个字符串的权值修改为v' Cs x a':把第x个字符串修改成a ...
- Vasya and a Tree CodeForces - 1076E(线段树+dfs)
I - Vasya and a Tree CodeForces - 1076E 其实参考完别人的思路,写完程序交上去,还是没理解啥意思..昨晚再仔细想了想.终于弄明白了(有可能不对 题意是有一棵树n个 ...
- CF1076E:Vasya and a Tree(DFS&差分)
Vasya has a tree consisting of n n vertices with root in vertex 1 1 . At first all vertices has 0 0 ...
- Vasya and a Tree CodeForces - 1076E (线段树 + dfs)
题面 Vasya has a tree consisting of n vertices with root in vertex 1. At first all vertices has 0 writ ...
- Codeforces 1076 E - Vasya and a Tree
E - Vasya and a Tree 思路: dfs动态维护关于深度树状数组 返回时将当前节点的所有操作删除就能保证每次访问这个节点时只进行过根节点到当前节点这条路径上的操作 代码: #pragm ...
- BZOJ3159决战——树链剖分+非旋转treap(平衡树动态维护dfs序)
题目描述 输入 第一行有三个整数N.M和R,分别表示树的节点数.指令和询问总数,以及X国的据点. 接下来N-1行,每行两个整数X和Y,表示Katharon国的一条道路. 接下来M行,每行描述一个指令或 ...
随机推荐
- NVIDIA 驱动安装(超详细)
目录 1. 删除原有驱动 2. 安装依赖 3. 禁用nouveau驱动: 4. reboot 5. 获取kernel source (important) 6. 关掉x graphic 服务 7. 安 ...
- Java中的CAS实现原理
一.什么是CAS? 在计算机科学中,比较和交换(Conmpare And Swap)是用于实现多线程同步的原子指令. 它将内存位置的内容与给定值进行比较,只有在相同的情况下,将该内存位置的内容修改为新 ...
- Rails-Treasure chest2 嵌套表单;
嵌套表单1-1 嵌套表单1-多 选日期时间的UI (一个jquery Plugin) 拆除前后台css和js Rich Editor, 显示输入的HTML tag 批次编辑/删除 嵌套表单1-1 核心 ...
- SpringBoot之依赖注入DI
相关注解: @Component @Service @Controller @Repository --------------------------------------------- @Inj ...
- Bean的Scope
Bean的scope: 1.Singleton(单例): 一个Spring容器只有以这个Bean实例. 2.prototype(多例): 每次调用新建一个Bean的实例. 3.request:一个ht ...
- Systemd程序及相关命令
Systemd程序及相关命令 Systemd是一款用于Linux操作系统系统管理和服务管理的工具.它向后兼容SysV init脚本,并且支持许多类似于startup系统服务的功能,比如系统快照(sna ...
- Qt 布局管理
在布局编辑环境里: sizePolicy 影响控件在布局上的大小. layout 的属性 如:如margin 设置控件在布局上边距. 有时候需要设置 下面是一个 代码布局的方式 #include &q ...
- Python—迭代器与生成器
迭代器与生成器 生成器(generator) 先来了解一下列表生成器: list = [i*2 for i in range(10)] print(list)>>>>[0, 2 ...
- react+classnames
之前做项目的时候一直不知道有不知道有classnames这个东西,一直用的都是字符串拼接,感觉用的很别扭. classnames用法和angular1.x及vue差不多,所以用起来会比较顺手 1)安装 ...
- ORA-12638: 身份证明检索失败的解决方法
本地oracle客户端用PLSQL Developer连接远程数据库,每次登录都会在很久之后,出现 ORA-12638: 身份证明检索失败,tnsping 表明TNS配置没有问题. 解决方案: D:\ ...