Loading...
Submission
Id When Author Problem Language CPU Memory Verdict
7523 2023-03-08 03:01:11 heisenberg_120 Bonus Project C++ 17 315 ms 57644 kb Wrong Answer - 1
Test Cases
CPU Memory Verdict
1 315 ms 57644 kb Wrong Answer
2 - - Skipped
3 - - Skipped
4 - - Skipped
5 - - Skipped
Source Code
program.cpp
Download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4.  
  5. const int N = 5e5 + 9;
  6. int A[N];
  7. vector<int> g[N], st(N), en(N); int timer, n, q;
  8. struct segment{
  9. #define lc (node << 1)
  10. #define rc (node << 1 | 1)
  11. int seg[4 * N]{}, lazy[4 * N]{};
  12. void init(){
  13. memset(seg, 0, sizeof seg);
  14. memset(lazy, 0, sizeof lazy);
  15. }
  16. void push(int node, int b, int e){
  17. if(lazy[node] == 0)return;
  18. seg[node] += lazy[node] * (e - b + 1);
  19. if(b != e){
  20. lazy[lc] += lazy[node];
  21. lazy[rc] += lazy[node];
  22. }
  23. lazy[node] = 0;
  24. }
  25. void pull(int node){
  26. seg[node] = seg[lc] + seg[rc];
  27. }
  28. static int combine(int a, int b){
  29. return a + b;
  30. }
  31. void build(int node, int b, int e){
  32. lazy[node] = 0;
  33. if(b == e){
  34. if(b <= n) seg[node] += A[b];
  35. return;
  36. }
  37. int m = (b + e) >> 1;
  38. build(lc, b, m);
  39. build(rc, m + 1, e);
  40. pull(node);
  41. }
  42. void upd(int node, int b, int e, int l, int r, int v){
  43. push(node, b, e);
  44. if(l > e or r < b) return;
  45. if(b >= l and e <= r){
  46. lazy[node] = v;
  47. push(node, b, e);
  48. return;
  49. }
  50. int m = (b + e) >> 1;
  51. upd(lc, b, m, l, r, v);
  52. upd(rc, m + 1, e, l, r, v);
  53. pull(node);
  54. }
  55.  
  56. int query(int node, int b, int e, int l, int r){
  57. push(node, b, e);
  58. if(l > e or r < b)return 0;
  59. if(b >= l and e <= r){
  60. return seg[node];
  61. }
  62. int m = (b + e) >> 1;
  63. return combine(query(lc, b, m, l, r), query(rc, m + 1, e, l, r));
  64. }
  65. }se;
  66.  
  67. void DFS(int cur, int par){
  68. st[cur] = ++timer;
  69. for(auto x : g[cur]){
  70. if(x != par){
  71. DFS(x, cur);
  72. }
  73. }
  74. en[cur] = timer;
  75. }
  76.  
  77. signed main(){
  78. ios_base::sync_with_stdio(false);
  79. cin.tie(nullptr);
  80. int tt;
  81. cin >> tt;
  82. while (tt--) {
  83. timer = 0; se.init();
  84. cin >> n >> q;
  85. for(int i = 0; i < N; i++){
  86. g[i].clear();
  87. A[i] = 0, st[i] = 0, en[i] = 0;
  88. }
  89. for (int i = 1; i <= n; i++) {
  90. cin >> A[i];
  91. }
  92. se.build(1, 1, n);
  93. for (int i = 0; i < n - 1; i++) {
  94. int u, v;
  95. cin >> u >> v;
  96. g[u].push_back(v);
  97. g[v].push_back(u);
  98. }
  99. DFS(1, -1);
  100. while (q--) {
  101. int x, v;
  102. cin >> x >> v;
  103. se.upd(1, 1, n, st[x], st[x], 2 * v);
  104. if(st[x] != en[x]) se.upd(1, 1, n, st[x] + 1, en[x], v);
  105. }
  106. for (int i = 1; i <= n; i++) {
  107. cout << se.query(1, 1, n, st[i], st[i]) << ' ';
  108. }
  109. cout << '\n';
  110. }
  111. return 0;
  112. }