Submission #757153

#TimeUsernameProblemLanguageResultExecution timeMemory
757153boris_mihovDigital Circuit (IOI22_circuit)C++17
46 / 100
3052 ms19448 KiB
#include "circuit.h"
#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
 
typedef long long llong;
const int MAXN = 200000 + 10;
const int MOD = 1000002022;
const int INF = 1e9;
 
int n, m;
llong w[MAXN];
llong b[MAXN];
llong pw[MAXN];
llong multBy[MAXN];
std::vector <int> g[MAXN];
std::vector <int> prefixPW[MAXN];
std::vector <int> suffixPW[MAXN];

void initDFS(int node)
{
    if (g[node].empty())
    {
        pw[node] = 1;
        return;
    }
    
    pw[node] = g[node].size();
    for (const int &i : g[node])
    {
        initDFS(i);
        pw[node] *= pw[i];
        pw[node] %= MOD;
    }

    prefixPW[node].resize(g[node].size());
    prefixPW[node][0] = 1;

    for (int i = 1 ; i < g[node].size() ; ++i)
    {
        prefixPW[node][i] = (prefixPW[node][i - 1] * pw[g[node][i - 1]]) % MOD;
    }

    suffixPW[node].resize(g[node].size());
    suffixPW[node][g[node].size() - 1] = 1;
    for (int i = (int)g[node].size() - 2 ; i >= 0 ; --i)
    {
        suffixPW[node][i] = (suffixPW[node][i + 1] * pw[g[node][i + 1]]) % MOD;
    }
}

void findMult(int node, llong mult)
{
    if (g[node].empty())
    {
        multBy[node] = mult;
        return;
    }
    
    for (int i = 0 ; i < g[node].size() ; ++i)
    {
        findMult(g[node][i], (((mult * prefixPW[node][i]) % MOD) * suffixPW[node][i]) % MOD);
    }
}

void init(int N, int M, std::vector <int> P, std::vector <int> A) 
{
    n = N;
    m = M;
    for (int i = 1 ; i < n + m ; ++i)
    {
        g[P[i] + 1].push_back(i + 1);
    }
 
    for (int i = n + 1 ; i <= n + m ; ++i)
    {
        if (A[i - n - 1] == 0)
        {
            w[i] = 1;
            b[i] = 0;
        } else
        {
            b[i] = 1;
            w[i] = 0;
        }
    }

    initDFS(1);
    findMult(1, 1);
}
 
int count_ways(int L, int R) 
{
    L++; R++;
    for (int i = L ; i <= R ; ++i)
    {
        std::swap(b[i], w[i]);
    }
 
    llong res = 0;
    for (int i = n + 1 ; i <= n + m ; ++i)
    {
        res += multBy[i] * b[i];
        if (res >= MOD) res -= MOD;
    }

    return res;
}

Compilation message (stderr)

circuit.cpp: In function 'void initDFS(int)':
circuit.cpp:40:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   40 |     for (int i = 1 ; i < g[node].size() ; ++i)
      |                      ~~^~~~~~~~~~~~~~~~
circuit.cpp: In function 'void findMult(int, llong)':
circuit.cpp:61:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   61 |     for (int i = 0 ; i < g[node].size() ; ++i)
      |                      ~~^~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...