답안 #836196

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
836196 2023-08-24T08:38:52 Z SamAnd 디지털 회로 (IOI22_circuit) C++17
100 / 100
896 ms 36532 KB
#include "circuit.h"
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define m_p make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
typedef long long ll;
const int N = 200005, M = 1000002022;

int n, m;
vector<int> g[N];
int s[N];

int a[N];
void dfs0(int x)
{
    a[x] = 1;
    if (g[x].empty())
        return;
    for (int i = 0; i < g[x].size(); ++i)
    {
        int h = g[x][i];
        dfs0(h);
        a[x] = (a[x] * 1LL * a[h]) % M;
    }
    a[x] = (a[x] * 1LL * sz(g[x])) % M;
}

int u[N];
void dfs1(int x, int aa)
{
    if (g[x].empty())
    {
        u[x - n] = aa;
        return;
    }

    vector<int> p;
    for (int i = 0; i < g[x].size(); ++i)
    {
        int h = g[x][i];
        if (p.empty())
            p.push_back(a[h]);
        else
            p.push_back((p.back() * 1LL * a[h]) % M);
    }
    vector<int> s;
    for (int i = sz(g[x]) - 1; i >= 0; --i)
    {
        int h = g[x][i];
        if (s.empty())
            s.push_back(a[h]);
        else
            s.push_back((s.back() * 1LL * a[h]) % M);
    }
    reverse(all(s));

    for (int i = 0; i < g[x].size(); ++i)
    {
        int h = g[x][i];
        int yaa = aa;
        if (i)
            yaa = (yaa * 1LL * p[i - 1]) % M;
        if (i + 1 < sz(g[x]))
            yaa = (yaa * 1LL * s[i + 1]) % M;
        dfs1(h, yaa);
    }
}

int t[N * 4], su[N * 4];
bool laz[N * 4];
void bil(int tl, int tr, int pos)
{
    if (tl == tr)
    {
        t[pos] = u[tl] * s[tl];
        su[pos] = u[tl];
        return;
    }
    int m = (tl + tr) / 2;
    bil(tl, m, pos * 2);
    bil(m + 1, tr, pos * 2 + 1);
    t[pos] = (t[pos * 2] + t[pos * 2 + 1]) % M;
    su[pos] = (su[pos * 2] + su[pos * 2 + 1]) % M;
}

void ave(int pos)
{
    laz[pos] ^= true;
    t[pos] = (su[pos] - t[pos] + M) % M;
}

void shi(int pos)
{
    if (!laz[pos])
        return;
    ave(pos * 2);
    ave(pos * 2 + 1);
    laz[pos] = false;
}

void ubd(int tl, int tr, int l, int r, int pos)
{
    if (l > r)
        return;
    if (tl == l && tr == r)
    {
        ave(pos);
        return;
    }
    shi(pos);
    int m = (tl + tr) / 2;
    ubd(tl, m, l, min(m, r), pos * 2);
    ubd(m + 1, tr, max(m + 1, l), r, pos * 2 + 1);
    t[pos] = (t[pos * 2] + t[pos * 2 + 1]) % M;
}

void init(int NN, int MM, std::vector<int> P, std::vector<int> A)
{
    n = NN;
    m = MM;
    for (int i = 1; i < n + m; ++i)
        g[P[i]].push_back(i);
    for (int i = 0; i < m; ++i)
        s[i] = A[i];
    dfs0(0);
    dfs1(0, 1);
    bil(0, m - 1, 1);
}

int count_ways(int L, int R)
{
    L -= n;
    R -= n;
    ubd(0, m - 1, L, R, 1);
    return t[1];
}

/*
3 4 3
-1 0 1 2 1 1 0
1 0 1 0
3 4
4 5
3 6
*/

Compilation message

circuit.cpp: In function 'void dfs0(int)':
circuit.cpp:22:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   22 |     for (int i = 0; i < g[x].size(); ++i)
      |                     ~~^~~~~~~~~~~~~
circuit.cpp: In function 'void dfs1(int, int)':
circuit.cpp:41:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   41 |     for (int i = 0; i < g[x].size(); ++i)
      |                     ~~^~~~~~~~~~~~~
circuit.cpp:60:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   60 |     for (int i = 0; i < g[x].size(); ++i)
      |                     ~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4944 KB Output is correct
2 Correct 2 ms 4944 KB Output is correct
3 Correct 2 ms 5072 KB Output is correct
4 Correct 2 ms 5072 KB Output is correct
5 Correct 2 ms 5072 KB Output is correct
6 Correct 2 ms 5072 KB Output is correct
7 Correct 2 ms 5072 KB Output is correct
8 Correct 2 ms 5072 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4944 KB Output is correct
2 Correct 2 ms 4944 KB Output is correct
3 Correct 3 ms 5072 KB Output is correct
4 Correct 3 ms 5072 KB Output is correct
5 Correct 3 ms 4944 KB Output is correct
6 Correct 3 ms 5072 KB Output is correct
7 Correct 3 ms 5072 KB Output is correct
8 Correct 3 ms 5072 KB Output is correct
9 Correct 3 ms 5072 KB Output is correct
10 Correct 3 ms 5328 KB Output is correct
11 Correct 3 ms 5200 KB Output is correct
12 Correct 3 ms 5072 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4944 KB Output is correct
2 Correct 2 ms 4944 KB Output is correct
3 Correct 2 ms 5072 KB Output is correct
4 Correct 2 ms 5072 KB Output is correct
5 Correct 2 ms 5072 KB Output is correct
6 Correct 2 ms 5072 KB Output is correct
7 Correct 2 ms 5072 KB Output is correct
8 Correct 2 ms 5072 KB Output is correct
9 Correct 3 ms 4944 KB Output is correct
10 Correct 2 ms 4944 KB Output is correct
11 Correct 3 ms 5072 KB Output is correct
12 Correct 3 ms 5072 KB Output is correct
13 Correct 3 ms 4944 KB Output is correct
14 Correct 3 ms 5072 KB Output is correct
15 Correct 3 ms 5072 KB Output is correct
16 Correct 3 ms 5072 KB Output is correct
17 Correct 3 ms 5072 KB Output is correct
18 Correct 3 ms 5328 KB Output is correct
19 Correct 3 ms 5200 KB Output is correct
20 Correct 3 ms 5072 KB Output is correct
21 Correct 2 ms 5072 KB Output is correct
22 Correct 2 ms 5072 KB Output is correct
23 Correct 2 ms 5072 KB Output is correct
24 Correct 3 ms 5072 KB Output is correct
25 Correct 2 ms 5072 KB Output is correct
26 Correct 3 ms 5072 KB Output is correct
27 Correct 3 ms 5072 KB Output is correct
28 Correct 3 ms 5072 KB Output is correct
29 Correct 2 ms 5072 KB Output is correct
30 Correct 3 ms 5072 KB Output is correct
31 Correct 2 ms 5200 KB Output is correct
32 Correct 3 ms 5072 KB Output is correct
33 Correct 3 ms 5072 KB Output is correct
34 Correct 3 ms 5048 KB Output is correct
35 Correct 2 ms 5072 KB Output is correct
36 Correct 3 ms 5328 KB Output is correct
37 Correct 3 ms 5328 KB Output is correct
38 Correct 3 ms 5328 KB Output is correct
39 Correct 2 ms 5072 KB Output is correct
40 Correct 2 ms 5072 KB Output is correct
41 Correct 2 ms 5072 KB Output is correct
42 Correct 2 ms 5072 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 429 ms 7760 KB Output is correct
2 Correct 700 ms 10568 KB Output is correct
3 Correct 730 ms 10640 KB Output is correct
4 Correct 674 ms 10596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 429 ms 7760 KB Output is correct
2 Correct 700 ms 10568 KB Output is correct
3 Correct 730 ms 10640 KB Output is correct
4 Correct 674 ms 10596 KB Output is correct
5 Correct 558 ms 7760 KB Output is correct
6 Correct 838 ms 10644 KB Output is correct
7 Correct 780 ms 10592 KB Output is correct
8 Correct 791 ms 10616 KB Output is correct
9 Correct 461 ms 5200 KB Output is correct
10 Correct 642 ms 5388 KB Output is correct
11 Correct 620 ms 5300 KB Output is correct
12 Correct 626 ms 5328 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4944 KB Output is correct
2 Correct 2 ms 4944 KB Output is correct
3 Correct 3 ms 5072 KB Output is correct
4 Correct 3 ms 5072 KB Output is correct
5 Correct 3 ms 4944 KB Output is correct
6 Correct 3 ms 5072 KB Output is correct
7 Correct 3 ms 5072 KB Output is correct
8 Correct 3 ms 5072 KB Output is correct
9 Correct 3 ms 5072 KB Output is correct
10 Correct 3 ms 5328 KB Output is correct
11 Correct 3 ms 5200 KB Output is correct
12 Correct 3 ms 5072 KB Output is correct
13 Correct 429 ms 7760 KB Output is correct
14 Correct 700 ms 10568 KB Output is correct
15 Correct 730 ms 10640 KB Output is correct
16 Correct 674 ms 10596 KB Output is correct
17 Correct 558 ms 7760 KB Output is correct
18 Correct 838 ms 10644 KB Output is correct
19 Correct 780 ms 10592 KB Output is correct
20 Correct 791 ms 10616 KB Output is correct
21 Correct 461 ms 5200 KB Output is correct
22 Correct 642 ms 5388 KB Output is correct
23 Correct 620 ms 5300 KB Output is correct
24 Correct 626 ms 5328 KB Output is correct
25 Correct 714 ms 14072 KB Output is correct
26 Correct 792 ms 14072 KB Output is correct
27 Correct 792 ms 14088 KB Output is correct
28 Correct 693 ms 14076 KB Output is correct
29 Correct 805 ms 35988 KB Output is correct
30 Correct 695 ms 36000 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4944 KB Output is correct
2 Correct 2 ms 4944 KB Output is correct
3 Correct 2 ms 5072 KB Output is correct
4 Correct 2 ms 5072 KB Output is correct
5 Correct 2 ms 5072 KB Output is correct
6 Correct 2 ms 5072 KB Output is correct
7 Correct 2 ms 5072 KB Output is correct
8 Correct 2 ms 5072 KB Output is correct
9 Correct 3 ms 4944 KB Output is correct
10 Correct 2 ms 4944 KB Output is correct
11 Correct 3 ms 5072 KB Output is correct
12 Correct 3 ms 5072 KB Output is correct
13 Correct 3 ms 4944 KB Output is correct
14 Correct 3 ms 5072 KB Output is correct
15 Correct 3 ms 5072 KB Output is correct
16 Correct 3 ms 5072 KB Output is correct
17 Correct 3 ms 5072 KB Output is correct
18 Correct 3 ms 5328 KB Output is correct
19 Correct 3 ms 5200 KB Output is correct
20 Correct 3 ms 5072 KB Output is correct
21 Correct 2 ms 5072 KB Output is correct
22 Correct 2 ms 5072 KB Output is correct
23 Correct 2 ms 5072 KB Output is correct
24 Correct 3 ms 5072 KB Output is correct
25 Correct 2 ms 5072 KB Output is correct
26 Correct 3 ms 5072 KB Output is correct
27 Correct 3 ms 5072 KB Output is correct
28 Correct 3 ms 5072 KB Output is correct
29 Correct 2 ms 5072 KB Output is correct
30 Correct 3 ms 5072 KB Output is correct
31 Correct 2 ms 5200 KB Output is correct
32 Correct 3 ms 5072 KB Output is correct
33 Correct 3 ms 5072 KB Output is correct
34 Correct 3 ms 5048 KB Output is correct
35 Correct 2 ms 5072 KB Output is correct
36 Correct 3 ms 5328 KB Output is correct
37 Correct 3 ms 5328 KB Output is correct
38 Correct 3 ms 5328 KB Output is correct
39 Correct 2 ms 5072 KB Output is correct
40 Correct 2 ms 5072 KB Output is correct
41 Correct 2 ms 5072 KB Output is correct
42 Correct 2 ms 5072 KB Output is correct
43 Correct 410 ms 5200 KB Output is correct
44 Correct 736 ms 5328 KB Output is correct
45 Correct 747 ms 5200 KB Output is correct
46 Correct 723 ms 5456 KB Output is correct
47 Correct 659 ms 5456 KB Output is correct
48 Correct 638 ms 5444 KB Output is correct
49 Correct 786 ms 5456 KB Output is correct
50 Correct 769 ms 5440 KB Output is correct
51 Correct 818 ms 5380 KB Output is correct
52 Correct 714 ms 5392 KB Output is correct
53 Correct 721 ms 6260 KB Output is correct
54 Correct 710 ms 5456 KB Output is correct
55 Correct 577 ms 5408 KB Output is correct
56 Correct 588 ms 5456 KB Output is correct
57 Correct 684 ms 5328 KB Output is correct
58 Correct 667 ms 6612 KB Output is correct
59 Correct 521 ms 6608 KB Output is correct
60 Correct 845 ms 6608 KB Output is correct
61 Correct 531 ms 5584 KB Output is correct
62 Correct 660 ms 5200 KB Output is correct
63 Correct 630 ms 5200 KB Output is correct
64 Correct 861 ms 5328 KB Output is correct
65 Correct 280 ms 5200 KB Output is correct
66 Correct 633 ms 5328 KB Output is correct
67 Correct 653 ms 5328 KB Output is correct
68 Correct 643 ms 5328 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4944 KB Output is correct
2 Correct 2 ms 4944 KB Output is correct
3 Correct 2 ms 5072 KB Output is correct
4 Correct 2 ms 5072 KB Output is correct
5 Correct 2 ms 5072 KB Output is correct
6 Correct 2 ms 5072 KB Output is correct
7 Correct 2 ms 5072 KB Output is correct
8 Correct 2 ms 5072 KB Output is correct
9 Correct 3 ms 4944 KB Output is correct
10 Correct 2 ms 4944 KB Output is correct
11 Correct 3 ms 5072 KB Output is correct
12 Correct 3 ms 5072 KB Output is correct
13 Correct 3 ms 4944 KB Output is correct
14 Correct 3 ms 5072 KB Output is correct
15 Correct 3 ms 5072 KB Output is correct
16 Correct 3 ms 5072 KB Output is correct
17 Correct 3 ms 5072 KB Output is correct
18 Correct 3 ms 5328 KB Output is correct
19 Correct 3 ms 5200 KB Output is correct
20 Correct 3 ms 5072 KB Output is correct
21 Correct 2 ms 5072 KB Output is correct
22 Correct 2 ms 5072 KB Output is correct
23 Correct 2 ms 5072 KB Output is correct
24 Correct 3 ms 5072 KB Output is correct
25 Correct 2 ms 5072 KB Output is correct
26 Correct 3 ms 5072 KB Output is correct
27 Correct 3 ms 5072 KB Output is correct
28 Correct 3 ms 5072 KB Output is correct
29 Correct 2 ms 5072 KB Output is correct
30 Correct 3 ms 5072 KB Output is correct
31 Correct 2 ms 5200 KB Output is correct
32 Correct 3 ms 5072 KB Output is correct
33 Correct 3 ms 5072 KB Output is correct
34 Correct 3 ms 5048 KB Output is correct
35 Correct 2 ms 5072 KB Output is correct
36 Correct 3 ms 5328 KB Output is correct
37 Correct 3 ms 5328 KB Output is correct
38 Correct 3 ms 5328 KB Output is correct
39 Correct 2 ms 5072 KB Output is correct
40 Correct 2 ms 5072 KB Output is correct
41 Correct 2 ms 5072 KB Output is correct
42 Correct 2 ms 5072 KB Output is correct
43 Correct 429 ms 7760 KB Output is correct
44 Correct 700 ms 10568 KB Output is correct
45 Correct 730 ms 10640 KB Output is correct
46 Correct 674 ms 10596 KB Output is correct
47 Correct 558 ms 7760 KB Output is correct
48 Correct 838 ms 10644 KB Output is correct
49 Correct 780 ms 10592 KB Output is correct
50 Correct 791 ms 10616 KB Output is correct
51 Correct 461 ms 5200 KB Output is correct
52 Correct 642 ms 5388 KB Output is correct
53 Correct 620 ms 5300 KB Output is correct
54 Correct 626 ms 5328 KB Output is correct
55 Correct 714 ms 14072 KB Output is correct
56 Correct 792 ms 14072 KB Output is correct
57 Correct 792 ms 14088 KB Output is correct
58 Correct 693 ms 14076 KB Output is correct
59 Correct 805 ms 35988 KB Output is correct
60 Correct 695 ms 36000 KB Output is correct
61 Correct 410 ms 5200 KB Output is correct
62 Correct 736 ms 5328 KB Output is correct
63 Correct 747 ms 5200 KB Output is correct
64 Correct 723 ms 5456 KB Output is correct
65 Correct 659 ms 5456 KB Output is correct
66 Correct 638 ms 5444 KB Output is correct
67 Correct 786 ms 5456 KB Output is correct
68 Correct 769 ms 5440 KB Output is correct
69 Correct 818 ms 5380 KB Output is correct
70 Correct 714 ms 5392 KB Output is correct
71 Correct 721 ms 6260 KB Output is correct
72 Correct 710 ms 5456 KB Output is correct
73 Correct 577 ms 5408 KB Output is correct
74 Correct 588 ms 5456 KB Output is correct
75 Correct 684 ms 5328 KB Output is correct
76 Correct 667 ms 6612 KB Output is correct
77 Correct 521 ms 6608 KB Output is correct
78 Correct 845 ms 6608 KB Output is correct
79 Correct 531 ms 5584 KB Output is correct
80 Correct 660 ms 5200 KB Output is correct
81 Correct 630 ms 5200 KB Output is correct
82 Correct 861 ms 5328 KB Output is correct
83 Correct 280 ms 5200 KB Output is correct
84 Correct 633 ms 5328 KB Output is correct
85 Correct 653 ms 5328 KB Output is correct
86 Correct 643 ms 5328 KB Output is correct
87 Correct 2 ms 4944 KB Output is correct
88 Correct 557 ms 13352 KB Output is correct
89 Correct 805 ms 10684 KB Output is correct
90 Correct 848 ms 10488 KB Output is correct
91 Correct 718 ms 14208 KB Output is correct
92 Correct 861 ms 14280 KB Output is correct
93 Correct 883 ms 14316 KB Output is correct
94 Correct 603 ms 14204 KB Output is correct
95 Correct 844 ms 14236 KB Output is correct
96 Correct 787 ms 10296 KB Output is correct
97 Correct 788 ms 10248 KB Output is correct
98 Correct 732 ms 31184 KB Output is correct
99 Correct 802 ms 14076 KB Output is correct
100 Correct 838 ms 11944 KB Output is correct
101 Correct 786 ms 11384 KB Output is correct
102 Correct 733 ms 10264 KB Output is correct
103 Correct 784 ms 35984 KB Output is correct
104 Correct 691 ms 36532 KB Output is correct
105 Correct 896 ms 36496 KB Output is correct
106 Correct 842 ms 15220 KB Output is correct
107 Correct 789 ms 10668 KB Output is correct
108 Correct 716 ms 10608 KB Output is correct
109 Correct 856 ms 10392 KB Output is correct