题意:
给定一棵树,对树有3e5的操作,每次操作为,把树上某个节点的不超过d的子节点都加上值x;
思路:
多开一个vector记录每个点上的操作。dfs这颗树,同时以深度开一个树状数组,踩到u节点的时候,给数组add(deep, x); add(min(maxn,deep + op[u][i].fi + 1), - op[u][i].se);
搜索下去,反回前得到这个节点的答案,并消去这个点的影响。
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
//#pragma GCC optimize(3) //#pragma comment(linker, "/STACK:102400000,102400000") //c++ // #pragma GCC diagnostic error "-std=c++11" // #pragma comment(linker, "/stack:200000000") // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #include <algorithm> #include <iterator> #include <iostream> #include <cstring> #include <cstdlib> #include <iomanip> #include <bitset> #include <cctype> #include <cstdio> #include <string> #include <vector> #include <stack> #include <cmath> #include <queue> #include <list> #include <map> #include <set> #include <cassert> using namespace std; #define lson (l , mid , rt << 1) #define rson (mid + 1 , r , rt << 1 | 1) #define debug(x) cerr << #x << " = " << x << "\n"; #define pb push_back #define pq priority_queue typedef long long ll; typedef unsigned long long ull; //typedef __int128 bll; typedef pair<ll ,ll > pll; typedef pair<int ,int > pii; typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q //priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q #define fi first #define se second //#define endl '\n' #define OKC ios::sync_with_stdio(false);cin.tie(0) #define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行 #define REP(i , j , k) for(int i = j ; i < k ; ++i) #define max3(a,b,c) max(max(a,b), c); //priority_queue<int ,vector<int>, greater<int> >que; const ll mos = 0x7FFFFFFF; //2147483647 const ll nmos = 0x80000000; //-2147483648 const int inf = 0x7f7f7f7f; const ll inff = 0x3f3f3f3f3f3f3f3f; //18 const int mod = 1e8+7; const double esp = 1e-8; const double PI=acos(-1.0); const double PHI=0.61803399; //黄金分割点 const double tPHI=0.38196601; template<typename T> inline T read(T&x){ x=0;int f=0;char ch=getchar(); while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar(); while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x=f?-x:x; } /*-----------------------showtime----------------------*/ const int maxn = 3e5+9; ll ans[maxn]; vector<pii> op[maxn]; vector<int>mp[maxn]; ll sum[maxn]; int lowbit(int x){ return x & (-x); } void add(int x,int val){ while(x < maxn){ sum[x] += val; x += lowbit(x); } } ll getsum(int x){ ll res = 0; while(x > 0){ res += sum[x]; x -= lowbit(x); } return res; } void dfs(int u, int fa, int deep){ for(int i=0; i<op[u].size(); i++){ add(deep, op[u][i].se); add(min(maxn,deep + op[u][i].fi + 1), -1ll*op[u][i].se); } for(int i=0; i<mp[u].size(); i++){ int v = mp[u][i]; if(v == fa)continue; dfs(v, u, deep + 1); } ans[u] = getsum(deep); for(int i=0; i<op[u].size(); i++){ add(deep, -1ll*op[u][i].se); add(min(maxn,deep + op[u][i].fi + 1), op[u][i].se); } } int main(){ int n,m; scanf("%d", &n); for(int i=1; i<n; i++){ int u,v; scanf("%d%d", &u, &v); mp[u].pb(v); mp[v].pb(u); } scanf("%d", &m); for(int i=1; i<=m; i++){ int v,d,x; scanf("%d%d%d", &v, &d, &x); op[v].pb(pii(d,x)); } dfs(1, -1, 1); for(int i=1; i<=n; i++) printf("%lld ", ans[i]); puts(""); return 0; }