给定根为 1 1 1 的一棵树,除了根其它节点度数都最多为 2 2 2。
给定 q q q 组询问:
0 u x d
:将与 u u u 距离最多为 d d d 的节点加上 x x x;1 u
:询问 u u u 的点权。易得根以下的全部是链。
将链提取出来,对于每个询问分讨其是否达到 1 1 1 节点,也是是否能到达别的链的标志。
即使用了树状数组,这样做也会 O ( n q log n ) O(nq\log n) O(nqlogn),比暴力还差。
我们可以发现,很多前缀加都是重复的,考虑合并。
我们设一个 public
数组,存放所有的“前缀加”。
同时,我们设一个 private
数组,存放所有的“区间加”。
这样,我们可以发现,复杂度就变为了 O ( q log n ) O(q\log n) O(qlogn)。
注意:private
和 public
都是 C++ 关键字!
#include
using namespace std;
int link[100001], ind[100001], pub[100001], st[100001];
pair<int, int> deg[100001];
vector<int> pri[100001];
void init_vars(){
// type your initiating code...
}
inline int lowbit(int x){
return x & (-x);
}
void pri_add(int i, int k, int x){
while(k < pri[i].size()){
pri[i][k] += x;
k += lowbit(k);
}
}
int pri_query(int i, int k){
int ans = 0;
while(k > 0){
ans += pri[i][k];
k -= lowbit(k);
}
return ans;
}
void pub_add(int k, int x){
while(k <= 100000){
pub[k] += x;
k += lowbit(k);
}
}
int pub_query(int k){
int ans = 0;
while(k > 0){
ans += pub[k];
k -= lowbit(k);
}
return ans;
}
void solve(int testcase, ...){
init_vars();
int n, q, tot = 0;
cin >> n >> q;
for(int i = 1; i < n; i++){
int u, v; cin >> u >> v;
if(u > v) swap(u, v);
if(u == 1){
link[v] = ++tot, ind[v] = 1, st[tot] = v;
if(!deg[v].first) deg[v].first = 1;
else deg[v].second = 1;
}
else{
if(!deg[u].first) deg[u].first = v;
else deg[u].second = v;
if(!deg[v].first) deg[v].first = u;
else deg[v].second = u;
}
}
for(int i = 1; i <= tot; i++){
int pos = st[i], ord = 1, fa = 1;
//cout << "Link " << i << ": " << pos << " ";
while(deg[pos].first && deg[pos].second){
if(deg[pos].first != fa)
fa = pos, pos = deg[pos].first;
else fa = pos, pos = deg[pos].second;
link[pos] = i, ind[pos] = ++ord;
//cout << pos << " ";
}
//cout << endl;
pri[i].resize(ord + 5);
}
int one_val = 0;
for(int i = 1; i <= q; i++){
int op, v, x, d;
cin >> op;
if(op == 0){
cin >> v >> x >> d;
if(v == 1){
one_val += x;
pub_add(1, x), pub_add(d + 1, -x);
continue;
}
int l = link[v], p = ind[v];
//cout << l << " " << p << endl;
if(p <= d){
//cout << d - p + 1 << " " << d + p + 1 << endl;
pub_add(1, x), pub_add(d - p + 1, -x);
pri_add(l, d - p + 1, x), pri_add(l, d + p + 1, -x);
one_val += x;
}
else{
pri_add(l, p - d, x), pri_add(l, d + p + 1, -x);
}
}
else{
cin >> v;
if(v == 1){
cout << one_val << endl;
continue;
}
int l = link[v], p = ind[v];
//cout << pri_query(l, p) << " " << pub_query(p) << endl;
cout << pri_query(l, p) + pub_query(p) << endl;
}
}
}
signed main(){
#ifdef files
freopen(".in", "r", stdin);
freopen(".out", "w", stdout);
#endif
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
solve(1);
#ifdef files
fclose(stdin); fclose(stdout);
#endif
return 0;
}
/*
* things to check
* 1. int overflow or long long memory need
* 2. recursion/array/binary search/dp/loop bounds
* 3. precision
* 4. special cases(n=1,bounds)
* 5. delete debug statements
* 6. initialize(especially multi-tests)
* 7. = or == , n or m ,++ or -- , i or j , > or >= , < or <=
* 8. keep it simple and stupid
* 9. do not delete, use // instead
* 10. operator priority
* 11. is there anything extra to output?
* 12. THINK TWICE CODE ONCE, THINK ONCE DEBUG FOREVER
* 13. submit ONCE, AC once. submit twice, WA forever
* 14. calm down and you'll get good rank
* 15. even a bit wrong scores zero
* 16. ...
**/
/*
* something to think about
* 1. greedy? dp? searching? dp with matrix/ segment tree? binary search? ...?
* 2. If it is difficult, why not the opposite?
**/
/*
########## ############ ##### #####
#### ##### #### ####
#### ##### #### ####
#### ########## #### ####
#### ##### #####
#### ##### #####
#### ##### #####
########### ############# #####
*/