답안 #973185

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
973185 2024-05-01T15:38:28 Z duckindog Selling RNA Strands (JOI16_selling_rna) C++17
100 / 100
265 ms 411480 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 Node {
  array<Node*, 4> nwt;
  int ma, mi, sz;

  Node() : mi(1e9), ma(0), sz(0) {
    for (int i = 0; i < 4; ++i) nwt[i] = nullptr;
  }

  void update(int value, int pass) {
    ma = max(ma, value);
    mi = min(mi, value);
    sz += pass;
  }
};
 
struct Trie { 
  bool pass;
  Node* root;
 
  Trie() : root(new Node()) {}

  void dfs(Node* it) {
    if (!it) return;
    for (int i = 0; i < 4; ++i) {
      if (it->nwt[i]) dfs(it->nwt[i]);
    }

    delete it;
  }
 
  void clear() {
    dfs(root);
    root = new Node();
    pass = true;
  }
 
  void add(const auto& s, int value) {
    auto g = root;
    for (int i = 1; i <= s.size(); ++i) { 
      int path = convert(s[i - 1]);
      if (!g->nwt[path]) g->nwt[path] = new Node();

      g = g->nwt[path];
      g->update(value, pass);
    }
  }
 
  pair<int, int> que(const auto& s) { 
    auto g = root;
    for (int i = 1; i <= s.size(); ++i) { 
      int path = convert(s[i - 1]);
      if (!g->nwt[path]) return {0, 0};
      g = g->nwt[path];
    }
    return {pass ? g->sz : g->mi, g->ma};
  }
 
} 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: In constructor 'Node::Node()':
selling_rna.cpp:14:11: warning: 'Node::mi' will be initialized after [-Wreorder]
   14 |   int ma, mi, sz;
      |           ^~
selling_rna.cpp:14:7: warning:   'int Node::ma' [-Wreorder]
   14 |   int ma, mi, sz;
      |       ^~
selling_rna.cpp:16:3: warning:   when initialized here [-Wreorder]
   16 |   Node() : mi(1e9), ma(0), sz(0) {
      |   ^~~~
selling_rna.cpp: At global scope:
selling_rna.cpp:48:18: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
   48 |   void add(const auto& s, int value) {
      |                  ^~~~
selling_rna.cpp:59:28: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
   59 |   pair<int, int> que(const auto& s) {
      |                            ^~~~
selling_rna.cpp: In instantiation of 'void Trie::add(const auto:24&, int) [with auto:24 = std::__cxx11::basic_string<char>]':
selling_rna.cpp:82:47:   required from here
selling_rna.cpp:50:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   50 |     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:85:39:   required from here
selling_rna.cpp:61:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   61 |     for (int i = 1; i <= s.size(); ++i) {
      |                     ~~^~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 86 ms 282232 KB Output is correct
2 Correct 73 ms 282028 KB Output is correct
3 Correct 69 ms 282192 KB Output is correct
4 Correct 69 ms 282028 KB Output is correct
5 Correct 71 ms 282192 KB Output is correct
6 Correct 69 ms 282240 KB Output is correct
7 Correct 70 ms 282452 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 173 ms 409500 KB Output is correct
2 Correct 181 ms 406720 KB Output is correct
3 Correct 265 ms 411480 KB Output is correct
4 Correct 225 ms 405336 KB Output is correct
5 Correct 217 ms 363984 KB Output is correct
6 Correct 212 ms 364880 KB Output is correct
7 Correct 96 ms 293628 KB Output is correct
8 Correct 164 ms 338648 KB Output is correct
9 Correct 158 ms 331604 KB Output is correct
10 Correct 134 ms 326060 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 78 ms 283280 KB Output is correct
2 Correct 76 ms 283236 KB Output is correct
3 Correct 82 ms 283536 KB Output is correct
4 Correct 81 ms 283196 KB Output is correct
5 Correct 75 ms 283472 KB Output is correct
6 Correct 83 ms 283476 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 86 ms 282232 KB Output is correct
2 Correct 73 ms 282028 KB Output is correct
3 Correct 69 ms 282192 KB Output is correct
4 Correct 69 ms 282028 KB Output is correct
5 Correct 71 ms 282192 KB Output is correct
6 Correct 69 ms 282240 KB Output is correct
7 Correct 70 ms 282452 KB Output is correct
8 Correct 173 ms 409500 KB Output is correct
9 Correct 181 ms 406720 KB Output is correct
10 Correct 265 ms 411480 KB Output is correct
11 Correct 225 ms 405336 KB Output is correct
12 Correct 217 ms 363984 KB Output is correct
13 Correct 212 ms 364880 KB Output is correct
14 Correct 96 ms 293628 KB Output is correct
15 Correct 164 ms 338648 KB Output is correct
16 Correct 158 ms 331604 KB Output is correct
17 Correct 134 ms 326060 KB Output is correct
18 Correct 78 ms 283280 KB Output is correct
19 Correct 76 ms 283236 KB Output is correct
20 Correct 82 ms 283536 KB Output is correct
21 Correct 81 ms 283196 KB Output is correct
22 Correct 75 ms 283472 KB Output is correct
23 Correct 83 ms 283476 KB Output is correct
24 Correct 170 ms 390992 KB Output is correct
25 Correct 173 ms 391992 KB Output is correct
26 Correct 159 ms 389440 KB Output is correct
27 Correct 225 ms 389568 KB Output is correct
28 Correct 152 ms 306508 KB Output is correct
29 Correct 111 ms 287216 KB Output is correct