6 const int N
= 100000 + 10, LOG
= 20;
9 std::vector
<int> adj
[N
];
11 int fa
[LOG
][N
], dep
[N
], left
[N
], right
[N
];
15 for (std::vector
<int>::iterator it
= adj
[a
].begin(); it
!= adj
[a
].end(); ++it
) {
23 right
[b
] = dfn
.size();
34 return this == p
->ch
[1];
37 inline void rotate() {
40 u
->p
->setc(this, u
->dir());
51 inline void setc(node_t
*c
, int d
) {
56 inline void update() {
58 if (ch
[0]) mx
= std::max(mx
, ch
[0]->mx
);
59 if (ch
[1]) mx
= std::max(mx
, ch
[1]->mx
);
62 inline void splay(node_t
*target
= NULL
) {
64 if (p
->p
!= target
) (dir() == p
->dir() ? p
: this)->rotate();
72 for (int i
= 0; i
<= n
; ++i
) {
73 pool
[left
[i
]].setc(pool
+ right
[i
], 1);
74 pool
[left
[i
]].update();
80 pool
[left
[v
]].splay();
81 pool
[right
[v
]].splay(pool
+ left
[v
]);
82 pool
[left
[u
]].splay();
83 pool
[right
[u
]].splay(pool
+ left
[u
]);
84 pool
[left
[v
]].setc(pool
+ left
[u
], 1);
85 pool
[right
[u
]].setc(pool
+ right
[v
], 1);
86 pool
[right
[v
]].splay();
90 pool
[left
[u
]].splay();
91 pool
[right
[u
]].splay(pool
+ left
[u
]);
92 node_t
*x
= pool
[left
[u
]].ch
[0], *y
= pool
[right
[u
]].ch
[1];
93 pool
[left
[u
]].ch
[0] = x
->p
= NULL
;
94 pool
[right
[u
]].ch
[1] = y
->p
= NULL
;
95 pool
[right
[u
]].update();
96 pool
[left
[u
]].update();
98 while (x
->ch
[1]) x
= x
->ch
[1];
104 void modify(int u
, int w
) {
105 pool
[left
[u
]].val
= pool
[right
[u
]].val
= w
;
106 pool
[left
[u
]].splay();
107 pool
[right
[u
]].splay();
111 node_t
*temp
= pool
+ left
[u
];
112 for (temp
->splay(); temp
->ch
[0];) temp
= temp
->ch
[0];
113 int x
= dfn
[temp
- pool
], y
= u
;
114 for (int i
= LOG
- 1; i
>= 0; --i
)
115 if (~fa
[i
][y
] && dep
[fa
[i
][y
]] > dep
[x
])
117 pool
[left
[y
]].splay();
118 pool
[right
[y
]].splay(pool
+ left
[y
]);
119 node_t
*z
= pool
[right
[y
]].ch
[0];
120 return std::max(z
? z
->mx
: INT_MIN
, pool
[left
[y
]].val
);
127 for (int ecnt
= n
- 1, u
, v
; ecnt
--;) {
128 scanf("%d%d", &u
, &v
);
136 right
[0] = dfn
.size();
140 for (int i
= 1; i
<= n
; ++i
) {
141 scanf("%d", col
+ i
);
142 tree
[col
[i
]].link(i
);
144 for (int i
= 1, w
; i
<= n
; ++i
) {
146 tree
[0].modify(i
, w
);
147 tree
[1].modify(i
, w
);
150 for (int j
= 1; j
< LOG
; ++j
)
151 for (int i
= 1; i
<= n
; ++i
)
152 fa
[j
][i
] = fa
[j
- 1][fa
[j
- 1][i
]];
154 for (scanf("%d", &tcase
); tcase
--;) {
156 scanf("%d%d", &op
, &u
);
159 printf("%d\n", tree
[col
[u
]].query(u
));
163 tree
[col
[u
] ^= 1].link(u
);
167 tree
[0].modify(u
, w
);
168 tree
[1].modify(u
, w
);