답안 #498527

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

using namespace std;

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

int aib[L];

void add(int i, int x) {
  while (i < L) {
    aib[i] += x;
    i += i & (-i);
  }
}

int get(int i) {
  int sol = 0;
  while (i) {
    sol += aib[i];
    i -= i & (-i);
  }
  return sol;
}

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;
};

struct Question {
  int x;
  int y;
  int coef;
  int index;
};

bool operator < (Question a, Question b) {
  return a.x < b.x;
}

bool cmp(int i, int j) {
  return x[i] < x[j];
}

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;

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

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

  sort(ord.begin(), ord.end(), cmp);

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


  vector<Question> questions;

  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 x1 = xx.first, x2 = xx.second;
    int y1 = yy.first, y2 = yy.second;

    questions.push_back({x2, y2, +1, iq});
    questions.push_back({x2, y1 - 1, -1, iq});
    questions.push_back({x1 - 1, y2, -1, iq});
    questions.push_back({x1 - 1, y1 - 1, +1, iq});
  }

  sort(questions.begin(), questions.end());
  sort(ord.begin(), ord.end(), cmp);

  int it = 0;

  for (auto &question : questions) {
    while (it < n && x[ord[it]] <= question.x) {
      add(y[ord[it]], +1);
      it++;
    }
    int sol =  get(question.y);
    sol *= question.coef;
    solPrint[question.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:126:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  126 |     freopen ("input", "r", stdin);
      |     ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 186 ms 162196 KB Output is correct
2 Correct 177 ms 154536 KB Output is correct
3 Correct 171 ms 153496 KB Output is correct
4 Correct 168 ms 146068 KB Output is correct
5 Correct 264 ms 196284 KB Output is correct
6 Correct 233 ms 199208 KB Output is correct
7 Correct 36 ms 1508 KB Output is correct
8 Correct 156 ms 115624 KB Output is correct
9 Correct 141 ms 97096 KB Output is correct
10 Correct 100 ms 94236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 5628 KB Output is correct
2 Correct 23 ms 4028 KB Output is correct
3 Correct 29 ms 6144 KB Output is correct
4 Correct 29 ms 5712 KB Output is correct
5 Correct 24 ms 4024 KB Output is correct
6 Correct 34 ms 6168 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 186 ms 162196 KB Output is correct
9 Correct 177 ms 154536 KB Output is correct
10 Correct 171 ms 153496 KB Output is correct
11 Correct 168 ms 146068 KB Output is correct
12 Correct 264 ms 196284 KB Output is correct
13 Correct 233 ms 199208 KB Output is correct
14 Correct 36 ms 1508 KB Output is correct
15 Correct 156 ms 115624 KB Output is correct
16 Correct 141 ms 97096 KB Output is correct
17 Correct 100 ms 94236 KB Output is correct
18 Correct 32 ms 5628 KB Output is correct
19 Correct 23 ms 4028 KB Output is correct
20 Correct 29 ms 6144 KB Output is correct
21 Correct 29 ms 5712 KB Output is correct
22 Correct 24 ms 4024 KB Output is correct
23 Correct 34 ms 6168 KB Output is correct
24 Correct 163 ms 138588 KB Output is correct
25 Correct 199 ms 140740 KB Output is correct
26 Correct 160 ms 136248 KB Output is correct
27 Correct 203 ms 131252 KB Output is correct
28 Correct 141 ms 31804 KB Output is correct
29 Correct 87 ms 13504 KB Output is correct