#include <bits/stdc++.h>
#include "circuit.h"
using namespace std;
const int BASE = 1e9 + 2022;
const int N = 100100;
struct SegmentTree
{
int low, mid, high;
long long value, sum;
int flip;
SegmentTree *l, *r;
SegmentTree(int low, int high, vector<long long> &coef, vector<int> &state): low(low), high(high)
{
mid = (low + high) / 2;
value = sum = flip = 0;
if (low == high)
{
l = r = NULL;
sum = coef[low];
value = state[low] * sum;
}
else
{
l = new SegmentTree(low, mid, coef, state);
r = new SegmentTree(mid + 1, high, coef, state);
sum = l->sum + r->sum;
value = r->value + r->value;
}
}
void update(int x, int y)
{
if (low == x && high == y)
{
value = sum - value;
flip ^= 1;
return;
}
if (x <= mid)
l->update(x, min(y, mid));
if (mid < y)
r->update(max(x, mid + 1), y);
value = l->value + r->value;
if (flip)
value = sum - value;
}
};
int n, m;
vector<int> a[N];
vector<long long> coef, ways;
SegmentTree *tree;
void dfsWay(int x)
{
if (empty(a[x]))
return;
for (int y : a[x])
{
dfsWay(y);
ways[x] *= ways[y];
ways[x] %= BASE;
}
ways[x] *= size(a[x]);
ways[x] %= BASE;
}
void dfsCoef(int x)
{
vector<long long> prefix(size(a[x]));
for (int i = 0; i < size(a[x]); i++)
{
prefix[i] = ways[a[x][i]];
if (i)
prefix[i] = prefix[i] * prefix[i - 1] % BASE;
}
long long suffix = 1;
for (int i = int(size(a[x])) - 1; i >= 0; i--)
{
int y = a[x][i];
coef[y] = suffix;
if (i)
coef[y] = coef[y] * prefix[i - 1] % BASE;
suffix *= ways[y];
suffix %= BASE;
}
for (int y : a[x])
{
coef[y] = coef[y] * coef[x] % BASE;
dfsCoef(y);
}
}
void init(int N, int M, vector<int> P, vector<int> A)
{
n = N;
m = M;
for (int i = 1; i < n + m; i++)
a[P[i]].push_back(i);
ways.assign(n + m, 1);
dfsWay(0);
coef.assign(n + m, 1);
dfsCoef(0);
for (int i = 0; i < m; i++)
coef[i] = coef[i + n];
coef.resize(m);
tree = new SegmentTree(0, m - 1, coef, A);
}
int count_ways(int L, int R)
{
tree->update(L - n, R - n);
return tree->value;
}
Compilation message
circuit.cpp: In function 'void dfsCoef(int)':
circuit.cpp:73:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
73 | for (int i = 0; i < size(a[x]); i++)
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2640 KB |
Output is correct |
2 |
Correct |
1 ms |
2640 KB |
Output is correct |
3 |
Incorrect |
2 ms |
2768 KB |
1st lines differ - on the 1st token, expected: '509', found: '357' |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2640 KB |
Output is correct |
2 |
Incorrect |
2 ms |
2640 KB |
1st lines differ - on the 1st token, expected: '52130940', found: '812966982' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2640 KB |
Output is correct |
2 |
Correct |
1 ms |
2640 KB |
Output is correct |
3 |
Incorrect |
2 ms |
2768 KB |
1st lines differ - on the 1st token, expected: '509', found: '357' |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
634 ms |
9596 KB |
1st lines differ - on the 1st token, expected: '431985922', found: '1057314848' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
634 ms |
9596 KB |
1st lines differ - on the 1st token, expected: '431985922', found: '1057314848' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2640 KB |
Output is correct |
2 |
Incorrect |
2 ms |
2640 KB |
1st lines differ - on the 1st token, expected: '52130940', found: '812966982' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2640 KB |
Output is correct |
2 |
Correct |
1 ms |
2640 KB |
Output is correct |
3 |
Incorrect |
2 ms |
2768 KB |
1st lines differ - on the 1st token, expected: '509', found: '357' |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2640 KB |
Output is correct |
2 |
Correct |
1 ms |
2640 KB |
Output is correct |
3 |
Incorrect |
2 ms |
2768 KB |
1st lines differ - on the 1st token, expected: '509', found: '357' |
4 |
Halted |
0 ms |
0 KB |
- |