999ms

Untitled

Jul 27th, 2020
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define all(x) begin(x),end(x)
  3.  
  4. using namespace std;
  5. using ll = long long;
  6.  
  7. const int N = 500500;
  8. const ll mod = 998244353;
  9.  
  10. vector<int> g[N];
  11. ll a[N];
  12. ll f[N];
  13.  
  14. vector<int> path;
  15. int ind[N];
  16. int from[N];
  17. int to[N];
  18.  
  19. void Dfs(int f, int p) {
  20. from[f] = path.size();
  21. path.push_back(f);
  22. for (int v : g[f]) {
  23. if (v == p) continue;
  24. Dfs(v, f);
  25. }
  26. to[f] = path.size() - 1;
  27. }
  28.  
  29. ll t[N];
  30.  
  31. void add(int i, int val) {
  32. while (i < N) {
  33. t[i] += val;
  34. i = ((i + 1) | i);
  35. }
  36. }
  37.  
  38. ll get(int i) {
  39. ll ans = 0;
  40. while (i >= 0) {
  41. ans += t[i];
  42. i = ((i + 1) & i) - 1;
  43. }
  44. return ans;
  45. }
  46.  
  47.  
  48. ll get(int l, int r) {
  49. return get(r) - get(l - 1);
  50. }
  51.  
  52.  
  53. int main() {
  54. ios_base::sync_with_stdio(false);
  55. cin.tie(nullptr);
  56. cout.tie(nullptr);
  57. int n, m;
  58. ll x;
  59. cin >> n >> m >> x;
  60. for (int i = 0; i < n - 1; i++) {
  61. int f, t;
  62. cin >> f >> t;
  63. f--, t--;
  64. g[f].push_back(t);
  65. g[t].push_back(f);
  66. }
  67. Dfs(0, 0);
  68. for (int i = 0; i < n; i++) {
  69. cin >> a[i];
  70. ind[path[i]] = i;
  71. }
  72.  
  73. auto dig = [] (int a) {
  74. int ans = 0;
  75. while (a) {
  76. int d = a % 10;
  77. ans += d * d;
  78. a /= 10;
  79. }
  80. return ans;
  81. };
  82.  
  83. {
  84. int cur = 1;
  85. for (int i = 1; i < 1e8 && cur < N; i++) {
  86. int v = dig(i);
  87. if (v <= x) {
  88. f[cur++] = i;
  89. }
  90. }
  91. }
  92. /*
  93. test
  94. 3 3 5
  95. 1 2
  96. 1 3
  97. 1 2 3
  98. 2 1
  99. 1 2 5
  100. 2 1
  101.  
  102. */
  103.  
  104.  
  105. for (int i = 0; i < n; i++) {
  106. a[i] = f[a[i]];
  107. add(ind[i], a[i]);
  108. }
  109.  
  110.  
  111.  
  112.  
  113. while (m--) {
  114. int t;
  115. cin >> t;
  116. if (t == 1) {
  117. int index;
  118. int y;
  119. cin >> index >> y;
  120. index--;
  121. ll nxt = f[y];
  122. ll pre = a[index];
  123. ll dlt = nxt - pre;
  124. add(index, dlt);
  125. a[index] = nxt;
  126. } else {
  127. int v;
  128. cin >> v;
  129. v--;
  130. cout << get(from[v], to[v]) % mod << '\n';
  131. }
  132. }
  133.  
  134. }
  135.  
Add Comment
Please, Sign In to add comment