#include #include #include #include #include #include #include #include #include using namespace std; using ll = long long; using ull = unsigned long long; using uint = unsigned int; const int MN = 201024; using B = array; const int BS = MN/64; void orassign(B &l, B r) { for (int i = 0; i < BS; i++) { l[i] |= r[i]; } } void xorassign(B &l, B r) { for (int i = 0; i < BS; i++) { l[i] ^= r[i]; } } void shiftLAndOr(const B &x, B &ans, int k) { // << int shift = k%64; if (shift) { ans[k/64] |= x[0]<>(64-shift); ans[i+k/64] |= x[i]< g[MD]; int n; scanf("%d", &n); for (int i = 0; i < n; i++) { int a; scanf("%d", &a); g[a].push_back(i); } B mp; mp.fill(0); for (int i = 0; i < n; i++) { int x; scanf("%d", &x); if (x) { mp[i/64] |= 1ULL<<(i%64); } } int ans[MN] = {}; // B ans; ans.fill(0); constexpr int U = 4; constexpr int BU = 15; constexpr ull bmap = 0x1111111111111111; for (int fe = 0; fe < MD/BU; fe++) { B res[BU]; for (int i = 0; i < BU; i++) { res[i].fill(0); for (int d: g[fe*BU+i]) { shiftLAndOr(mp, res[i], d); } } for (int i = 0; i < BS; i++) { for (int b = 0; b < U; b++) { ull sm = 0; for (int j = 0; j < BU; j++) { sm += ((res[j][i]>>b) & bmap); } for (int j = 0; j < 64/U; j++) { ans[i*64+j*U+b] += (sm>>(j*U))&((1<