Submission #858073

# Submission time Handle Problem Language Result Execution time Memory
858073 2023-10-07T11:39:31 Z boris_mihov Brperm (RMI20_brperm) C++17
100 / 100
521 ms 161904 KB
#include "brperm.h"
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>

typedef long long llong;
const int MAXN = 500000 + 10;
const int MOD = 998244353;
const int MAXLOG = 19;

int n;
char s1[MAXN];
int base[3 * MAXN];

struct Sparse
{
    int normalHash[MAXN][MAXLOG];
    int danceHash[MAXN][2][MAXLOG];
    int answerHash[MAXN][MAXLOG];

    void build()
    {
        for (int i = 0 ; i < n ; ++i)
        {
            normalHash[i][0] = s1[i] - 'a' + 1;
            for (int lgH = 0 ; lgH < MAXLOG ; ++lgH)
            {
                danceHash[i][0][lgH] = s1[i] - 'a' + 1;
            }
        }

        for (int lgLen = 1 ; (1 << lgLen) <= n ; ++lgLen)
        {
            for (int i = 0 ; i + (1 << lgLen) <= n ; ++i)
            {
                normalHash[i][lgLen] = (1LL * base[(1 << (lgLen - 1))] * normalHash[i][(lgLen - 1)] + 1LL * normalHash[i + (1 << (lgLen - 1))][(lgLen - 1)]) % MOD;
                for (int lgH = 0 ; lgH + 1 < MAXLOG ; ++lgH)
                {
                    danceHash[i][lgLen & 1][lgH] = (1LL * base[1 << lgH] * danceHash[i][(lgLen - 1) & 1][lgH + 1] + 1LL * danceHash[i + (1 << (lgLen - 1))][(lgLen - 1) & 1][lgH + 1]) % MOD;
                }

                answerHash[i][lgLen] = danceHash[i][lgLen & 1][0];
            }        
        }
    }

    int query(int idx, int log)
    {
        if ((idx + (1 << log)) > n) return 0;
        return normalHash[idx][log] == answerHash[idx][log];
    }
};

Sparse sparse;
void init(int N, const char S[]) 
{
    n = N;
    for (int i = 0 ; i < n ; ++i)
    {
        s1[i] = S[i];
    }

    base[0] = 1;
    for (int i = 1 ; i < 3 * MAXN ; ++i)
    {
        base[i] = (27LL * base[i - 1]) % MOD;
    }

    sparse.build();
}

int query(int idx, int log) 
{
    return sparse.query(idx, log);
}
# Verdict Execution time Memory Grader output
1 Correct 9 ms 11100 KB Output is correct
2 Correct 9 ms 11220 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 11100 KB Output is correct
2 Correct 9 ms 11220 KB Output is correct
3 Correct 90 ms 40336 KB Output is correct
4 Correct 94 ms 40576 KB Output is correct
5 Correct 94 ms 40280 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 521 ms 156840 KB Output is correct
2 Correct 503 ms 161852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 11100 KB Output is correct
2 Correct 9 ms 11220 KB Output is correct
3 Correct 90 ms 40336 KB Output is correct
4 Correct 94 ms 40576 KB Output is correct
5 Correct 94 ms 40280 KB Output is correct
6 Correct 521 ms 156840 KB Output is correct
7 Correct 503 ms 161852 KB Output is correct
8 Correct 521 ms 161904 KB Output is correct
9 Correct 494 ms 161884 KB Output is correct
10 Correct 491 ms 161904 KB Output is correct