QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#346299#7506. StrCartesianwsyearTL 11209ms233384kbC++146.4kb2024-03-08 10:31:312024-03-08 10:31:31

Judging History

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

  • [2024-03-08 10:31:31]
  • 评测
  • 测评结果:TL
  • 用时:11209ms
  • 内存:233384kb
  • [2024-03-08 10:31:31]
  • 提交

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(1);

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]) {
        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;
      }
      // cerr << "cxk: " << cxk.x << " " << cxk.y << ": " << sm << 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: 0ms
memory: 30220kb

input:

2 3
a
ab
a
aa
ba
2
3
4

output:

1 3
2 1

result:

ok Correct Answer

Test #2:

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

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: 11209ms
memory: 233384kb

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: -100
Time Limit Exceeded

input:

42793 43577
osjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjyosjxosjy...

output:


result: