Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define all(x) begin(x),end(x)
- using namespace std;
- using ll = long long;
- const int N = 500500;
- const ll mod = 998244353;
- vector<int> g[N];
- ll a[N];
- ll f[N];
- vector<int> path;
- int ind[N];
- int from[N];
- int to[N];
- void Dfs(int f, int p) {
- from[f] = path.size();
- path.push_back(f);
- for (int v : g[f]) {
- if (v == p) continue;
- Dfs(v, f);
- }
- to[f] = path.size() - 1;
- }
- ll t[N];
- void add(int i, int val) {
- while (i < N) {
- t[i] += val;
- i = ((i + 1) | i);
- }
- }
- ll get(int i) {
- ll ans = 0;
- while (i >= 0) {
- ans += t[i];
- i = ((i + 1) & i) - 1;
- }
- return ans;
- }
- ll get(int l, int r) {
- return get(r) - get(l - 1);
- }
- int main() {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- int n, m;
- ll x;
- cin >> n >> m >> x;
- for (int i = 0; i < n - 1; i++) {
- int f, t;
- cin >> f >> t;
- f--, t--;
- g[f].push_back(t);
- g[t].push_back(f);
- }
- Dfs(0, 0);
- for (int i = 0; i < n; i++) {
- cin >> a[i];
- ind[path[i]] = i;
- }
- auto dig = [] (int a) {
- int ans = 0;
- while (a) {
- int d = a % 10;
- ans += d * d;
- a /= 10;
- }
- return ans;
- };
- {
- int cur = 1;
- for (int i = 1; i < 1e8 && cur < N; i++) {
- int v = dig(i);
- if (v <= x) {
- f[cur++] = i;
- }
- }
- }
- /*
- test
- 3 3 5
- 1 2
- 1 3
- 1 2 3
- 2 1
- 1 2 5
- 2 1
- */
- for (int i = 0; i < n; i++) {
- a[i] = f[a[i]];
- add(ind[i], a[i]);
- }
- while (m--) {
- int t;
- cin >> t;
- if (t == 1) {
- int index;
- int y;
- cin >> index >> y;
- index--;
- ll nxt = f[y];
- ll pre = a[index];
- ll dlt = nxt - pre;
- add(index, dlt);
- a[index] = nxt;
- } else {
- int v;
- cin >> v;
- v--;
- cout << get(ind[from[v]], ind[to[v]]) % mod << '\n';
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement