답안 #494410

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
494410 2021-12-15T12:01:31 Z jasen_penchev Brperm (RMI20_brperm) C++14
100 / 100
2031 ms 253800 KB
#include "brperm.h"
#include <iostream>
#define endl '\n'
using namespace std;

const int MAXN = 500000;
const int LOG = 20;
const int BASE = 26;
const int MOD1 = 1000000007;
const int MOD2 = 1000000009;

int n;
int pw[MAXN + 1][2];

struct Hash
{
    int len;
    int num1, num2;
};

Hash operator + (Hash h1, Hash h2)
{
    Hash ret;
    ret.len = h1.len + h2.len;
    ret.num1 = (1ll * h1.num1 * pw[h2.len][0] + h2.num1) % MOD1;
    ret.num2 = (1ll * h1.num2 * pw[h2.len][1] + h2.num2) % MOD2;
    return ret;
}

bool operator == (Hash h1, Hash h2)
{
    return (h1.len == h2.len and h1.num1 == h2.num1 and h1.num2 == h2.num2);
}

Hash h1[MAXN + 1][LOG + 1];
Hash h2[MAXN + 1][LOG + 1];

void init(int n0, const char s[])
{
    n = n0;

    pw[0][0] = pw[0][1] = 1;
    for (int i = 1; i <= MAXN; ++ i)
    {
        pw[i][0] = (1ll * pw[i - 1][0] * BASE) % MOD1;
        pw[i][1] = (1ll * pw[i - 1][1] * BASE) % MOD2;
    }

    for (int i = 0; i < n; ++ i)
    {
        h1[i][0].len = 1;
        h1[i][0].num1 = h1[i][0].num2 = s[i] - 'a';
    }

    for (int j = 1; j <= LOG; ++ j)
    {
        for (int i = 0; i < n - (1ll << (j - 1)); ++ i)
        {
            h1[i][j] = h1[i][j - 1] + h1[i + (1ll << (j - 1))][j - 1];
        }
    }

    for (int j = 0; j <= LOG; ++ j)
    {
        for (int i = 0; i < n; ++ i)
        {
            h2[i][j].len = 1;
            h2[i][j].num1 = h2[i][j].num2 = s[i] - 'a';
        }
        for (int k = j - 1; k >= 0; -- k)
        {
            for (int i = 0; i < n - (1ll << k); ++ i)
            {
                h2[i][j] = h2[i][j] + h2[i + (1ll << k)][j];
            }
        }
    }

    return;
}

int query(int pos, int k)
{
    if (pos + (1ll << k) > n) return 0;
    return (h1[pos][k] == h2[pos][k]);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 4684 KB Output is correct
2 Correct 8 ms 4684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 4684 KB Output is correct
2 Correct 8 ms 4684 KB Output is correct
3 Correct 319 ms 53896 KB Output is correct
4 Correct 347 ms 53784 KB Output is correct
5 Correct 289 ms 53748 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1958 ms 252228 KB Output is correct
2 Correct 1981 ms 253616 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 4684 KB Output is correct
2 Correct 8 ms 4684 KB Output is correct
3 Correct 319 ms 53896 KB Output is correct
4 Correct 347 ms 53784 KB Output is correct
5 Correct 289 ms 53748 KB Output is correct
6 Correct 1958 ms 252228 KB Output is correct
7 Correct 1981 ms 253616 KB Output is correct
8 Correct 1961 ms 253800 KB Output is correct
9 Correct 2031 ms 253740 KB Output is correct
10 Correct 2013 ms 253676 KB Output is correct