QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#487909 | #1942. Another Substring Query Problem | LR | AC ✓ | 293ms | 149396kb | C++20 | 5.5kb | 2024-07-23 12:23:28 | 2024-07-23 12:23:28 |
Judging History
answer
#include<bits/stdc++.h>
#define int long long
#define ii pair<int,int>
#define ff first
#define ss second
using namespace std;
const int maxn = 4e5 + 5;
int n;
string s;
#define ia array<int, 2>
const ia mod = {2409235909, (int)1e9 + 9}, base = {631, 311};
ia mu[maxn];
int lcp[maxn];
ia operator - (ia a, ia b)
{
ia c;
for (int i = 0; i < 2; i++)
c[i] = (a[i] - b[i] + mod[i]) % mod[i];
return c;
}
ia operator + (ia a, ia b)
{
ia c;
for (int i = 0; i < 2; i++)
{
c[i] = (a[i] + b[i]);
if (c[i] >= mod[i])
c[i] -= mod[i];
}
return c;
}
ia operator * (ia a, ia b)
{
ia c;
for (int i = 0; i < 2; i++)
c[i] = (a[i] * b[i]) % mod[i];
return c;
}
inline ia makepair(char c)
{
ia tmp = {c - 'a' + 1, c - 'a' + 1};
return tmp;
}
ia h[maxn];
void build(string &s)
{
mu[0] = {1, 1};
for (int i = 1; i < maxn; i++)
mu[i] = mu[i - 1] * base;
h[0] = {0, 0};
for (int i = 0; i < s.size(); i++)
h[i + 1] = h[i] * base + makepair(s[i]);
}
inline bool check(int u, int v, int x, int y)
{
return (h[v] - (h[u - 1] * mu[v - u + 1])) == (h[y] - (h[x - 1] * mu[y - x + 1]));
}
void _sort(vector<pair<ii,int>> &a)
{
vector<pair<ii,int>> a_new(n);
{
vector<int> cnt(n, 0), pos(n, 0);
for (auto i : a)
cnt[i.ff.ss]++;
for (int i = 1; i < n; i++)
pos[i] = pos[i - 1] + cnt[i - 1];
for (auto i : a)
a_new[pos[i.ff.ss]] = i,
pos[i.ff.ss]++;
a.swap(a_new);
}
{
vector<int> cnt(n, 0), pos(n, 0);
for (auto i : a)
cnt[i.ff.ff]++;
for (int i = 1; i < n; i++)
pos[i] = pos[i - 1] + cnt[i - 1];
for (auto i : a)
a_new[pos[i.ff.ff]] = i,
pos[i.ff.ff]++;
a.swap(a_new);
}
}
int get_same(int pos1, int pos2)
{
int l = 1, r = min(n - pos1 - 1, n - pos2 - 1), ans = 0;
while (l <= r)
{
int mid = (l + r) / 2;
if (check(pos1 + 1, pos1 + mid, pos2 + 1, pos2 + mid) && s[pos1 + mid - 1] != '$')
ans = mid, l = mid + 1;
else r = mid - 1;
}
return ans;
}
vector<int> suffix_array(string s)
{
int n = s.size();
vector<int> p(n), r(n);
vector<ii> a(n);
for (int i = 0; i < n; i++)
a[i] = {(int)s[i], i};
sort(a.begin(), a.end());
for (int i = 0; i < n; i++)
{
p[i] = a[i].ss;
r[p[i]] = 0;
if (i > 0)
r[p[i]] = r[p[i - 1]] + (a[i].ff != a[i - 1].ff);
}
for (int k = 0; (1 << k) < n; k++)
{
vector<pair<ii,int>> a(n);
for (int i = 0; i < n; i++)
a[i] = {{r[i], r[(i + (1 << k)) % n]}, i};
_sort(a);
for (int i = 0; i < n; i++)
{
p[i] = a[i].ss;
r[p[i]] = 0;
if (i > 0)
r[p[i]] = r[p[i - 1]] + (a[i].ff != a[i - 1].ff);
}
}
return p;
}
struct node{
node *l, *r;
int sum;
node(int val = 0)
{
sum = val;
l = r = 0;
}
node (node *lx, node *rx)
{
l = lx;
r = rx;
sum = (l ? l -> sum : 0) + (r ? r -> sum : 0);
}
};
node *v[maxn];
int lim;
inline void build(node *id, int l = 0, int r = lim)
{
if (l == r)
{
id -> sum = 0;
return;
}
int mid = (l + r) / 2;
build(id -> l = new node(), l, mid);
build(id -> r = new node(), mid + 1, r);
id -> sum = id -> l -> sum + id -> r -> sum;
}
inline node* upd(int pos, int val, node *id, int l = 0, int r = lim)
{
if (l == r) return new node(val);
int mid = (l + r) / 2;
if (pos <= mid) return new node(upd(pos, val, id -> l, l, mid), id -> r);
return new node(id -> l, upd(pos, val, id -> r, mid + 1, r));
}
int qry(node *x, node *y, int k, int l = 0, int r = lim)
{
if (l == r) return l;
int mid = (l + r) / 2;
int le = y -> l -> sum - x -> l -> sum;
if (le >= k) return qry(x -> l, y -> l, k, l, mid);
return qry(x -> r, y -> r, k - le, mid + 1, r);
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
// freopen("test.inp", "r", stdin);
// freopen("test.out", "w", stdout);
cin >> s;
s += '#';
n = s.size();
build(s);
vector<int> p = suffix_array(s);
lim = n;
v[0] = new node();
build(v[0]);
for (int i = 1; i < n; i++)
v[i] = v[i - 1],
v[i] = upd(p[i], 1, v[i]);
int q; cin >> q;
while (q--)
{
string qr;
int k;
cin >> qr >> k;
int no = 0, rl = 1, rr = n - 1;
for (int i = 0; i < qr.size(); i++)
{
int l = rl, r = rr, ansl = n + 1, ansr = 0;
while (l <= r)
{
int mid = (l + r) / 2;
if (p[mid] + i < n && s[p[mid] + i] >= qr[i])
ansl = mid, r = mid - 1;
else l = mid + 1;
}
l = rl, r = rr;
while (l <= r)
{
int mid = (l + r) / 2;
if (p[mid] + i < n && s[p[mid] + i] <= qr[i])
ansr = mid, l = mid + 1;
else r = mid - 1;
}
rl = ansl;
rr = ansr;
}
if (rr - rl + 1 < k) cout << -1 << "\n";
else cout << qry(v[rl - 1], v[rr], k) + 1 << "\n";
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 161ms
memory: 148832kb
input:
hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh...
output:
50997 112451 97534 13355 150562 5772 146250 10904 4343 194777 71395 96501 6809 18571 166901 114789 123587 176339 29660 82650 63509 80101 47907 66143 199971 108032 140518 49221 46212 28127 61461 149025 142818 33646 17132 196161 36671 28656 16860 141951 139429 72866 162807 21343 194803 16263 118701 20...
result:
ok 631 lines
Test #2:
score: 0
Accepted
time: 288ms
memory: 149052kb
input:
gnzlzbsarybaxilacuopvdkhjfiynrgfdhsrecyxedcrriztcxboczhfaseqglcwshaewaepasxqpaxxbroxndbxqabrvtrrshrnnlyfodtxyltjkbghfkjcqrqsuwdsgypcaroaojqbfrjqbdrimglxcqtfmqaeogqunaegfpfzcmyjwfgbwaigwitcyspiedbisjuedeyseneuzjdteifzvdfzhenapmmreqpvwsmtrxowjnmmrthzhojgxsoppfgfgohoghuwvqclzsmeydusfndrdyqokpqwupixyoxr...
output:
-1 -1 174057 -1 141370 -1 75407 -1 112842 -1 -1 4510 100584 12223 35161 28768 -1 5258 151196 -1 25061 87769 -1 135272 -1 -1 -1 159851 -1 -1 121971 108762 60263 143662 -1 5200 65515 -1 -1 -1 -1 157064 65499 -1 84122 173745 171855 -1 193921 -1 121426 162134 137032 126244 99210 155648 95498 151755 1105...
result:
ok 54751 lines
Test #3:
score: 0
Accepted
time: 293ms
memory: 148960kb
input:
gnzlzbsarybaxilacuopvdkhjfiynrgfdhsrecyxedcrriztcxboczhfaseqglcwshaewaepasxqpaxxbroxndbxqabrvtrrshrnnlyfodtxyltjkbghfkjcqrqsuwdsgypcaroaojqbfrjqbdrimglxcqtfmqaeogqunaegfpfzcmyjwfgbwaigwitcyspiedbisjuedeyseneuzjdteifzvdfzhenapmmreqpvwsmtrxowjnmmrthzhojgxsoppfgfgohoghuwvqclzsmeydusfndrdyqokpqwupixyoxr...
output:
-1 -1 -1 66435 197872 -1 83779 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 91584 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 21808 -1 -1 -1 -1 -1 15575 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 196132 -...
result:
ok 200000 lines
Test #4:
score: 0
Accepted
time: 281ms
memory: 148680kb
input:
qzpazeynjuulxowssvbghqhdtywsnzgwmpospvxyeoxsrwxccwucqgsyllrkburhfrivzqcpnevmixivrtbxexhrjauxayxmrdluzkvxnfvocgoceavurwlxzdfepcxzcdaoppzfhqpyhmjlhbfsntaewlfjqgefvqttczahgzgucgoohnjsdvyaxltixdvbgzoewptjuosymanxeiewbvnibhjbkhoebygmtmwldtvyijasjquxvtwkhlngbcremdsprdetxkggogyjbgsuduzmutcgblzoqopwspbzrurs...
output:
109498 154508 -1 -1 -1 -1 1028 36133 -1 -1 -1 -1 -1 191213 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 129366 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 196816 -1 -1 141983 -1 -1 137732 -1 11350 -1 -1 -1 25285 52189 -1 -1 -1 -1 80999 -1 -1 17773 5870 -1 -1 -1 -1 -1 128813 -1 1492 -1 -1 1513 -1 -1 -1 139760 199034 -1 -1 49122 ...
result:
ok 50034 lines
Test #5:
score: 0
Accepted
time: 0ms
memory: 11936kb
input:
dkfjahfwbaofboahsdlvkjnalsf 10 dkfjahfwbaofboahsdlvkjnalsfd 1 adkfjahfwbaofboahsdlvkjnalsf 1 kfj 2 kfj 1 a 3 a 1 a 2 a 4 w 2 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 1
output:
-1 -1 -1 2 15 5 10 24 -1 -1
result:
ok 10 lines
Test #6:
score: 0
Accepted
time: 4ms
memory: 13108kb
input:
alfalfa 5 alfa 1 alfa 2 alfa 3 alfalfa 1 alfalfa 2
output:
1 4 -1 1 -1
result:
ok 5 lines
Test #7:
score: 0
Accepted
time: 3ms
memory: 12360kb
input:
x 28 a 1 b 1 c 1 d 1 e 1 f 1 g 1 h 1 i 1 j 1 k 1 l 1 m 1 n 1 o 1 p 1 q 1 r 1 s 1 t 1 u 1 v 1 w 1 x 1 y 1 z 1 abcdefghijklmnopqrstuvwxyz 1 abcdefghijklmnopqrstuvwxyz 1
output:
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 1 -1 -1 -1 -1
result:
ok 28 lines
Test #8:
score: 0
Accepted
time: 230ms
memory: 148856kb
input:
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 ...
result:
ok 200000 lines
Test #9:
score: 0
Accepted
time: 173ms
memory: 149104kb
input:
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
100001 -1
result:
ok 2 lines
Test #10:
score: 0
Accepted
time: 151ms
memory: 148864kb
input:
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
2
result:
ok single line: '2'
Test #11:
score: 0
Accepted
time: 175ms
memory: 149396kb
input:
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
1
result:
ok single line: '1'
Test #12:
score: 0
Accepted
time: 244ms
memory: 149016kb
input:
abbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababbabaababbaabbabaababbabaabbaababbaabbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababbaabbabaabbaababbabaababbaabbabaababbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababbabaababbaabbabaababbabaabbaababbaabbabaabbaab...
output:
1 1537 3073 5121 6145 7681 9217 10241 12289 13825 15361 17409 18433 20481 22017 23553 24577 26113 27649 29697 30721 32257 33793 34817 36865 38401 39937 40961 42497 44033 46081 47105 49153 50689 52225 54273 55297 56833 58369 59393 61441 62977 64513 66561 67585 69633 71169 72705 73729 75265 76801 7884...
result:
ok 195 lines