답안 #498523

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
498523 2021-12-25T11:34:27 Z 600Mihnea Selling RNA Strands (JOI16_selling_rna) C++17
35 / 100
1500 ms 196900 KB
#include <bits/stdc++.h>

using namespace std;

const int N = (int) 1e5 + 7;

int decode(char ch) {
  if (ch == 'A') return 0;
  if (ch == 'G') return 1;
  if (ch == 'C') return 2;
  if (ch == 'U') return 3;
  assert(false);
}

struct TrieNode {
  TrieNode* kids[4];
  int first;
  int last;
  vector<int> guys;

  TrieNode() {
    for (int i = 0; i < 4; i++) {
      kids[i] = nullptr;
    }
  }
};

void addToTrie(TrieNode* root, string s, int index) {
  TrieNode* current = root;
  for (auto &ch : s) {
    int decoded = decode(ch);
    if (!current->kids[decoded]) {
      current->kids[decoded] = new TrieNode;
    }
    current = current->kids[decoded];
  }
  current->guys.push_back(index);
}


void prepTrie(TrieNode* root, int inverse[]) {
  int index = 0;
  function<void(TrieNode*)> dfs = [&] (TrieNode* current) {
    current->first = ++index;
    for (auto &i : current->guys) {
      inverse[i] = index;
    }
    for (int j = 0; j < 4; j++) {
      if (current->kids[j]) {
        dfs(current->kids[j]);
      }
    }
    current->last = index;
  };
  dfs(root);
}

pair<int, int> getInterval(TrieNode* root, string s) {
  TrieNode* current = root;
  for (auto &ch : s) {
    int decoded = decode(ch);
    if (!current->kids[decoded]) {
      return {1, 0};
    }
    current = current->kids[decoded];
  }
  return {current->first, current->last};
}

TrieNode* root1 = new TrieNode;
TrieNode* root2 = new TrieNode;
int x[N];
int y[N];

struct BBox {
  int xmin;
  int ymin;
  int xmax;
  int ymax;
  int index;
};

int solPrint[N];

int main() {
  bool isHome;

  isHome = true;
  isHome = false;

  if (isHome) {
    freopen ("input", "r", stdin);
  } else {
    ios::sync_with_stdio(0); cin.tie(0);
  }

  int n, q;
  cin >> n >> q;
  for (int i = 1; i <= n; i++) {
    string s, revS;
    cin >> s;
    revS = s;
    reverse(revS.begin(), revS.end());

    addToTrie(root1, s, i);
    addToTrie(root2, revS, i);
  }

  prepTrie(root1, x);
  prepTrie(root2, y);


  vector<BBox> bboxes;

  for (int iq = 1; iq <= q; iq++) {
    string pref, suf;
    cin >> pref >> suf;

    reverse(suf.begin(), suf.end());

    auto xx = getInterval(root1, pref);
    auto yy = getInterval(root2, suf);


    if (xx.first > xx.second || yy.first > yy.second) {
      continue;
    }

    int xmin = xx.first, xmax = xx.second;
    int ymin = yy.first, ymax = yy.second;

    bboxes.push_back({xmin, ymin, xmax, ymax, iq});
  }

  /// sort(bboxes.begin(), bboxes.end());
  for (auto &bbox : bboxes) {
    int xmin = bbox.xmin, ymin = bbox.ymin;
    int xmax = bbox.xmax, ymax = bbox.ymax;
    int sol = 0;
    for (int i = 1; i <= n; i++) {
      sol += (xmin <= x[i] && x[i] <= xmax && ymin <= y[i] && y[i] <= ymax);
    }
    solPrint[bbox.index] = sol;
  }


  for (int iq = 1; iq <= q; iq++) {
    cout << solPrint[iq] << "\n";
  }


  return 0;
}

Compilation message

selling_rna.cpp: In function 'int main()':
selling_rna.cpp:92:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   92 |     freopen ("input", "r", stdin);
      |     ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 320 KB Output is correct
5 Correct 1 ms 320 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 324 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 188 ms 159072 KB Output is correct
2 Correct 292 ms 151040 KB Output is correct
3 Correct 229 ms 157040 KB Output is correct
4 Correct 264 ms 149776 KB Output is correct
5 Correct 296 ms 193760 KB Output is correct
6 Correct 292 ms 196900 KB Output is correct
7 Correct 44 ms 6532 KB Output is correct
8 Correct 215 ms 118444 KB Output is correct
9 Correct 210 ms 100408 KB Output is correct
10 Correct 124 ms 95108 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1571 ms 3040 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 320 KB Output is correct
5 Correct 1 ms 320 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 324 KB Output is correct
8 Correct 188 ms 159072 KB Output is correct
9 Correct 292 ms 151040 KB Output is correct
10 Correct 229 ms 157040 KB Output is correct
11 Correct 264 ms 149776 KB Output is correct
12 Correct 296 ms 193760 KB Output is correct
13 Correct 292 ms 196900 KB Output is correct
14 Correct 44 ms 6532 KB Output is correct
15 Correct 215 ms 118444 KB Output is correct
16 Correct 210 ms 100408 KB Output is correct
17 Correct 124 ms 95108 KB Output is correct
18 Execution timed out 1571 ms 3040 KB Time limit exceeded
19 Halted 0 ms 0 KB -