Untitled
unknown
plain_text
a year ago
1.9 kB
11
Indexable
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 2e5 + 5;
vector<int> adj[N];
template<class T>
class BIT {
private:
int size;
vector<T> bit;
vector<T> arr;
public:
BIT(int size) : size(size), bit(size + 1), arr(size) {}
void set(int ind, int val) { add(ind, val); }
void add(int ind, int val) {
ind++;
for (; ind <= size; ind += ind & -ind) { bit[ind] += val; }
}
T pref_sum(int ind) {
ind++;
T total = 0;
for (; ind > 0; ind -= ind & -ind) { total += bit[ind]; }
return total;
}
};
int timer{0};
ll tin[N] , tout[N];
void dfs(int u , int p){
tin[u] = timer++;
for(auto v : adj[u])
if(v != p)
dfs(v , u);
tout[u] = timer;
}
void solve() {
int n , q;
cin >> n >> q;
vector<int>a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
for (int i = 0; i < n - 1; ++i) {
int u , v;
cin >> u >> v;
--u , --v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(0 , 0);
BIT<ll>bit(n+1);
for (int i = 0; i < n; ++i) {
bit.set(tin[i] , a[i]);
bit.set(tout[i] , -a[i]);
}
for (int i = 0; i < q; ++i) {
int op;
cin >> op;
if(op == 1){
int idx , val;
cin >> idx >> val;
idx--;
bit.set(tin[idx] , val- a[idx]);
bit.set(tout[idx] , - (val- a[idx]));
a[idx] = val;
}else{
int idx;
cin >> idx;
cout << bit.pref_sum(tin[--idx]) << "\n";
}
}
}
int main() {
ios::sync_with_stdio(false),
cin.tie(nullptr),
cout.tie(nullptr);
int t{1};
// cin >> t;
while (t--) {
solve();
}
return 0;
}
Editor is loading...
Leave a Comment