QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#644436#6419. Baby's First Suffix Array Problemrush-from-behind#WA 7ms17148kbC++232.4kb2024-10-16 13:57:362024-10-16 13:57:36

Judging History

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

  • [2024-10-16 13:57:36]
  • 评测
  • 测评结果:WA
  • 用时:7ms
  • 内存:17148kb
  • [2024-10-16 13:57:36]
  • 提交

answer

#include <bits/stdc++.h>
#define mid ((l + r) >> 1)
#define SZ(x) (int) x.size() - 1
#define all(x) x.begin(), x.end()
#define ms(x, y) memset(x, y, sizeof x)
#define F(i, x, y) for (int i = (x); i <= (y); i++)
#define DF(i, x, y) for (int i = (x); i >= (y); i--)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
template <typename T> void chkmax(T &x, T y) {x = max(x, y); }
template <typename T> void chkmin(T &x, T y) {x = min(x, y); }
// template <typename T> void read(T &x) {
// 	x = 0; int f = 1; char c = getchar();
// 	for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
// 	for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
// 	x *= f;
// }
const int N = 1e6 + 10, M = 30;
string st;
int n, m, sa[N], cnt[N], tmp[N], x[N], y[N], q;
void tsort() {
    F(i, 1, m) cnt[i] = 0;
    F(i, 1, n) cnt[x[i]]++;
    F(i, 1, m) cnt[i] += cnt[i - 1];
    DF(i, n, 1) sa[cnt[x[y[i]]]--] = y[i];
}
const int T = N * 30;
int info[T], ls[T], rs[T], val[T], rt[T], tot;
int modify(int lastnum, int l, int r, int x) {
	int num = ++tot;
	val[num] = val[lastnum] + 1;
	if (l != r) {
		if (mid >= x) rs[num] = rs[lastnum], ls[num] = modify(ls[lastnum], l, mid, x);
		else ls[num] = ls[lastnum], rs[num] = modify(rs[lastnum], mid + 1, r, x);
	}
	return num;
}
int query(int num, int l, int r, int L, int R) {
	if (L <= l && r <= R) return val[num];
	if (mid >= R) return query(ls[num], l, mid, L, R);
	if (mid < L) return query(rs[num], mid + 1, r, L, R);
	return query(ls[num], l, mid, L, R) + query(rs[num], mid + 1, r, L, R);
}
void zhk() {
	cin >> n >> q >> st;
	st = ' ' + st; m = 125;
    F(i, 1, n) x[i] = st[i], y[i] = i;
    tsort();
    for (int len = 1; len <= n; len <<= 1) {
        int sz = 0;
        F(i, n - len + 1, n) y[++sz] = i;
        F(i, 1, n)
            if (sa[i] > len) y[++sz] = sa[i] - len;
        tsort();
        swap(x, y);
        m = 0;
        F(i, 1, n) {
            if (y[sa[i]] != y[sa[i - 1]] || y[sa[i] + len] != y[sa[i - 1] + len]) m++;
            x[sa[i]] = m;
        }
		if (m == n) break;
    }
	F(i, 1, n) {
		rt[i] = modify(rt[i - 1], 1, n, x[i]);
	}
	while (q--) {
		int l, r, k; cin >> l >> r >> k;
		cout << query(rt[r], 1, n, 1, x[k]) - query(rt[l - 1], 1, n, 1, x[k]) << '\n';
	}
}
signed main() {
	ios::sync_with_stdio(0); // don't use puts
	cin.tie(0), cout.tie(0);
    int _; cin >> _;
	while (_--) zhk();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 7ms
memory: 17148kb

input:

2
10 4
baaabbabba
2 8 3
1 1 1
2 3 2
2 5 4
20 3
cccbccbadaacbbbcccab
14 17 16
3 20 17
17 20 18

output:

2
1
1
3
4
15
3

result:

wrong answer 3rd numbers differ - expected: '2', found: '1'