QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#864121#2343. First of Her NamewinsunAC ✓1169ms107092kbC++143.1kb2025-01-20 10:38:472025-01-20 10:38:49

Judging History

你现在查看的是最新测评结果

  • [2025-01-20 10:38:49]
  • 评测
  • 测评结果:AC
  • 用时:1169ms
  • 内存:107092kb
  • [2025-01-20 10:38:47]
  • 提交

answer

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cassert>
#include <vector>
#include <queue>
#include <algorithm>
#include <utility>
#define fi first
#define se second
using namespace std;
using LL = long long;
using LLL = __int128;
const int MAXN = 1e6+5;

template <typename Tp> void read(Tp &res) {
    char ch; bool op = 0; res = 0;
    do ch = getchar(), op |= ch == '-'; while (ch < '0' || ch > '9');
    do res = (res<<3)+(res<<1)+(ch&15), ch = getchar(); while(ch >= '0' && ch <= '9');
    if (op) res = -res;
}


bool Mst;
int n, m, q;
int pa[MAXN][21], dep[MAXN], tot;
char str[MAXN];
int sa[MAXN], rk[MAXN], tmp[MAXN], cnt[MAXN], ht[MAXN];
bool Med;

void initpa(int x) {
    for (int k = 1; pa[x][k-1]; ++k) pa[x][k] = pa[pa[x][k-1]][k-1];
}

void rsort() {
    for (int i = 1; i <= m; ++i) cnt[i] = 0;
    for (int i = 1; i <= n; ++i) ++cnt[rk[i]];
    for (int i = 1; i <= m; ++i) cnt[i] += cnt[i-1];
    for (int i = n; i; --i) sa[cnt[rk[tmp[i]]]--] = tmp[i];
}

void suffixSort() {
    m = 26;
    for (int i = 1; i <= n; ++i) tmp[i] = n - i + 1, rk[i] = str[i] - 'A' + 1;
    rsort();
    for (int k = 0; k <= 20; ++k) {
        int x = 0;
        for (int i = 1; i <= m; ++i) cnt[i] = 0;
        for (int i = 1; i <= n; ++i) !pa[i][k] ? tmp[++x] = i : ++cnt[rk[pa[i][k]]];
        for (int i = 1; i <= m; ++i) cnt[i] += cnt[i-1];
        for (int i = n; i; --i) if (pa[i][k]) tmp[x+cnt[rk[pa[i][k]]]--] = i;
        rsort();
        for (int i = 1; i <= n; ++i) tmp[i] = rk[i];
        auto cmp = [k](int x, int y) { return tmp[x] != tmp[y] || tmp[pa[x][k]] != tmp[pa[y][k]]; };
        rk[sa[1]] = m = 1;
        for (int i = 2; i <= n; ++i) rk[sa[i]] = m += cmp(sa[i], sa[i-1]);
    }
}

char t[MAXN]; int len;
int cmp(int x) {
    int lim = min(len, dep[x]);
    for (int i = 1; i <= lim; x = pa[x][0], ++i) {
        if (t[i] < str[x]) return 1;
        if (t[i] > str[x]) return -1;
    }
    return -(lim != len);
}

int queryL() {
    int l = 1, r = n + 1;
    while (l < r) {
        int mid = (l + r) >> 1;
        cmp(sa[mid]) >= 0 ? r = mid : l = mid + 1; 
    } 
    return l;
}

int queryR() {
    int l = 1, r = n + 1;
//    for (int i = 1; i <= n; ++i) printf("%d%c", cmp(sa[i]), " \n"[i==n]);
    while (l < r) {
        int mid = (l + r) >> 1;
        cmp(sa[mid]) > 0 ? r = mid : l = mid + 1;
    }
    return l;
}

int main() {
    #ifndef ONLINE_JUDGE
    freopen("lg6257.in", "r", stdin);
    freopen("lg6257.out", "w", stdout);
    fprintf(stderr, "%.3lf\n", (&Mst - &Med) / 1048576.0);
    #endif
    read(n), read(q);
    for (int i = 1; i <= n; ++i) {
        int p;
        scanf("%s%d", &str[i], &p);
        dep[i] = dep[p] + 1; 
        pa[i][0] = p;
        initpa(i);
    }
    suffixSort();
//    for (int i = 1; i <= n; ++i) {
//        int x = sa[i];
//        for (; x; x = pa[x][0]) putchar(str[x]);
//        putchar('\n');
//    }
    while (q--) {
        scanf("%s", t + 1);
        len = strlen(t + 1);
        int lb = queryL(), rb = queryR();
        printf("%d\n", rb - lb);
    }
    return 0;
}

Details

Test #1:

score: 100
Accepted
time: 0ms
memory: 14060kb

Test #2:

score: 0
Accepted
time: 0ms
memory: 14060kb

Test #3:

score: 0
Accepted
time: 0ms
memory: 14064kb

Test #4:

score: 0
Accepted
time: 0ms
memory: 14056kb

Test #5:

score: 0
Accepted
time: 0ms
memory: 14052kb

Test #6:

score: 0
Accepted
time: 0ms
memory: 13876kb

Test #7:

score: 0
Accepted
time: 0ms
memory: 14048kb

Test #8:

score: 0
Accepted
time: 0ms
memory: 14060kb

Test #9:

score: 0
Accepted
time: 0ms
memory: 14056kb

Test #10:

score: 0
Accepted
time: 0ms
memory: 14064kb

Test #11:

score: 0
Accepted
time: 1018ms
memory: 106672kb

Test #12:

score: 0
Accepted
time: 0ms
memory: 14060kb

Test #13:

score: 0
Accepted
time: 0ms
memory: 14056kb

Test #14:

score: 0
Accepted
time: 0ms
memory: 14060kb

Test #15:

score: 0
Accepted
time: 0ms
memory: 14056kb

Test #16:

score: 0
Accepted
time: 1ms
memory: 13844kb

Test #17:

score: 0
Accepted
time: 0ms
memory: 14064kb

Test #18:

score: 0
Accepted
time: 0ms
memory: 14060kb

Test #19:

score: 0
Accepted
time: 0ms
memory: 14056kb

Test #20:

score: 0
Accepted
time: 0ms
memory: 14060kb

Test #21:

score: 0
Accepted
time: 0ms
memory: 14056kb

Test #22:

score: 0
Accepted
time: 0ms
memory: 14064kb

Test #23:

score: 0
Accepted
time: 4ms
memory: 14056kb

Test #24:

score: 0
Accepted
time: 3ms
memory: 14060kb

Test #25:

score: 0
Accepted
time: 2ms
memory: 16144kb

Test #26:

score: 0
Accepted
time: 1ms
memory: 15980kb

Test #27:

score: 0
Accepted
time: 2ms
memory: 16104kb

Test #28:

score: 0
Accepted
time: 3ms
memory: 14100kb

Test #29:

score: 0
Accepted
time: 2ms
memory: 14056kb

Test #30:

score: 0
Accepted
time: 3ms
memory: 14100kb

Test #31:

score: 0
Accepted
time: 2ms
memory: 16104kb

Test #32:

score: 0
Accepted
time: 10ms
memory: 16108kb

Test #33:

score: 0
Accepted
time: 17ms
memory: 20360kb

Test #34:

score: 0
Accepted
time: 807ms
memory: 106232kb

Test #35:

score: 0
Accepted
time: 1169ms
memory: 104836kb

Test #36:

score: 0
Accepted
time: 920ms
memory: 105716kb

Test #37:

score: 0
Accepted
time: 896ms
memory: 105484kb

Test #38:

score: 0
Accepted
time: 933ms
memory: 105440kb

Test #39:

score: 0
Accepted
time: 871ms
memory: 106908kb

Test #40:

score: 0
Accepted
time: 712ms
memory: 106792kb

Test #41:

score: 0
Accepted
time: 799ms
memory: 104484kb

Test #42:

score: 0
Accepted
time: 1056ms
memory: 107092kb

Test #43:

score: 0
Accepted
time: 1080ms
memory: 105080kb

Test #44:

score: 0
Accepted
time: 836ms
memory: 105984kb

Test #45:

score: 0
Accepted
time: 1072ms
memory: 105156kb

Test #46:

score: 0
Accepted
time: 0ms
memory: 14052kb

Test #47:

score: 0
Accepted
time: 0ms
memory: 14056kb

Test #48:

score: 0
Accepted
time: 0ms
memory: 14052kb

Test #49:

score: 0
Accepted
time: 0ms
memory: 14056kb

Test #50:

score: 0
Accepted
time: 0ms
memory: 14056kb

Test #51:

score: 0
Accepted
time: 1ms
memory: 13980kb

Test #52:

score: 0
Accepted
time: 0ms
memory: 14060kb

Test #53:

score: 0
Accepted
time: 1ms
memory: 14056kb

Test #54:

score: 0
Accepted
time: 4ms
memory: 14088kb

Test #55:

score: 0
Accepted
time: 0ms
memory: 14072kb

Test #56:

score: 0
Accepted
time: 6ms
memory: 16196kb