-
Notifications
You must be signed in to change notification settings - Fork 0
/
**Lazy Proagation - Graphs,trees,Policy Based Data Structure**
71 lines (67 loc) · 1.35 KB
/
**Lazy Proagation - Graphs,trees,Policy Based Data Structure**
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
HackerEarth : Mother Flippin' Tree
Author : Dnynaneshwar Ware
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
#define int long long
const int N = 1e5 + 5;
using namespace __gnu_pbds;
template <typename T>
using ordered_set = tree
<T, null_type, less<T>, rb_tree_tag,
tree_order_statistics_node_update>;
vector<int> v[N];
vector<int> upd[N];
vector<int> ask[N];
int val[N];
ordered_set<int> st;
set<pair<int,int>> ans;
void dfs(int u,int par)
{
for(auto i:upd[u])st.insert(i);
for(auto time:ask[u])
{
int res = val[u];
if(st.order_of_key(time) & 1)
res = 1 - res;
ans.emplace(time,res);
}
for(auto i:v[u])
{
if(i!=par)
{
dfs(i,u);
}
}
for(auto i:upd[u])st.erase(i);
}
signed main()
{
int n;
cin >> n;
for(int i=1;i<n;i++)
{
int x,y;
cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
for(int i=1;i<=n;i++)cin >> val[i];
int clk=1;
int q;
cin >> q;
while(q--)
{
string type;
int x;
cin >> type >> x;
if(type == "flip")
upd[x].push_back(clk);
else
ask[x].push_back(clk);
clk++;
}
dfs(1,1);
for(auto i:ans) cout << i.second << ' ';
}