답안 #973091

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
973091 2024-05-01T13:38:28 Z duckindog Selling RNA Strands (JOI16_selling_rna) C++17
100 / 100
204 ms 345936 KB
#include <bits/stdc++.h>
 
using namespace std;
 
const int N = 2'000'000 + 10;
int n, m;
string str[N];
string s[N], e[N];
 
int convert(const auto& a) { return a == 'A' ? 0 : a == 'C' ? 1 : a == 'G' ? 2 : 3; }

struct Trie { 
  bool pass;
  int nwt[N][4], ma[N], mi[N], sz[N], it;

  Trie() {
    memset(mi, 63, sizeof mi);
  }

  void clear() { 
    memset(nwt, 0, sizeof nwt);
    memset(ma, 0, sizeof ma);
    memset(mi, 0, sizeof mi);
    it = 0;
    pass = true;
  }

  void add(const auto& s, const int& node) {
    int g = 0;
    for (int i = 1; i <= s.size(); ++i) { 
      int path = convert(s[i - 1]);
      if (!nwt[g][path]) nwt[g][path] = ++it;
      g = nwt[g][path];

      ma[g] = max(ma[g], node);
      mi[g] = min(mi[g], node);
      sz[g] += pass;
    }
  }

  pair<int, int> que(const auto& s) { 
    int g = 0;
    for (int i = 1; i <= s.size(); ++i) { 
      int path = convert(s[i - 1]);
      if (!nwt[g][path]) return {0, 0};
      g = nwt[g][path];
    }
    return {pass ? sz[g] : mi[g], ma[g]};
  }

} T;

vector<int> l[N], r[N];

int32_t main() { 
  cin.tie(0)->sync_with_stdio(0);
 
  cin >> n >> m;
 
  for (int i = 1; i <= n; ++i) cin >> str[i];
  for (int i = 1; i <= m; ++i) cin >> s[i] >> e[i];

  sort(str + 1, str + n + 1);
  for (int i = 1; i <= n; ++i) T.add(str[i], i);
  for (int i = 1; i <= m; ++i) {
    reverse(e[i].begin(), e[i].end());
    const auto& [st, ed] = (T.que(s[i]));
    if (st) l[st - 1].push_back(i);
    r[ed].push_back(i);
  }
  
  T.clear();

  vector<int> answer(m + 1);
  for (int i = 1; i <= n; ++i) {
    T.add(string(str[i].rbegin(), str[i].rend()), i);
    for (const auto& j : l[i]) answer[j] -= T.que(e[j]).first;
    for (const auto& j : r[i]) answer[j] += T.que(e[j]).first;
  }

  for (int i = 1; i <= m; ++i) cout << answer[i] << "\n";
}

Compilation message

selling_rna.cpp:10:19: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
   10 | int convert(const auto& a) { return a == 'A' ? 0 : a == 'C' ? 1 : a == 'G' ? 2 : 3; }
      |                   ^~~~
selling_rna.cpp:28:18: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
   28 |   void add(const auto& s, const int& node) {
      |                  ^~~~
selling_rna.cpp:41:28: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
   41 |   pair<int, int> que(const auto& s) {
      |                            ^~~~
selling_rna.cpp: In instantiation of 'void Trie::add(const auto:24&, const int&) [with auto:24 = std::__cxx11::basic_string<char>]':
selling_rna.cpp:64:47:   required from here
selling_rna.cpp:30:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   30 |     for (int i = 1; i <= s.size(); ++i) {
      |                     ~~^~~~~~~~~~~
selling_rna.cpp: In instantiation of 'std::pair<int, int> Trie::que(const auto:25&) [with auto:25 = std::__cxx11::basic_string<char>]':
selling_rna.cpp:67:39:   required from here
selling_rna.cpp:43:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   43 |     for (int i = 1; i <= s.size(); ++i) {
      |                     ~~^~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 204 ms 330716 KB Output is correct
2 Correct 86 ms 330868 KB Output is correct
3 Correct 85 ms 330740 KB Output is correct
4 Correct 86 ms 330868 KB Output is correct
5 Correct 85 ms 330832 KB Output is correct
6 Correct 84 ms 330836 KB Output is correct
7 Correct 86 ms 330836 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 123 ms 341072 KB Output is correct
2 Correct 118 ms 345136 KB Output is correct
3 Correct 126 ms 345112 KB Output is correct
4 Correct 127 ms 345200 KB Output is correct
5 Correct 121 ms 340124 KB Output is correct
6 Correct 129 ms 340508 KB Output is correct
7 Correct 119 ms 342080 KB Output is correct
8 Correct 138 ms 345008 KB Output is correct
9 Correct 134 ms 344372 KB Output is correct
10 Correct 116 ms 339464 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 102 ms 331860 KB Output is correct
2 Correct 96 ms 331332 KB Output is correct
3 Correct 103 ms 331836 KB Output is correct
4 Correct 97 ms 331488 KB Output is correct
5 Correct 97 ms 331432 KB Output is correct
6 Correct 99 ms 331600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 204 ms 330716 KB Output is correct
2 Correct 86 ms 330868 KB Output is correct
3 Correct 85 ms 330740 KB Output is correct
4 Correct 86 ms 330868 KB Output is correct
5 Correct 85 ms 330832 KB Output is correct
6 Correct 84 ms 330836 KB Output is correct
7 Correct 86 ms 330836 KB Output is correct
8 Correct 123 ms 341072 KB Output is correct
9 Correct 118 ms 345136 KB Output is correct
10 Correct 126 ms 345112 KB Output is correct
11 Correct 127 ms 345200 KB Output is correct
12 Correct 121 ms 340124 KB Output is correct
13 Correct 129 ms 340508 KB Output is correct
14 Correct 119 ms 342080 KB Output is correct
15 Correct 138 ms 345008 KB Output is correct
16 Correct 134 ms 344372 KB Output is correct
17 Correct 116 ms 339464 KB Output is correct
18 Correct 102 ms 331860 KB Output is correct
19 Correct 96 ms 331332 KB Output is correct
20 Correct 103 ms 331836 KB Output is correct
21 Correct 97 ms 331488 KB Output is correct
22 Correct 97 ms 331432 KB Output is correct
23 Correct 99 ms 331600 KB Output is correct
24 Correct 127 ms 344872 KB Output is correct
25 Correct 132 ms 345936 KB Output is correct
26 Correct 123 ms 344820 KB Output is correct
27 Correct 130 ms 344940 KB Output is correct
28 Correct 171 ms 342812 KB Output is correct
29 Correct 141 ms 335844 KB Output is correct