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的矩阵,只 ...
随机推荐
- lintcode514 栅栏染色
栅栏染色 我们有一个栅栏,它有n个柱子,现在要给柱子染色,有k种颜色可以染.必须保证不存在超过2个相邻的柱子颜色相同,求有多少种染色方案. 注意事项 n和k都是非负整数 您在真实的面试中是否遇到过这个 ...
- 周期串 (Periodic Strings,UVa455)
#include<stdio.h> #include<string.h> int main(void) { int n,stlen,i,j; ]; while(scanf(&q ...
- Memcache的客户端连接系列(四) PHP
关键词: Memcached PHP 客户端 声明:本文并非原创,转自华为云帮助中心的分布式缓存服务(Memcached)的用户指南.客户端连接方法通用,故摘抄过来分享给大家. PHP客户端 Re ...
- Python3 下安装python-votesmart
在python2下安装python-smart还比较容易,而python3中由于很多函数库的变化直接使用python setup.py install 命令来安装的话会导致错误,而导致错误的原因就是p ...
- 【主席树维护mex】 【SG函数递推】 Problem H. Cups and Beans 2017.8.11
Problem H. Cups and Beans 2017.8.11 原题: There are N cups numbered 0 through N − 1. For each i(1 ≤ i ...
- idea clion编译器
RNMV64P0LA-eyJsaWNlbnNlSWQiOiJSTk1WNjRQMExBIiwibGljZW5zZWVOYW1lIjoiY24gdHUiLCJhc3NpZ25lZU5hbWUiOiIiL ...
- 二 Capacity Scheduler 计算能力调度器
官网的写的太难懂,参考:http://www.360doc.com/content/14/0603/14/14935022_383254798.shtml Capacity Scheduler 一种可 ...
- 互评Alpha版本——Thunder团队
基于NABCD评论作品 Hello World! :http://www.cnblogs.com/120626fj/p/7807544.html 欢迎来怼 :http://www.cnblogs.co ...
- ACM 第十二天
博弈论(巴什博奕,威佐夫博弈,尼姆博弈,斐波那契博弈,SG函数,SG定理) 一. 巴什博奕(Bash Game): A和B一块报数,每人每次报最少1个,最多报4个,看谁先报到30.这应该是最古老的关 ...
- iOS APP中第三方APP调用自己的APP,打开文件
根据需求需要在项目中要打开word.pdf.excel等文件,在info.plist文件中添加 <key>CFBundleDocumentTypes</key> <arr ...