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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
|
#include <cstdio> #include <cctype> #include <vector>
typedef std::pair<int, int> pii;
#define fir first #define sec second #define debug(...) fprintf(stderr, __VA_ARGS__) #define Debug(s) debug("The message in line %d, Function %s: %s\n", __LINE__, __FUNCTION__, s) #define getchar() (ipos == iend and (iend = (ipos = _ibuf) + fread(_ibuf, 1, __bufsize, stdin), ipos == iend) ? EOF : *ipos++) #define putchar(ch) (opos == oend ? fwrite(_obuf, 1, __bufsize, stdout), opos = _obuf : 0, *opos++ = (ch)) #define __bufsize (1 << 21 | 1)
char _ibuf[__bufsize], _obuf[__bufsize], _stk[50]; char *ipos = _ibuf, *iend = _ibuf, *opos = _obuf, *oend = _obuf + __bufsize, *stkpos = _stk;
struct END { ~END() { fwrite(_obuf, 1, opos - _obuf, stdout); } } __;
inline int read() { register int x = 0; register char ch; while (!isdigit(ch = getchar())); while (x = (x << 3) + (x << 1) + (ch & 15), isdigit(ch = getchar())); return x; }
template <typename _INT> inline void write(_INT x) { while (*++stkpos = x % 10 ^ 48, x /= 10, x); while (stkpos != _stk) putchar(*stkpos--); }
inline void Puts(const char* str) { while (*str) putchar(*str++); putchar('\n'); }
template <typename _Tp> inline bool Chkmax(_Tp& x, const _Tp& y) { return x < y ? x = y, true : false; }
template <typename _Tp> inline bool Chkmin(_Tp& x, const _Tp& y) { return x > y ? x = y, true : false; }
const int maxN = 2e5 + 2;
int n, m, dfst; int fa[maxN], top[maxN], son[maxN], dfn[maxN], size[maxN]; std::vector<int> G[maxN];
inline pii Merge(pii l, pii r) { return pii(l.fir + r.fir, std::max(r.sec, l.sec + r.fir)); }
namespace SEG { typedef struct Node* ptr;
ptr root;
struct Node { int l, r; pii info; bool tag; ptr lson, rson;
Node() { }
Node(int _l, int _r) : l(_l), r(_r) { this->tag = false; }
void Cover() { this->tag = true, this->info = pii(this->l - this->r - 1, -1); }
void Pushup() { this->info = Merge(this->lson->info, this->rson->info); }
void Pushdown() { if (this->tag) this->lson->Cover(), this->rson->Cover(), this->tag = false; } };
inline ptr New(int l, int r) { static ptr mempool = new Node[n << 2]; return new(mempool++) Node(l, r); }
void Build(int l, int r, ptr& cur = root) { cur = New(l, r); if (l == r) { cur->info = pii(-1, -1); return; } int mid = (l + r) >> 1; Build(l, mid, cur->lson), Build(mid + 1, r, cur->rson); cur->Pushup(); }
void Add(int pos, int addval, ptr cur = root) { if (cur->l == cur->r) { cur->info = pii(cur->info.fir + addval, cur->info.sec + addval); return; } cur->Pushdown(); int mid = (cur->l + cur->r) >> 1; if (pos <= mid) Add(pos, addval, cur->lson); else Add(pos, addval, cur->rson); cur->Pushup(); }
pii Query(int ql, int qr, ptr cur = root) { if (ql <= cur->l and cur->r <= qr) return cur->info; cur->Pushdown(); pii res(0, -1e9); int mid = (cur->l + cur->r) >> 1; if (mid < qr) res = Merge(Query(ql, qr, cur->rson), res); if (ql <= mid) res = Merge(Query(ql, qr, cur->lson), res); return res; }
void Cover(int ql, int qr, ptr cur = root) { if (ql <= cur->l and cur->r <= qr) { cur->Cover(); return; } if (ql > cur->r or qr < cur->l) return; cur->Pushdown(); Cover(ql, qr, cur->lson), Cover(ql, qr, cur->rson); cur->Pushup(); } }
void DFS1(int u) { size[u] = 1; for (int v : G[u]) if (v != fa[u]) { DFS1(v); size[u] += size[v]; if (size[v] > size[son[u]]) son[u] = v; } }
void DFS2(int u) { dfn[u] = ++dfst; if (son[u]) top[son[u]] = top[u], DFS2(son[u]); for (int v : G[u]) if (!top[v]) top[v] = v, DFS2(v); }
inline int Query(int x) { pii res(0, -1e9); while (top[x] != 1) { res = Merge(SEG::Query(dfn[top[x]], dfn[x]), res); x = fa[top[x]]; } res = Merge(SEG::Query(1, dfn[x]), res); return res.sec; }
int main() { #ifndef ONLINE_JUDGE freopen("bigbro.in", "r", stdin); freopen("bigbro.out", "w", stdout); #endif n = read(), m = read(); for (int i = 2; i <= n; ++i) G[fa[i] = read()].push_back(i); DFS1(1); top[1] = 1, DFS2(1); SEG::Build(1, n); while (m--) { int opt = read(), x = read(), val; switch (opt) { case 1 : SEG::Add(dfn[x], 1); break; case 2 : SEG::Cover(dfn[x], dfn[x] + size[x] - 1); SEG::Add(dfn[x], -Query(x) - 1); break; case 3 : Puts(Query(x) >= 0 ? "Yes" : "No"); break; } } return 0; }
|