Submission #401492

# Submission time Handle Problem Language Result Execution time Memory
401492 2021-05-10T12:03:42 Z tranxuanbach Brperm (RMI20_brperm) C++17
100 / 100
205 ms 15724 KB
#include "brperm.h"

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#pragma GCC optimize("O3,unroll-loops,no-stack-protector")
#pragma GCC target("mmx,sse,sse2,sse3,ssse3,sse4,avx,avx2,fma")

#define endl '\n'
#define fi first
#define se second
#define For(i, l, r) for (int i = l; i < r; i++)
#define ForE(i, l, r) for (int i = l; i <= r; i++)
#define FordE(i, l, r) for (int i = l; i >= r; i--)
#define Fora(v, a) for (auto v: a)
#define bend(a) a.begin(), a.end()
#define isz(a) ((signed)a.size())

typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;
typedef vector <int> vi;
typedef vector <pii> vpii;
typedef vector <vi> vvi;

const int N = 5e5 + 5;
const int BIT = 18;

namespace Namespace{
    bool sub3 = 1;
    int n;
    char s[N];
    int cnta[N], cntb[N];
    vi posa, posb;
    int rev[BIT + 1][1 << BIT];
    mt19937 rando(chrono::steady_clock::now().time_since_epoch().count());
}

using namespace Namespace;

void init(int cacn, const char cacs[]){
    n = cacn;
    For(i, 0, n){
        s[i] = cacs[i];
        cnta[i] = (i == 0 ? 0 : cnta[i - 1]); cntb[i] = (i == 0 ? 0 : cntb[i - 1]);
        if (s[i] == 'a'){
            cnta[i]++;
            posa.emplace_back(i);
        }
        if (s[i] == 'b'){
            cntb[i]++;
            posb.emplace_back(i);
        }
        if (s[i] != 'a' and s[i] != 'b'){
            sub3 = 0;
        }
    }
    rev[0][0] = 0;
    ForE(l, 1, BIT){
        For(i, 0, (1 << l)){
            rev[l][i] = 0;
            For(j, 0, l){
                rev[l][i] = rev[l][i] * 2 + ((i & (1 << j)) ? 1 : 0);
            }
        }
    }
    return;
}

int query(int i, int k){
    if (i + (1 << k) - 1 >= n){
        return 0;
    }
    if (k <= 0){
        For(j, 0, (1 << k)){
            if (s[i + j] != s[i + rev[k][j]]){
                return 0;
            }
        }
        return 1;
    }
    if (sub3){
        if (cnta[i + (1 << k) - 1] - (i == 0 ? 0 : cnta[i - 1]) <= (1 << 10)){
            int idx = lower_bound(bend(posa), i) - posa.begin();
            while (idx < isz(posa) and posa[idx] <= i + (1 << k) - 1){
                if (s[posa[idx]] != s[i + rev[k][posa[idx] - i]]){
                    return 0;
                }
                idx++;
            }
            return 1;
        }
        else if (cntb[i + (1 << k) - 1] - (i == 0 ? 0 : cntb[i - 1]) <= (1 << 10)){
            int idx = lower_bound(bend(posb), i) - posb.begin();
            while (idx < isz(posb) and posb[idx] <= i + (1 << k) - 1){
                if (s[posb[idx]] != s[i + rev[k][posb[idx] - i]]){
                    return 0;
                }
                idx++;
            }
            return 1;
        }
        else{
            return 0;
        }
    }
    For(j, 0, (1 << k)){
        if (s[i + j] != s[i + rev[k][j]]){
            return 0;
        }
    }
    return 1;
}

/*
==================================================+
INPUT:                                            |
--------------------------------------------------|

--------------------------------------------------|
==================================================+
OUTPUT:                                           |
--------------------------------------------------|

--------------------------------------------------|
==================================================+
*/
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2384 KB Output is correct
2 Correct 4 ms 2380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2384 KB Output is correct
2 Correct 4 ms 2380 KB Output is correct
3 Correct 33 ms 4588 KB Output is correct
4 Correct 31 ms 4548 KB Output is correct
5 Correct 31 ms 4500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 167 ms 15724 KB Output is correct
2 Correct 205 ms 15340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2384 KB Output is correct
2 Correct 4 ms 2380 KB Output is correct
3 Correct 33 ms 4588 KB Output is correct
4 Correct 31 ms 4548 KB Output is correct
5 Correct 31 ms 4500 KB Output is correct
6 Correct 167 ms 15724 KB Output is correct
7 Correct 205 ms 15340 KB Output is correct
8 Correct 142 ms 13548 KB Output is correct
9 Correct 145 ms 13588 KB Output is correct
10 Correct 146 ms 13548 KB Output is correct