1. 程式人生 > >Educational Codeforces Round 54 E

Educational Codeforces Round 54 E

題意:

給定一棵包含n個結點的樹,開始每個結點權值為0,現在有q個操作,每個操作包含 v, d, x,表示第v號結點,以及再往下(對於樹:他的孩子方向)遍歷d層,訪問到的結點權值都加上x;

輸出所有結點的權值

思路:

一下想到的就是區間更新,單點查詢,想寫個樹剖來著,感覺有點麻煩,然後就想到了樹狀陣列很快的那個區間更新,字首和當單點查詢的操作,

然後再想想就想到了把所有操作離線出來,然後dfs遍歷樹的時候更新每個結點的操作,回溯的時候在更新回去,這樣在更新到某個結點的時候,他的祖先結點的所有更新都全了;

複雜度O(n*log(maxn)+q)吧算是;

#include<bits/stdc++.h>
using namespace std;
#define out fflush(stdout);
#define fast ios::sync_with_stdio(0),cin.tie(0);
#define FI first
#define SE second
typedef long long ll;
typedef pair<ll,ll> P;
const int maxn = 3e5;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll mod = 998244353;

int n, m;
ll v, d, x;
vector<int> vec[maxn+7];

ll c[maxn + 7];
void add(int id, ll x) {
    for(int i = id; i <= maxn; i += (i&-i)) {
        c[i] += x;
    }
}
ll sum(int id) {
    ll res = 0;
    for(int i = id; i > 0; i -= (i&-i)) {
        res += c[i];
    }
    return res;
}

vector<P> q[maxn+7];
ll ans[maxn+7];

void dfs(int id, int cnt, int f) {
    for(auto i : q[id]) {
        ll d = i.FI, x = i.SE;
        add(cnt, x);
        add(cnt+d+1, -x);
    }
    ans[id] = sum(cnt);
    for(auto i : vec[id]) {
        if(i == f) continue;
        dfs(i, cnt+1, id);
    }
    for(auto i : q[id]) {
        ll d = i.FI, x = i.SE;
        add(cnt, -x);
        add(cnt+d+1, x);
    }
}

int main() {
    scanf("%d", &n);
    int u, v;
    for(int i = 1; i < n; ++i) {
        scanf("%d%d", &u, &v);
        vec[u].push_back(v);
        vec[v].push_back(u);
    }
    scanf("%d", &m);
    for(int i = 1; i <= m; ++i) {
        scanf("%lld%lld%lld", &v, &d, &x);
        q[v].push_back(P(d,x));
    }

    dfs(1, 1, -1);
    for(int i = 1; i <= n; ++i) {
        printf("%lld%c", ans[i], (i == n ? '\n' : ' '));
    }
    return 0;
}

相關推薦

Educational Codeforces Round 54 E - Vasya and a Tree 樹上:離線+dfs+樹狀陣列

題意: 給定一棵包含n個結點的樹,開始每個結點權值為0,現在有q個操作,每個操作包含 v, d, x,表示第v號結點,以及再往下(對於樹:他的孩子方向)遍歷d層,訪問到的結點權值都加上x; 輸出所有結點的權值 思路: 一下想到的就是區間更新,單點查詢,想寫個樹剖來著,感覺有點麻煩

1076E/Educational Codeforces Round 54-E. Vasya and a Tree<<dfs序 樹狀陣列

題意 給定一棵樹,初始每個節點權值為零,q次更改,每次修改將以v為頂點的深度為d的子樹全部加上x,最後輸出所有節點的權重。 思路 題目只要求每個點最後的值,那麼經過觀察,發現一個點最後的權值大小隻與他的父節點的更新有關,那麼我們就只需要考慮他的父節點到他這條鏈上的情況,把這條鏈拿出來成為線段,然後維護字

Educational Codeforces Round 54 E. Vasya and a Tree 樹上字首和或樹狀陣列

文章目錄 Educational Codeforces Round 54 E. Vasya and a Tree 樹上字首和 樹狀陣列 分析 Educational Codeforces

Educational Codeforces Round 54 E

題意: 給定一棵包含n個結點的樹,開始每個結點權值為0,現在有q個操作,每個操作包含 v, d, x,表示第v號結點,以及再往下(對於樹:他的孩子方向)遍歷d層,訪問到的結點權值都加上x; 輸出所有結點的權值 思路: 一下想到的就是區間更新,單點查詢,想寫個樹剖來著,

Educational Codeforces Round 54 E. Vasya and a Tree 樹上字首和或樹狀陣列

Educational Codeforces Round 54 E. Vasya and a Tree 樹上字首和 #include <bits/stdc++.h> #define m

Educational Codeforces Round 54 (Rated for Div. 2) A B C D E題解

這些題目挺有意思,起碼我都錯過,可能這兩天精力有點不足,腦子不太夠用???   A題連結:http://codeforces.com/contest/1076/problem/A 題意:給定一個字串,最多可以刪掉一個字元,使得字典序最小; 思路:首先跟原串比較的話,某一

Educational Codeforces Round #54 (Div. 2) E. Vasya and a Tree 技巧題

題目連線:http://codeforces.com/contest/1076/problem/E   本題大意:   給一棵根節點為“1”樹,給m個操作,每個操作三個整數,v,d,x,意思是從節點1,往下深度d,遍及的節點的值都加上x,d可能是0,就是隻加在自己上。結束m個操作後輸出每個節點的值。 &

Educational Codeforces Round 54 (Rated for Div. 2) E. Vasya and a Tree(dfs+思維)

E. Vasya and a Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output

Educational Codeforces Round 54 (Rated for Div. 2) E. Vasya and a Tree】 dfs+樹狀陣列

HEU 大三蒟蒻一枚 有任何問題或建議都可以加Q聯絡我^_^ QQ : 986195894 CodeForces id : lajiyuan CodeForces id : biubiubiu_ Vjudge id : heu2016201206

Educational Codeforces Round 22 E. Army Creation 主席樹 或 分塊

tor ron following time long different value comm member E. Army Creation As you might remember from our previous

Educational Codeforces Round 23 E. Choosing The Commander (trie)

ati -- main 刪除 span std ble targe pla 題目鏈接: Educational Codeforces Round 23 E. Choosing The Commander 題意: 一共有n個操作。 1. 插入一個數p 2. 刪除一個數p

Educational Codeforces Round 22 E. Army Creation(主席樹)

type sin -- cat clu 方法 cto hid 一個數 題目鏈接:Educational Codeforces Round 22 E. Army Creation 題意: 給你n個數和一個數k,然後有q個詢問. 每個詢問 有一個區間[l,r],問你這個區間內在

Educational Codeforces Round 32 E. Maximum Subsequence

push_back () bound efi sin 輸出 eof include -i E. Maximum Subsequence 題意: n 個數,選出其中 k 個數,使得他們的和對 m 取模後最大。 輸出這個最大值。 tags:註意到 n 很小, 所以折半枚

Educational Codeforces Round 35 E. Stack Sorting 模擬

esp 可能 turn -- ref spa make force 排序 Educational Codeforces Round 35 E. Stack Sorting 題意:長度為 n 的序列 a[] ,a[] 裏的數是 1~n,一個空棧 s,一個空序列 b[]。兩

Educational Codeforces Round 37 E】Connected Components?

com 很快 之間 include mar while 它的 所有 conn 【鏈接】 我是鏈接,點我呀:) 【題意】 在這裏輸入題意 【題解】 bfs. 用一個鏈表來記錄哪些點已經確定在某一個聯通快裏了。 一開始每個點都能用。 然後從第一個點開始進行bfs

Educational Codeforces Round 41 E. Tufurama (961E)

pri code 記錄 pan info clas ret for IT 【題解】   第一眼看題飛快地想到一種做法,然後假掉了。   這道題其實是主席樹的模板題來著。但是也有別的水法。   我們可以發現每個位置的查詢區間是[1,min(a[i],i-1)],所以我們可以

Educational Codeforces Round 44 - E. Pencils and Boxes

一個 std splay OS http string for lin push

dsu on tree(Educational Codeforces Round 2: E. Lomsat gelral)

  題意: 一棵n個節點的樹,每個節點都有一種顏色,如果顏色c在以u為根的子樹中出現的次數大於等於一半,那麼這個顏色就是u節點的支配色, 因為是大於等於,所以一個節點的支配色可能不止一種,求出每個節點的支配色編號和   思路: 一個無腦的暴力:

Codeforces Educational Codeforces Round 54 題解

EDA 離線 closed gif targe lse viso 最小值 round 題目鏈接:https://codeforc.es/contest/1076 A. Minimizing the String 題意:給出一個字符串,最多刪掉一個字母,輸出操作後字典序最小的