add some
This commit is contained in:
95
storage/aloha/cpp/2026-cherry-div1-D.cpp
Normal file
95
storage/aloha/cpp/2026-cherry-div1-D.cpp
Normal file
@@ -0,0 +1,95 @@
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
#define let auto
|
||||
#define fn auto
|
||||
#define usize size_t
|
||||
using namespace std;
|
||||
|
||||
struct Node {
|
||||
usize acc;
|
||||
usize self;
|
||||
vector<Node *> *adjs;
|
||||
|
||||
void add_route(Node *node) {
|
||||
adjs->push_back(node);
|
||||
}
|
||||
|
||||
void update_delta(usize delta) {
|
||||
acc += delta;
|
||||
self += delta;
|
||||
for (let x: *adjs) {
|
||||
x->update_by_direct(delta);
|
||||
}
|
||||
}
|
||||
|
||||
void update_by_direct(usize delta) {
|
||||
acc += delta;
|
||||
}
|
||||
};
|
||||
|
||||
fn fastio() {
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
}
|
||||
|
||||
fn main() -> int {
|
||||
fastio();
|
||||
usize n, q;
|
||||
cin >> n >> q;
|
||||
let arr = (usize *) calloc(n, sizeof(usize));
|
||||
|
||||
let graph = (Node **) calloc(n, sizeof(Node *));
|
||||
|
||||
for (usize i = 0; i < n; i++) {
|
||||
graph[i] = (Node *) malloc(sizeof(Node));
|
||||
graph[i]->acc = 0;
|
||||
graph[i]->self = 0;
|
||||
graph[i]->adjs = new vector<Node *>();
|
||||
}
|
||||
|
||||
for (usize i = 0; i < n; i++) {
|
||||
usize a;
|
||||
cin >> a;
|
||||
arr[i] = a;
|
||||
}
|
||||
|
||||
for (usize i = 0; i < n - 1; i++) {
|
||||
usize u, v;
|
||||
cin >> u >> v;
|
||||
u = u - 1;
|
||||
v = v - 1;
|
||||
graph[u]->add_route(graph[v]);
|
||||
graph[v]->add_route(graph[u]);
|
||||
}
|
||||
|
||||
for (usize i = 0; i < n; i++) {
|
||||
graph[i]->update_delta(arr[i]);
|
||||
}
|
||||
|
||||
for (usize i = 0; i < q; i++) {
|
||||
usize inst;
|
||||
cin >> inst;
|
||||
|
||||
if (inst == 1) {
|
||||
usize target, c;
|
||||
cin >> target >> c;
|
||||
target -= 1;
|
||||
graph[target]->update_delta(c);
|
||||
} else {
|
||||
usize target;
|
||||
cin >> target;
|
||||
target -= 1;
|
||||
cout << graph[target]->acc << '\n';
|
||||
}
|
||||
}
|
||||
end:
|
||||
|
||||
free(arr);
|
||||
for (usize i = 0; i < n; i++) {
|
||||
delete graph[i]->adjs;
|
||||
free(graph[i]);
|
||||
}
|
||||
free(graph);
|
||||
}
|
||||
Reference in New Issue
Block a user