QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#51308 | #4631. Interesting String Problem | Hongzy | WA | 0ms | 5892kb | C++ | 4.2kb | 2022-10-01 17:40:57 | 2022-10-01 17:41:00 |
Judging History
answer
#include <bits/stdc++.h>
#define LOG(FMT...) fprintf(stderr, FMT);
#define rep(i, j, k) for(int i = j; i <= k; ++ i)
#define per(i, j, k) for(int i = j; i >= k; -- i)
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
typedef double db;
typedef long long ll;
typedef long double ldb;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
mt19937 mt(std::chrono::system_clock::now().time_since_epoch().count());
uniform_int_distribution<ll> ran(0, 1ll << 62);
void ucin() { ios::sync_with_stdio(0); cin.tie(0); }
// uniform_real_distribution<double> dbran;
template<class T> inline void chkmax(T &x, const T &y) { if(x < y) x = y; }
template<class T> inline void chkmin(T &x, const T &y) { if(x > y) x = y; }
inline ll sqr(ll x) { return x * x; }
inline ll cub(ll x) { return x * x * x; }
const int N = 5e5 + 5, M = N * 42;
const int mod = 1e9 + 7;
int n, id, rk[N * 2], sa[N * 2], cnt[N * 2], t[N * 2], h[N * 2];
char s[N * 2];
int rt[N], sz[M], ls[M], rs[M];
void insert(int p, int &u, int l, int r, int x) {
u = ++ id;
sz[u] = sz[p] + 1;
if(l == r) return ;
int mid = (l + r) >> 1;
if(x <= mid) rs[u] = rs[p], insert(ls[p], ls[u], l, mid, x);
else ls[u] = ls[p], insert(rs[p], rs[u], mid + 1, r, x);
}
int kth(int tu, int tv, int l, int r, int k) {
if(l == r) return l;
int mid = (l + r) >> 1;
if(k <= sz[ls[tu]] - sz[ls[tv]])
return kth(ls[tu], ls[tv], l, mid, k);
return kth(rs[tu], rs[tv], mid + 1, r, k - (sz[ls[tu]] - sz[ls[tv]]));
}
int bd[N * 50], len, L[N * 2], R[N * 2];
ll nb[N * 50];
ll pre[N * 2];
bool tag;
void build() {
int m = *max_element(s + 1, s + n + 1);
rep(i, 1, n) cnt[rk[i] = s[i]] ++;
rep(i, 1, m) cnt[i] += cnt[i - 1];
rep(i, 1, n) sa[cnt[rk[i]] --] = i;
for(int k = 1, z = 0; k < n; k <<= 1, m = z, z = 0) {
rep(i, n - k + 1, n) t[++ z] = i;
rep(i, 1, n) if(sa[i] > k) t[++ z] = sa[i] - k;
rep(i, 1, m) cnt[i] = 0;
rep(i, 1, n) cnt[rk[i]] ++;
rep(i, 1, m) cnt[i] += cnt[i - 1];
per(i, n, 1) sa[cnt[rk[t[i]]] --] = t[i];
copy(rk + 1, rk + n + 1, t + 1); rk[sa[1]] = z = 1;
rep(i, 2, n) {
if(t[sa[i]] == t[sa[i - 1]] && t[sa[i] + k] == t[sa[i - 1] + k]) {
rk[sa[i]] = z;
} else {
rk[sa[i]] = ++ z;
}
}
if(z == n) break ;
}
int z = 0;
rep(i, 1, n) {
if(rk[i] == 1) {
h[1] = z = 0; continue ;
}
if(z) z --;
int u = sa[rk[i] - 1];
while(s[u + z] == s[i + z]) z ++;
h[rk[i]] = z;
}
rep(i, 1, n) {
insert(rt[i-1], rt[i], 1, n, sa[i]);
// printf("sa%d = %d\n", i, sa[i]);
}
static int st[N * 2];
int top = 0;
h[n + 1] = 0;
st[top = 1] = n + 1;
// rep(i, 1, n) printf("%s\n", &s[sa[i]]);
// rep(i, 1, n) printf("h%d = %d\n", i, h[i]);
per(i, n, 1) {
// printf("%d: ", i);
L[i] = len+1;
rep(j, h[i] + 1, n - sa[i] + 1) {
int l = 1, r = top, z = 1;
while(l <= r) {
int mid = (l + r) >> 1;
if(h[st[mid]] < j) l = (z=mid)+1;
else r = mid-1;
}
len++;
bd[len] = st[z]-1;
nb[len] = 1ll * j * (st[z]-i);
pre[i] += 1ll * j * (st[z]-i);
if(len > 2*n) {
puts("?");
}
// printf("[%d]", st[z]-1);
}
R[i] = len;
for(int j = L[i] + 1; j <= R[i]; j ++)
nb[j] += nb[j-1];
// puts("");
while(top && h[st[top]] >= h[i])
-- top;
st[++ top] = i;
}
rep(i, 1, n) pre[i] += pre[i-1];
}
int q;
ll solve(ll k) {
int z = lower_bound(pre + 1, pre + n + 1, k) - pre;
k -= pre[z-1];
int c = lower_bound(nb + L[z], nb + R[z] + 1, k) - nb;
k -= (c != L[z] ? nb[c-1] : 0);
int j = h[z]+1 + c-L[z];
// printf("[%d, %d] %lld = %d\n", z, bd[z][c], (k+j-1)/j, kth(rt[bd[z][c]], rt[z-1], 1, n, (k+j-1)/j));
return kth(rt[bd[c]], rt[z-1], 1, n, (k+j-1)/j) + ((k-1) % j);
}
int main() {
scanf("%s", s + 1);
n = strlen(s + 1);
if(s[1] == 'o' && s[2] == 's') {
tag = 1;
}
build();
scanf("%d", &q);
rep(T, 1, q) {
ll k;
scanf("%lld", &k);
printf("%lld\n", solve(k));
}
return 0;
}
/*
pbpbppb
3
1
2
3
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 5892kb
input:
pbpbppb 3 1 2 3
output:
? ? ? ? ? 2 4 7
result:
wrong answer 1st lines differ - expected: '2', found: '?'