Submission #542419

# Submission time Handle Problem Language Result Execution time Memory
542419 2022-03-26T16:31:13 Z cadmiumsky Selling RNA Strands (JOI16_selling_rna) C++14
100 / 100
412 ms 198952 KB
#include <bits/stdc++.h>

using namespace std;

const int lenmax = 2e6 + 5, nmax = 1e5 + 5;
int n, q;
  

int encr[256], decr[256];

string pad;

struct Trie {
  Trie* v[4] = {0, 0, 0, 0};
  vector<int> list;
  int l, r;
  void insert(int *s, int val) {
    l = 0, r = 0;
    if(s[0] == -1) {
      list.push_back(val);
      return;
    }
    if(v[s[0]] == 0)
      v[s[0]] = new Trie;
    v[s[0]]->insert(s + 1, val);
    return;
  }
  void construct(vector<int>& preord) {
    l = preord.size();
    for(auto x : list)
      preord.push_back(x);
    for(int i = 0; i < 4; i++) {
      if(v[i] != 0)
        v[i]->construct(preord);
    }
    r = preord.size() - 1;
  }
  pair<int,int> query(int *s) {
    if(s[0] == -1) {
      return {l, r};
    }
    if(v[s[0]] == 0)
      return {-1, -1};
    return v[s[0]]->query(s + 1);
  }
} forw, back;
int s[lenmax];
static void init() {
  encr['A'] = 0;
  encr['C'] = 1;
  encr['G'] = 2;
  encr['U'] = 3;
  encr['\n'] = -1;
  decr[0] = 'A';
  decr[1] = 'C';
  decr[2] = 'G';
  decr[3] = 'U';
}

namespace AINT {
  int aint[nmax * 4];
  void add(int poz, int node = 1, int cl = 0, int cr = n - 1) {
    if(cl == cr) {
      aint[node]++;
      return;
    }
    int mid = cl + cr >> 1;
    if(poz <= mid)  
      add(poz, 2 * node, cl, mid);
    else
      add(poz, 2 * node + 1, mid + 1, cr);
    aint[node] = aint[node  * 2 + 1] + aint[node * 2];
  }
  int query(int l, int r, int node = 1, int cl = 0, int cr = n - 1) {
    if(cr < l || r < cl)
      return 0;
    if(l <= cl && cr <= r)
      return aint[node];
    int mid = cl + cr >> 1;
    return query(l, r, 2 * node, cl, mid) + query(l, r, 2 * node + 1, mid + 1, cr);
  }
}

int rez[nmax];
vector<tuple<int,int,int> > qs[nmax];
#define qs (qs + 1)

int main() {
  init();
  cin >> n >> q;
  cin.get();
  for(int i = 0, ptr = 0; i < n; ptr = 0, i++) {
    char ch;
    while((ch = cin.get()) != '\n') {
      s[ptr++] = encr[ch];
    }
    s[ptr] = -1;
    forw.insert(s, i);
    reverse(s, s + ptr);
    back.insert(s, i);
  }
  vector<int> fata, spate, inv;
  forw.construct(fata);
  back.construct(spate);
  assert(fata.size() == n);
  assert(spate.size() == n);
  inv.resize(spate.size());
  //for(auto x : fata)
    //cout << x << ' ';
  //cout << '\n';
  //for(auto x : spate)
    //cout << x << ' ';
  //cout << '\n';
  for(int i = 0; i < spate.size(); i++)
    inv[spate[i]] = i;
  for(int i = 0; i < fata.size(); i++)
    fata[i] = inv[fata[i]];
  //for(auto x : fata)
    //cout << x << ' ';
  //cout << '\n';
  for(int i = 1, ptr = 0, l1, r1, l2, r2; i <= q; ptr = 0, i++) {
    char ch;
    while(!isspace(ch = cin.get())) {
      s[ptr++] = encr[ch];
    }
    s[ptr] = -1;
    ptr = 0;
    tie(l1, r1) = forw.query(s);
    while(!isspace(ch = cin.get())) {
      s[ptr++] = encr[ch];
    }
    s[ptr] = -1;
    reverse(s, s + ptr);
    tie(l2, r2) = back.query(s);
    if(l1 >= 0 && l2 >= 0 && r1 >= 0 && r2 >= 0) {
      qs[l1 - 1].push_back({-i, l2, r2});
      qs[r1].push_back({i, l2, r2});
    }
    //cerr << l1 << ' ' << r1 << ' '<< l2 << ' ' << r2 << '\n';
  }
  for(int i = 0; i < fata.size(); i++) {
    AINT::add(fata[i]);
    for(auto [ind, l, r] : qs[i])
      rez[abs(ind)] += AINT::query(l, r) * (ind / abs(ind));
  }
  for(int i = 1; i <= q; i++)
    cout << rez[i] << '\n';
}

Compilation message

selling_rna.cpp: In function 'void AINT::add(int, int, int, int)':
selling_rna.cpp:67:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   67 |     int mid = cl + cr >> 1;
      |               ~~~^~~~
selling_rna.cpp: In function 'int AINT::query(int, int, int, int, int)':
selling_rna.cpp:79:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   79 |     int mid = cl + cr >> 1;
      |               ~~~^~~~
selling_rna.cpp: In function 'int main()':
selling_rna.cpp:95:23: warning: array subscript has type 'char' [-Wchar-subscripts]
   95 |       s[ptr++] = encr[ch];
      |                       ^~
In file included from /usr/include/c++/10/cassert:44,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
                 from selling_rna.cpp:1:
selling_rna.cpp:105:22: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  105 |   assert(fata.size() == n);
      |          ~~~~~~~~~~~~^~~~
selling_rna.cpp:106:23: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  106 |   assert(spate.size() == n);
      |          ~~~~~~~~~~~~~^~~~
selling_rna.cpp:114:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  114 |   for(int i = 0; i < spate.size(); i++)
      |                  ~~^~~~~~~~~~~~~~
selling_rna.cpp:116:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  116 |   for(int i = 0; i < fata.size(); i++)
      |                  ~~^~~~~~~~~~~~~
selling_rna.cpp:124:23: warning: array subscript has type 'char' [-Wchar-subscripts]
  124 |       s[ptr++] = encr[ch];
      |                       ^~
selling_rna.cpp:130:23: warning: array subscript has type 'char' [-Wchar-subscripts]
  130 |       s[ptr++] = encr[ch];
      |                       ^~
selling_rna.cpp:141:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  141 |   for(int i = 0; i < fata.size(); i++) {
      |                  ~~^~~~~~~~~~~~~
selling_rna.cpp:143:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  143 |     for(auto [ind, l, r] : qs[i])
      |              ^
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2664 KB Output is correct
2 Correct 2 ms 2660 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2664 KB Output is correct
5 Correct 2 ms 2668 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 332 ms 161368 KB Output is correct
2 Correct 324 ms 153216 KB Output is correct
3 Correct 329 ms 159188 KB Output is correct
4 Correct 325 ms 151872 KB Output is correct
5 Correct 314 ms 196136 KB Output is correct
6 Correct 320 ms 198952 KB Output is correct
7 Correct 257 ms 8668 KB Output is correct
8 Correct 412 ms 120908 KB Output is correct
9 Correct 376 ms 102736 KB Output is correct
10 Correct 247 ms 97448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 6732 KB Output is correct
2 Correct 33 ms 5676 KB Output is correct
3 Correct 40 ms 6620 KB Output is correct
4 Correct 33 ms 5744 KB Output is correct
5 Correct 32 ms 5624 KB Output is correct
6 Correct 40 ms 6280 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2664 KB Output is correct
2 Correct 2 ms 2660 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2664 KB Output is correct
5 Correct 2 ms 2668 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 332 ms 161368 KB Output is correct
9 Correct 324 ms 153216 KB Output is correct
10 Correct 329 ms 159188 KB Output is correct
11 Correct 325 ms 151872 KB Output is correct
12 Correct 314 ms 196136 KB Output is correct
13 Correct 320 ms 198952 KB Output is correct
14 Correct 257 ms 8668 KB Output is correct
15 Correct 412 ms 120908 KB Output is correct
16 Correct 376 ms 102736 KB Output is correct
17 Correct 247 ms 97448 KB Output is correct
18 Correct 40 ms 6732 KB Output is correct
19 Correct 33 ms 5676 KB Output is correct
20 Correct 40 ms 6620 KB Output is correct
21 Correct 33 ms 5744 KB Output is correct
22 Correct 32 ms 5624 KB Output is correct
23 Correct 40 ms 6280 KB Output is correct
24 Correct 314 ms 134132 KB Output is correct
25 Correct 335 ms 135792 KB Output is correct
26 Correct 306 ms 132088 KB Output is correct
27 Correct 318 ms 132328 KB Output is correct
28 Correct 299 ms 31548 KB Output is correct
29 Correct 206 ms 13028 KB Output is correct