답안 #295194

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
295194 2020-09-09T14:15:15 Z arman_ferdous Selling RNA Strands (JOI16_selling_rna) C++17
100 / 100
232 ms 153288 KB
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define pb push_back
#define sz(v) (int)v.size()
#define all(v) v.begin(),v.end()
#define dbg(x) cerr << #x << " is " << x << "\n";

using ll = long long;
using ii = pair<ll,ll>;

// misread 2e6 as 2e5 -> resulted in getting RTE
const int N = 1e5+10;
const int M = 2e6+10;

int n, m;
char s[N];

char ID(char x) {
  if(x == 'A') return 'a';
  if(x == 'C') return 'b';
  if(x == 'G') return 'c';
  return 'd';
}

struct Trie{
  int node, to[4][M];
  vector<int> ending[M];

  Trie() { node = 1; }

  void insert(int len, int id) {
    int cur = 1;
    for(int i = 0; i < len; i++) {
      int val = ID(s[i]) - 'a';
      if(to[val][cur] == 0) to[val][cur] = ++node;
      assert(node < M);
      cur = to[val][cur];
    }
    ending[cur].push_back(id);
  }

  int in[M], out[M];
  vector<int> eul;
  void dfs(int u) {
    in[u] = eul.size();
    for(int x : ending[u]) eul.push_back(x);
    for(int v = 0; v < 4; v++) if(to[v][u] != 0)
      dfs(to[v][u]);
    out[u] = eul.size() - 1;
  }

  void find(int &l, int &r) {
    int cur = 1, len = strlen(s);
    for(int i = 0; i < len; i++) {
      int val = ID(s[i]) - 'a';
      if(to[val][cur] == 0) {
        l = r = -1;
        return;
      } cur = to[val][cur];
    }
    l = in[cur], r = out[cur];
  }
}trie, eirt;

int result[N];
struct CPR{
  struct data{
    int x, y1, y2;
    int ty, id;
    // point = 0, [ = -1, ] = 1
    bool operator<(data o) const {
      if(x == o.x) return ty < o.ty;
      return x < o.x;
    }
  };
  vector<data> v;

  void insert_point(int x, int y) {
    x++, y++;
    v.push_back({x, y, 0, 0, 0});
  }
  void insert_rect(int x1, int x2, int y1, int y2, int id) {
    x1++, y1++, x2++, y2++;
    v.push_back({x1, y1, y2, -1, id});
    v.push_back({x2, y1, y2, +1, id});
  }

  int bit[N];
  void upd(int pos, int x) {
    while(pos < N) bit[pos] += x, pos += pos&-pos;
  }
  int get(int pos, int r = 0) {
    while(pos > 0) r += bit[pos], pos -= pos&-pos;
    return r;
  }

  void sweep() {
    sort(all(v));
    for(auto d : v) {
      if(d.ty == 0) upd(d.y1, +1);
      else result[d.id] += d.ty * (get(d.y2) - get(d.y1 - 1));
    }
  }
}ds;

int mp[M];

int main() {
  int sum = 0;
  scanf("%d %d", &n, &m);
  for(int i = 0; i < n; i++) {
    scanf(" %s", s);
    int len = strlen(s);
    trie.insert(len, i);
    sum += len;
    reverse(s, s + len);
    eirt.insert(len, i);
  }
  trie.dfs(1); eirt.dfs(1);
  for(int i = 0; i < n; i++) mp[trie.eul[i]] = i;
  for(int i = 0; i < n; i++) {
    eirt.eul[i] = mp[eirt.eul[i]];
    ds.insert_point(i, eirt.eul[i]);
  }
  for(int i = 0; i < m; i++) {
    scanf(" %s", s);
    int l, r; trie.find(l, r);

    scanf(" %s", s);
    reverse(s, s + strlen(s));
    int L, R; eirt.find(L, R);

    if(l != -1 && L != -1 && l <= r && L <= R) ds.insert_rect(L, R, l, r, i);
  }
  ds.sweep();
  for(int i = 0; i < m; i++)
    printf("%d\n", result[i]);
  return 0;
}

Compilation message

selling_rna.cpp: In function 'int main()':
selling_rna.cpp:113:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  113 |   scanf("%d %d", &n, &m);
      |   ~~~~~^~~~~~~~~~~~~~~~~
selling_rna.cpp:115:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  115 |     scanf(" %s", s);
      |     ~~~~~^~~~~~~~~~
selling_rna.cpp:129:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  129 |     scanf(" %s", s);
      |     ~~~~~^~~~~~~~~~
selling_rna.cpp:132:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  132 |     scanf(" %s", s);
      |     ~~~~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 62 ms 94456 KB Output is correct
2 Correct 61 ms 94460 KB Output is correct
3 Correct 63 ms 94456 KB Output is correct
4 Correct 61 ms 94456 KB Output is correct
5 Correct 61 ms 94584 KB Output is correct
6 Correct 61 ms 94588 KB Output is correct
7 Correct 60 ms 94456 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 190 ms 141492 KB Output is correct
2 Correct 181 ms 139116 KB Output is correct
3 Correct 190 ms 140900 KB Output is correct
4 Correct 183 ms 138824 KB Output is correct
5 Correct 230 ms 152648 KB Output is correct
6 Correct 232 ms 153288 KB Output is correct
7 Correct 109 ms 95080 KB Output is correct
8 Correct 166 ms 116168 KB Output is correct
9 Correct 180 ms 118636 KB Output is correct
10 Correct 135 ms 116976 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 104 ms 100676 KB Output is correct
2 Correct 88 ms 97848 KB Output is correct
3 Correct 94 ms 98340 KB Output is correct
4 Correct 92 ms 97832 KB Output is correct
5 Correct 91 ms 97936 KB Output is correct
6 Correct 97 ms 98372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 62 ms 94456 KB Output is correct
2 Correct 61 ms 94460 KB Output is correct
3 Correct 63 ms 94456 KB Output is correct
4 Correct 61 ms 94456 KB Output is correct
5 Correct 61 ms 94584 KB Output is correct
6 Correct 61 ms 94588 KB Output is correct
7 Correct 60 ms 94456 KB Output is correct
8 Correct 190 ms 141492 KB Output is correct
9 Correct 181 ms 139116 KB Output is correct
10 Correct 190 ms 140900 KB Output is correct
11 Correct 183 ms 138824 KB Output is correct
12 Correct 230 ms 152648 KB Output is correct
13 Correct 232 ms 153288 KB Output is correct
14 Correct 109 ms 95080 KB Output is correct
15 Correct 166 ms 116168 KB Output is correct
16 Correct 180 ms 118636 KB Output is correct
17 Correct 135 ms 116976 KB Output is correct
18 Correct 104 ms 100676 KB Output is correct
19 Correct 88 ms 97848 KB Output is correct
20 Correct 94 ms 98340 KB Output is correct
21 Correct 92 ms 97832 KB Output is correct
22 Correct 91 ms 97936 KB Output is correct
23 Correct 97 ms 98372 KB Output is correct
24 Correct 182 ms 134192 KB Output is correct
25 Correct 201 ms 135436 KB Output is correct
26 Correct 171 ms 133264 KB Output is correct
27 Correct 184 ms 133672 KB Output is correct
28 Correct 227 ms 112448 KB Output is correct
29 Correct 190 ms 107228 KB Output is correct