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
|
#include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #include <vector> #include <queue>
#define FILE_IN(__fname) freopen(__fname, "r", stdin) #define FILE_OUT(__fname) freopen(__fname, "w", stdout) #define rap(a,s,t,i) for (int a = s; a <= t; a += i) #define basketball(a,t,s,i) for (int a = t; a > s; a -= i) #define countdown(s) while (s --> 0) #define IMPROVE_IO() std::ios::sync_with_stdio(false)
using std::cin; using std::cout; using std::endl;
typedef long long int lli;
lli getll() { long long int x; scanf("%lld", &x); return x; }
const int MAXN = 1000000 + 10;
namespace fastIO{ #define BUF_SIZE 100000 #define OUT_SIZE 100000 #define ll long long bool IOerror=0; inline char nc(){ static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE; if (p1==pend){ p1=buf; pend=buf+fread(buf,1,BUF_SIZE,stdin); if (pend==p1){IOerror=1;return -1;} } return *p1++; } inline bool blank(char ch){return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';} inline int getint(){ bool sign=0; char ch=nc(); int x=0; for (;blank(ch);ch=nc()); if (IOerror)return 0; if (ch=='-')static_cast<void>(sign=1),ch=nc(); for (;ch>='0'&&ch<='9';ch=nc())x=x*10+ch-'0'; if (sign)x=-x; return x; } inline void read(ll &x){ bool sign=0; char ch=nc(); x=0; for (;blank(ch);ch=nc()); if (IOerror)return; if (ch=='-')static_cast<void>(sign=1),ch=nc(); for (;ch>='0'&&ch<='9';ch=nc())x=x*10+ch-'0'; if (sign)x=-x; } #undef ll #undef OUT_SIZE #undef BUF_SIZE }; using namespace fastIO;
std::vector<int> head[MAXN];
int n, m;
namespace FastSolution { int father[MAXN], bfn[MAXN], first[MAXN], timestamp; int siz[MAXN]; void BFS(int root = 1) { std::queue<int> q; while (!q.empty()) q.pop(); q.push(root); while (!q.empty()) { int u = q.front(); q.pop(); bfn[u] = ++timestamp; for (int i = 0, ssiz = (int) head[u].size(); i < ssiz; ++i) { int v = head[u][i]; if (bfn[v]) continue; if (!first[u]) first[u] = v; q.push(v); ++siz[u]; } } } } using namespace FastSolution;
namespace SegmentTree { const int RAW_MAXN = 1000000 + 10; const int SGMAXN = RAW_MAXN << 2; #define lson ((root << 1)) #define rson ((root << 1 | 1)) int sum[SGMAXN], rev[SGMAXN]; void PushDownSelf(int root, int l, int r) { sum[root] = (r - l + 1) - sum[root]; rev[root] ^= 1; } void PushDown(int root, int l, int r) { if (rev[root] == 0) return; int mid = (l + r) >> 1; PushDownSelf(lson, l, mid); PushDownSelf(rson, mid + 1, r); rev[root] = 0; } void Modify(int root, int l, int r, int ll, int rr) { if (ll <= l && r <= rr) { sum[root] = (r - l + 1) - sum[root]; rev[root] ^= 1; return; } PushDown(root, l, r); int mid = (l + r) >> 1; if (ll <= mid) Modify(lson, l, mid, ll, rr); if (mid + 1 <= rr) Modify(rson, mid + 1, r, ll, rr); sum[root] = sum[lson] + sum[rson]; } int Query(int root, int l, int r, int ll, int rr) { if (ll <= l && r <= rr) return sum[root]; PushDown(root, l, r); int mid = (l + r) >> 1; int ans = 0; if (ll <= mid) ans += Query(lson, l, mid, ll, rr); if (mid + 1 <= rr) ans += Query(rson, mid + 1, r, ll, rr); return ans; } }
int main() { n = getint(); for (int i = 2; i <= n; ++i) { int x = getint(); father[i] = x; head[x].push_back(i); head[i].push_back(x); } m = getint(); BFS(); for (int i = 1; i <= m; ++i) { int cmd = getint(); int x = getint(); if (cmd == 1) { SegmentTree::Modify(1, 1, n, bfn[x], bfn[x]); int ff = first[x]; if (father[x]) SegmentTree::Modify(1, 1, n, bfn[father[x]], bfn[father[x]]); if (ff) SegmentTree::Modify(1, 1, n, bfn[ff], bfn[ff] + (int) siz[x] - 1); } else { int ff = first[x], ans = SegmentTree::Query(1, 1, n, bfn[x], bfn[x]); if (ff) ans += SegmentTree::Query(1, 1, n, bfn[ff], bfn[ff] + (int) siz[x] - 1); if (father[x]) ans += SegmentTree::Query(1, 1, n, bfn[father[x]], bfn[father[x]]); printf("%d\n", ans); } } return 0; }
|