Change FZU - 2277 毒瘤啊 毒瘤题目
There is a rooted tree with n nodes, number from 1-n. Root’s number is 1.Each node has a value ai.
Initially all the node’s value is 0.
We have q operations. There are two kinds of operations.
1 v x k : a[v]+=x , a[v’]+=x-k (v’ is child of v) , a[v’’]+=x-2*k (v’’ is child of v’) and so on.
2 v : Output a[v] mod 1000000007(10^9 + 7).
Input
First line contains an integer T (1 ≤ T ≤ 3), represents there are T test cases.
In each test case:
The first line contains a number n.
The second line contains n-1 number, p2,p3,…,pn . pi is the father of i.
The third line contains a number q.
Next q lines, each line contains an operation. (“1 v x k” or “2 v”)
1 ≤ n ≤ 3*10^5
1 ≤ pi < i
1 ≤ q ≤ 3*10^5
1 ≤ v ≤ n; 0 ≤ x < 10^9 + 7; 0 ≤ k < 10^9 + 7
Output
For each operation 2, outputs the answer.
Sample Input
1
3
1 1
3
1 1 2 1
2 1
2 2
Sample Output
2
1
题意
节点u加x,节点u的儿子u′加x−k,节点u的孙子u′′加x−2×k,依次类推。 看数据范围标准的线段树
但是这个k不好处理 这个k和深度有关系
所以这题在初始化处理的时候 1LL * x + 1LL * dep[v]*k
这样就便于我们查询了
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
#include <set>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <vector>
#define rtl rt<<1
#define rtr rt<<1|1
#define mem(a,b) memset(a,b,sizeof(a))
#define fuck(x) cout<<"["<<x<<"]"<<endl
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define sffff(a,b,c,d) scanf("%d %d %d %d", &a, &b, &c, &d)
#define FIN freopen("DATA.txt","r",stdin)
#define read(x) scanf("%d", &x)
using namespace std;
typedef long long LL;
const int maxn = 1e6 + ;
const int mod = 1e9 + ;
int t, n, q, tot, dfscnt, head[maxn], L[maxn], R[maxn], dep[maxn];
struct Edge {
int v, nxt;
} edge[maxn << ];
void add(int u, int v) {
edge[tot].v = v;
edge[tot].nxt = head[u];
head[u] = tot++;
}
void dfs(int u, int fa, int d) {
L[u] = ++dfscnt;
dep[u] = d;
for (int i = head[u] ; ~i ; i = edge[i].nxt) {
int v = edge[i].v;
if (v != fa) dfs(v, u, d + );
}
R[u] = dfscnt;
}
struct node {
int l, r;
LL num, k;
int mid() {
return (l + r) >> ;
}
} tree[maxn << ];
void build(int l, int r, int rt) {
tree[rt].l = l, tree[rt].r = r;
tree[rt].num = tree[rt].k = ;
if (l == r) return ;
int m = (l + r) >> ;
build(l, m, rtl);
build(m + , r, rtr);
}
void pushdown(int rt) {
if (tree[rt].num != ) {
tree[rtl].num = (tree[rt].num + tree[rtl].num) % mod;
tree[rtr].num = (tree[rt].num + tree[rtr].num) % mod;
tree[rt].num = ;
}
if (tree[rt].k != ) {
tree[rtl].k = (tree[rt].k + tree[rtl].k) % mod;
tree[rtr].k = (tree[rt].k + tree[rtr].k) % mod;
tree[rt].k = ;
}
} void update(LL a, LL b, int L, int R, int rt) {
if (L <= tree[rt].l && tree[rt].r <= R) {
tree[rt].num = ((tree[rt].num + a) % mod + mod) % mod;
tree[rt].k = ((tree[rt].k + b) % mod + mod) % mod;
return ;
}
pushdown(rt);
int mid = tree[rt].mid();
if (R <= mid) update(a, b, L, R, rtl);
else if(L > mid) update(a, b, L, R, rtr);
else {
update(a, b, L, mid, rtl);
update(a, b, mid + , R, rtr);
}
}
LL query(int pos, int d, int rt) {
if (tree[rt].l == tree[rt].r) {
return ((tree[rt].num - d * tree[rt].k % mod) % mod + mod) % mod;
}
pushdown(rt);
int mid = tree[rt].mid();
if (pos <= mid) return query(pos, d, rtl);
return query(pos, d, rtr);
}
int main() {
int op, v, x, k;
sf(t);
while(t--) {
sf(n);
for (int i = ; i <= n ; i++) head[i] = -;
tot = ;
for (int i = ; i <= n ; i++) {
sf(v);
add(v, i);
add(i, v);
}
dfscnt = ;
dfs(, -, );
build(, n, );
sf(q);
while(q--) {
sf(op);
if (op == ) {
sfff(v, x, k);
update(1LL * x + 1LL * dep[v]*k, k, L[v], R[v], );
} else {
sf(v);
printf("%lld\n", query(L[v], dep[v], ));
}
}
}
return ;
}
Change FZU - 2277 毒瘤啊 毒瘤题目的更多相关文章
- F - Change FZU - 2277 (DFS序+线段树)
题目链接: F - Change FZU - 2277 题目大意: 题意: 给定一棵根为1, n个结点的树. 有q个操作,有两种不同的操作 (1) 1 v k x : a[v] += x, a[v ' ...
- FZU 2277 Change(dfs序+树状数组)
Problem Description There is a rooted tree with n nodes, number from 1-n. Root’s number is 1.Each no ...
- 【FZU 2277】Change
题意 有一颗有n个节点的有根树,根节点编号时1,每个结点都有一个值ai,开始的时候,所有节点的值都是0. 我们有q个操作,操作只有两种类型 1 v x k,a[v]+=x,a[v']+=x-k,a[v ...
- 【HNOI 2018】毒瘤
Problem Description 从前有一名毒瘤. 毒瘤最近发现了量产毒瘤题的奥秘.考虑如下类型的数据结构题:给出一个数组,要求支持若干种奇奇怪怪的修改操作(例如给一个区间内的数同时加上 \(c ...
- [bzoj5287] [HNOI2018]毒瘤
题目描述 从前有一名毒瘤. 毒瘤最近发现了量产毒瘤题的奥秘.考虑如下类型的数据结构题:给出一个数组,要求支持若干种奇奇怪怪的修改操作(比如区间加一个数,或者区间开平方),并支持询问区间和.毒瘤考虑了n ...
- UOJ67 新年的毒瘤【Tarjan,割点】
Online Judge:#uoj 67 Label:Tarjan,割点,细节 题目描述 辞旧迎新之际,喜羊羊正在打理羊村的绿化带,然后他发现了一棵长着毒瘤的树.这个长着毒瘤的树可以用\(n\)个结点 ...
- [HNOI2018]毒瘤
Description 从前有一名毒瘤. 毒瘤最近发现了量产毒瘤题的奥秘.考虑如下类型的数据结构题:给出一个数组,要求支持若干种奇奇怪怪的修改操作(比如区间加一个数,或者区间开平方),并支持询问区间和 ...
- @loj - 2496@ 「AHOI / HNOI2018」毒瘤
目录 @description@ @solution@ @accepted code@ @details@ @description@ 从前有一名毒瘤. 毒瘤最近发现了量产毒瘤题的奥秘.考虑如下类型的 ...
- fzu 1911 Construct a Matrix(矩阵快速幂+规律)
题目链接:fzu 1911 Construct a Matrix 题目大意:给出n和m,f[i]为斐波那契数列,s[i]为斐波那契数列前i项的和.r = s[n] % m.构造一个r * r的矩阵,只 ...
随机推荐
- Ubuntu14.04 panic --not syncing: Attempt to kill init 解决方法
Ubuntu14.04 panic --not syncing: Attempt to kill init 解决方法 工作电脑装了一个虚拟机玩玩,胡乱下载了一些软件,apt-get了不少操作,后来重启 ...
- nginx 重启报错
错误信息: nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or d ...
- PHP正则相关
描述字符串排列模式的一种自定义语法规则 如果可以使用字符串函数处理的任务 就不要使用正则 正则表达式 就是通过构建具有特定规则的模式,与输入的字符信息比较 在进行 分割 匹配 查找 替换 等工作 ...
- [2018 ACL Short and System] 对话系统
Short Paper(s) 1. Task-oriented Dialogue System for Automatic Diagnosis. (Cited by 0) Zhongyu Wei, ...
- Struts2文件上传带进度条,虽然不是很完美
好久没有写东西,最近在做个项目,要用到文件h 传的,以前虽然也做上传,但是总觉得不好用 ,现在和队友合作做了一个带进度条的上传,觉得还行~~和大家分享一下. 首先说一下大概是这样实现的,在我们平时的上 ...
- Bower 显示‘bower ESUDO Cannot be run with sudo’的错误解决方法
使用 sudo 命令后或者当前用户为 root,执行 bower 相关命令会出现错误: 解决办法: 在命令后面加 --allow-root 例: bower init --allow-root bo ...
- 由作业题引发对C++引用的一些思考
首先分析一段代码: #include <bits/c++config.h> #include <ostream> #include <iostream> #incl ...
- tomcat web页面管理应用配置
大部分时候,我们的tomcat服务器都不是部署在本机,那么怎么样不通过ftp/sftp方式来将war包部署到tomcat容器呢? tomcat有提供web页面管理应用的功能. 我们来看看怎么配置实现该 ...
- Oracle查询字段中有空格的数据
一.问题说明 最近在给某个用户下的表批量添加注释时,在程序中将注释名用trim()过滤一遍就可以了,但是在程序执行成功后怎么检测添加的注释名是否有空格存在呢? 二.解决方法 1.SELECT * FR ...
- Java 多线程 三种实现方式
Java多线程实现方式主要有三种:继承Thread类.实现Runnable接口.使用ExecutorService.Callable.Future实现有返回结果的多线程.其中前两种方式线程执行完后都没 ...