QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#346311#7506. StrCartesianwsyearAC ✓9296ms235568kbC++146.8kb2024-03-08 10:43:182024-03-08 10:43:19

Judging History

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

  • [2024-03-08 10:43:19]
  • 评测
  • 测评结果:AC
  • 用时:9296ms
  • 内存:235568kb
  • [2024-03-08 10:43:18]
  • 提交

answer

#include <bits/stdc++.h>
#ifdef dbg
#define D(...) fprintf(stderr, __VA_ARGS__)
#define DD(...) D("[Debug] "#__VA_ARGS__ " = "), \
              debug_helper::debug(__VA_ARGS__), D("\n")
#include "C:\Users\wsyear\Desktop\OI\templates\debug.hpp"
#else
#define D(...) ((void)0)
#define DD(...) ((void)0)
#endif
#define rep(i, j, k) for (int i = (j); i <= (k); ++i)
#define per(i, j, k) for (int i = (j); i >= (k); --i)
#define SZ(v) int((v).size())
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second
using ll = long long;
using pii = std::pair<int, int>;
using pll = std::pair<ll, ll>;

template<class T> void chkmn(T &x, T y) { if (y < x) x = y; }
template<class T> void chkmx(T &x, T y) { if (y > x) x = y; }

using namespace std;

namespace SA {
  static const int maxn = 2100010;
  // init: len = m, |char| = sigma, string = a[]
  int m, sigma, a[maxn], rnk[maxn], sa[maxn], h[maxn], cnt[maxn], p[maxn];
  
  void SA_sort() {
    rep (i, 1, sigma) cnt[i] = 0;
    rep (i, 1, m) cnt[rnk[i]] += 1;
    rep (i, 1, sigma) cnt[i] += cnt[i - 1];
    per (i, m, 1) sa[cnt[rnk[p[i]]]--] = p[i];
  }
  
  void Get_SA() {
    rep (i, 1, m) rnk[i] = a[i], p[i] = i;
    SA_sort();
    for (int len = 1, tot = 0; tot < m; sigma = tot, len <<= 1) {
      tot = 0;
      per (i, m, m - len + 1) p[++tot] = i;
      rep (i, 1, m) if (sa[i] > len) p[++tot] = sa[i] - len;
      SA_sort();
      rep (i, 1, m) p[i] = rnk[i];
      rnk[sa[1]] = tot = 1;
      rep (i, 2, m) {
        if (p[sa[i - 1]] == p[sa[i]] &&
          p[sa[i - 1] + len] == p[sa[i] + len]) rnk[sa[i]] = tot;
        else rnk[sa[i]] = ++tot;
      }
    }
    rep (i, 1, m) rnk[sa[i]] = i;
  }
  
  void Get_Height() {
    int k = 0;
    rep (i, 1, m) {
      if (rnk[i] == 1) continue;
      k = max(k - 1, 0);
      int j = sa[rnk[i] - 1];
      while (max(i, j) + k <= m && a[i + k] == a[j + k]) k++;
      h[rnk[i]] = k;
    }
  }
}

const int maxn = 2100010;
mt19937 rnd(1145141);

struct node {
  int pos, len, id;
} a[maxn], b[maxn];

int n, m, q, tot, st[maxn][22], L[maxn], R[maxn], p1[maxn], p2[maxn];
char t[maxn];

inline int LCP(int x, int y) {
  x = SA::rnk[x], y = SA::rnk[y];
  if (x > y) swap(x, y);
  if (x == y) return 1e9;
  int k = __lg(y - x);
  return min(st[x + 1][k], st[y - (1 << k) + 1][k]);
}

bool cmp(int s1, int l1, int s2, int l2) {
  int lcp = LCP(s1, s2);
  if (lcp < min(l1, l2)) return SA::a[s1 + lcp] < SA::a[s2 + lcp];
  return l1 < l2;
}

struct str {
  int x, y;
  str() { x = y = 0; }
  str(int a, int b) { x = a, y = b; }
  friend bool operator<(str x, str y) {
    int a1 = x.x, a2 = x.y, b1 = y.x, b2 = y.y;
    int lcp = LCP(a[a1].pos, a[b1].pos);
    if (lcp < min(a[a1].len, a[b1].len)) {
      return SA::a[a[a1].pos + lcp] < SA::a[a[b1].pos + lcp];
    }
    if (a[a1].len == a[b1].len) {
      lcp = LCP(b[a2].pos, b[b2].pos);
      if (lcp < min(b[a2].len, b[b2].len)) {
        return SA::a[b[a2].pos + lcp] < SA::a[b[b2].pos + lcp];
      }
      return b[a2].len < b[b2].len;
    } else if (a[a1].len < a[b1].len) {
      lcp = LCP(b[a2].pos, a[b1].pos + a[a1].len);
      if (lcp < min(b[a2].len, a[b1].len - a[a1].len)) {
        return SA::a[b[a2].pos + lcp] < SA::a[a[b1].pos + a[a1].len + lcp];
      }
      if (lcp == b[a2].len) return 1;
      return cmp(b[a2].pos + lcp, b[a2].len - lcp, b[b2].pos, b[b2].len);
    } else {
      lcp = LCP(a[a1].pos + a[b1].len, b[b2].pos);
      if (lcp < min(a[a1].len - a[b1].len, b[b2].len)) {
        return SA::a[a[a1].pos + a[b1].len + lcp] < SA::a[b[b2].pos + lcp];
      }
      if (lcp == b[b2].len) return 0;
      return cmp(b[a2].pos, b[a2].len, b[b2].pos + lcp, b[b2].len - lcp);
    }
  }
};

int main() {
  cin.tie(nullptr) -> ios::sync_with_stdio(false);
  cin >> n >> m;
  rep (i, 1, n) {
    cin >> (t + 1), a[i].len = strlen(t + 1), a[i].pos = tot + 1;
    rep (j, 1, a[i].len) SA::a[++tot] = t[j] - 'a' + n + m + 1;
    SA::a[++tot] = i, a[i].id = i;
  }
  rep (i, 1, m) {
    cin >> (t + 1), b[i].len = strlen(t + 1), b[i].pos = tot + 1;
    rep (j, 1, b[i].len) SA::a[++tot] = t[j] - 'a' + n + m + 1;
    SA::a[++tot] = n + i, b[i].id = i;
  }
  SA::m = tot, SA::sigma = n + m + 26, SA::Get_SA(), SA::Get_Height();
  rep (i, 1, tot) st[i][0] = SA::h[i];
  rep (j, 1, 21) rep (i, 1, tot - (1 << j) + 1)
    st[i][j] = min(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
  sort(a + 1, a + n + 1, [&](node x, node y) { return SA::rnk[x.pos] < SA::rnk[y.pos]; });
  sort(b + 1, b + m + 1, [&](node x, node y) { return SA::rnk[x.pos] < SA::rnk[y.pos]; });
  // rep (i, 1, n) cerr << a[i].id << " ";
  // cerr << endl;
  // rep (i, 1, m) cerr << b[i].id << " ";
  // cerr << endl;
  cin >> q;
  while (q--) {
    ll k;
    cin >> k;
    rep (i, 1, n) L[i] = 1, R[i] = m;
    while (true) {
      // cerr << "tmp: " << k << endl;
      // rep (i, 1, n) cerr << L[i] << " " << R[i] << endl;
      ll cnt = 0;
      rep (i, 1, n) if (L[i]) cnt += R[i] - L[i] + 1;
      ll kth = uniform_int_distribution<ll>(1, cnt)(rnd);
      str cxk = str();
      rep (i, 1, n) if (L[i]) {
        if (R[i] - L[i] + 1 >= kth) {
          cxk = str(i, L[i] + kth - 1);
          break;
        }
        kth -= R[i] - L[i] + 1;
      }
      assert(cxk.x);
      ll sm = 0, seq = 0;
      rep (i, 1, n) if (L[i]) {
        if (i == cxk.x) {
          sm += cxk.y - L[i];
          seq += cxk.y - L[i] + 1;
          p1[i] = cxk.y - 1, p2[i] = cxk.y;
        } else if (LCP(a[i].pos, a[cxk.x].pos) == min(a[i].len, a[cxk.x].len)) {
          int l = L[i], r = R[i], res = L[i] - 1;
          while (l <= r) {
            int mid = (l + r) >> 1;
            if (str(i, mid) < cxk) res = mid, l = mid + 1;
            else r = mid - 1;
          }
          sm += res - L[i] + 1, p1[i] = res;
          l = L[i], r = R[i], res = L[i] - 1;
          while (l <= r) {
            int mid = (l + r) >> 1;
            if (!(cxk < str(i, mid))) res = mid, l = mid + 1;
            else r = mid - 1;
          }
          seq += res - L[i] + 1, p2[i] = res;
        } else if (i < cxk.x) {
          sm += R[i] - L[i] + 1;
          seq += R[i] - L[i] + 1;
          p1[i] = p2[i] = R[i];
        } else {
          p1[i] = p2[i] = L[i] - 1;
        }
      }
      // cerr << "cxk: " << cxk.x << " " << cxk.y << ": " << sm << " " << seq << endl;
      if (sm < k && seq >= k) {
        // cerr << "OK" << endl;
        cout << a[cxk.x].id << ' ' << b[cxk.y].id << '\n';
        break;
      }
      if (sm >= k) {
        rep (i, 1, n) if (L[i]) {
          R[i] = p1[i];
          if (L[i] > R[i]) L[i] = R[i] = 0;
        }
      } else {
        k -= seq;
        rep (i, 1, n) if (L[i]) {
          L[i] = p2[i] + 1;
          if (L[i] > R[i]) L[i] = R[i] = 0;
        }
      }
    }
  }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 13868kb

input:

2 3
a
ab
a
aa
ba
2
3
4

output:

2 1
1 3

result:

ok Correct Answer

Test #2:

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

input:

27 28
ptdtljwzjbyctrjetnossaiogcfce
kmuwnsxjenpjybcwrecoqddbltzvnkzzrxvne
lsbqkpbgihqahpotzrnggqrompohf
ixonxrpixmurqgdtedhavpawmddfw
ruhqnxhvucv
fvarlrf
uyxxjzjf
tlzrxaykw
kldatopsv
fywmszxti
yraazxvbfebdhekphdrqhvpmferaghezebm
dovtqdvdd
waohxsasgl
biakvxhqx
aeyzaqum
repmbqtrvwd
dvqojkduc
sjxapwnyz...

output:

23 1
2 27
4 11
2 11
21 9
11 13
7 6
27 1
5 26
17 3
21 23
1 10
6 25
13 5
17 20
22 22
1 5
23 21
10 23
3 18
10 14
16 14
18 5
22 26
14 6
12 22
19 17
14 17
23 28
10 20
26 15
27 6
19 28
16 27
21 25
16 1
6 8
10 20
12 19
1 10
17 10
14 14
18 19
15 27
10 9
9 27
4 19
8 1
17 7
21 1
17 26
26 13
22 13
27 7
2 28
15...

result:

ok Correct Answer

Test #3:

score: 0
Accepted
time: 4612ms
memory: 232368kb

input:

21673 22789
osjxapwnyzosjxapwnyaosjxapwnyzosjxapwnyaosjxapwnyzosjxapwnyaosjxapwnyzosjxapwnyaosjxapwnyzosjxapwnyaosjxapwnyzosjxapwnyaosjxapwnyzosjxapwnyaosjxapwnyzosjxapwnyaosjxapwnyzosjxapwnyaosjxapwnyzosjxapwnyaosjxapwnyzosjxapwnyaosjxapwnyzosjxapwnyaosjxapwnyzosjxapwnyaosjxapwnyzosjxapwnyaosjxapwn...

output:

11748 7566
13026 16461
9916 11268
12937 6138
20884 12212
310 19018
14665 17118
7698 15274
18832 22061
7551 10949
12970 7192
1098 9710
19691 12422
5351 11239
14696 2061
2889 12319
8909 10800
6739 1691
19504 5705
599 16293
2991 6114
20067 16922
11544 17617
7562 4195
7364 20824
19750 22736
15334 22205
...

result:

ok Correct Answer

Test #4:

score: 0
Accepted
time: 9296ms
memory: 234616kb

input:

42793 43577
osjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjy...

output:

478 23079
19578 5700
32327 37245
23174 34890
8775 43551
33235 7846
40699 41435
24607 32335
10681 5081
40664 38308
4711 11678
31506 13791
13592 7796
9099 15409
35 36153
12575 30943
1418 37944
18258 11644
4315 19248
29431 9382
9938 30089
16317 36775
25241 10476
27724 29981
33560 26458
311 26256
24402 ...

result:

ok Correct Answer

Test #5:

score: 0
Accepted
time: 4292ms
memory: 225288kb

input:

21455 22492
osjxaposjxaqosjxaposjxaqosjxaposjxaqosjxaposjxaqosjxaposjxaqosjxaposjxaqosjxaposjxaqosjxaposjxaqosjxaposjxaqosjxaposjxaqosjxaposjxaqosjxaposjxaqosjxaposjxaqosjxaposjxaqosjxaposjxaqosjxaposjxaqosjxaposjxaqosjxaposjxaqosjxaposjxaqosjxaposjxaqosjxaposjxaqosjxaposjxaqosjxaposjxaqosjxaposjxaq...

output:

1153 18391
12397 8491
9381 14158
8926 9710
19054 21286
18999 489
2337 3914
20214 18000
4041 11022
3232 2622
632 12989
4831 1494
7204 17465
7959 13995
4214 14108
14052 20985
1761 21505
3079 11058
16940 15409
4088 14365
14018 20169
19551 9309
16472 21758
13613 21356
6865 9708
11938 3822
14038 1038
370...

result:

ok Correct Answer

Test #6:

score: 0
Accepted
time: 7026ms
memory: 235568kb

input:

33056 33045
yiyrokayfnzhamwfzuadvuyndjspptokvoqlgrgqytwceezsczyljaxyicjepttyvllsbqcfmsseskryayuezexpiypfgswfvzexhocjyzpxuazgrdjngsovhcrkdoirnxfxwjypnamxblzrqbgoomgltuoxkgtdnjsanhszewdkbediutypzjcasccewjnxakdeinqczivdzoecriaabjfuipztnvlapnqrsnqoigciixjrpgjxmtacshfrkeflmxjzmuwmkgpcsgqcgtlyqgugtdyoasns...

output:

13792 9065
8306 26578
7635 30555
18164 11919
11011 8423
25829 23538
30170 28490
19726 650
19941 2741
4050 1943
25996 22793
29283 32610
4160 4087
4759 31867
6046 6176
23450 14827
21825 16147
7681 18029
4173 15086
6943 7840
2462 27663
6954 18606
17654 23682
14061 2338
30610 1881
8076 25979
18709 32844...

result:

ok Correct Answer