Submission #391106

# Submission time Handle Problem Language Result Execution time Memory
391106 2021-04-17T21:08:51 Z Return_0 Selling RNA Strands (JOI16_selling_rna) C++17
0 / 100
228 ms 196376 KB
#pragma GCC optimize("Ofast,unroll-loops")
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma,tune=native")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef int ll;
typedef long double ld;
typedef pair <ll, ll> pll;

#ifdef SINA
#define dbg(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << std::endl; }
template <typename Arg1, typename... Args> void __f (const char* names, Arg1&& arg1, Args&&... args) {
	const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...); }
#define dbg2(x, j, n) cout<< #x << " : "; output((j), (n), x, 1); cout.flush();
#else
#define dbg(...) 0
#define dbg2(x, j, n) 0
#endif
#define SZ(x) ((ll)((x).size()))
#define File(s, t) freopen(s ".txt", "r", stdin); freopen(t ".txt", "w", stdout);
#define input(j, n, a) for (int _i = (j); _i < (n)+(j); _i++) cin>> a[_i];
#define output(j, n, a, t) for (int _i = (j); _i < (n)+(j); _i++) cout<< a[_i] << (((t) && _i != (n)+(j)-1)? ' ' : '\n');
#define kill(x) return cout<< (x) << endl, 0
#define cl const ll
#define fr first
#define sc second
#define lc (v << 1)
#define rc (lc | 1)
#define mid ((l + r) >> 1)
#define All(x) (x).begin(), (x).end()

cl inf = sizeof(ll) == 4 ? (1e9 + 10) : (3e18), mod = 1e9 + 7, MOD = 998244353;

template <class A,class B> ostream& operator << (ostream& out,const pair<A,B>&a){return out<<'('<<a.first<<", "<<a.second<<')';}
template <class A> ostream& operator << (ostream& out, const vector<A> &a) {
	out<< '['; for (int i = -1; ++i < int(a.size());) out<< a[i] << (i + 1 < int(a.size()) ? ", " : ""); return out<<']'; }
template <class T, typename _t = less <T> > using Tree = tree <T, null_type, _t, rb_tree_tag, tree_order_statistics_node_update>;

#define ask(l, r, x) ll(upper_bound(All(x), r) - lower_bound(All(x), l))

cl N = 1e5 + 7, K = 2e6 + 7;

ll tin [K], tout [K], chr [100], n, q;
string s [N], t1, t2;

ll tr [K][4], lst, tr2 [K][4], lst2;
vector <ll> id [K], id2 [K];

inline void add2 (const string &_s, cl &_id) {
	ll cur = 0;
	for (auto &ch : _s) {
		id2[cur].push_back(_id);
		if (!tr2[cur][chr[ch]]) tr2[cur][chr[ch]] = ++lst2;
		cur = tr2[cur][chr[ch]];
	}
	id2[cur].push_back(_id);
}

inline void add (const string &_s, cl &_id) {
	ll cur = 0;
	for (auto &ch : _s) {
		if (!tr[cur][chr[ch]]) tr[cur][chr[ch]] = ++lst;
		cur = tr[cur][chr[ch]];
	}
	id[cur].push_back(_id);
}

void _sort (cl &v = 0) {
	tin[v] = lst + 1;
	for (auto &x : id[v]) add2(s[x], ++lst);
	if (tr[v][0]) _sort(tr[v][0]);
	if (tr[v][1]) _sort(tr[v][1]);
	if (tr[v][2]) _sort(tr[v][2]);
	if (tr[v][3]) _sort(tr[v][3]);
	tout[v] = lst;
}

ll get_id (const string &_s) {
	ll cur = 0;
	for (auto &ch : _s) {
		if (!tr[cur][chr[ch]]) return -1;
		cur = tr[cur][chr[ch]];
	}
	return cur;
}

ll get_id2 (const string &_s) {
	ll cur = 0;
	for (auto &ch : _s) {
		if (!tr2[cur][chr[ch]]) return -1;
		cur = tr2[cur][chr[ch]];
	}
	return cur;
}

ll solve () {
	cin>> t1 >> t2;
	ll x = get_id(t1);
	if (!~x) return 0;
	ll y = get_id2(t2);
	return max(0, ask(tin[x], tout[x], id2[y]));
}

int main ()
{
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);

	chr['A'] = 0, chr['C'] = 1, chr['G'] = 2, chr['U'] = 3;
	cin>> n >> q;
	for (ll i = 1; i <= n; i++) {
		cin>> s[i];
		add(s[i], i);
		reverse(All(s[i]));
	}

	lst = 0; _sort();

	for (; q--;) cout<< solve() << '\n';

	cerr<< "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";

	return 0;
}
/*
2 3
AUGC
AGC
G C
AU C
A C

*/

Compilation message

selling_rna.cpp: In function 'void add2(const string&, const ll&)':
selling_rna.cpp:60:21: warning: array subscript has type 'char' [-Wchar-subscripts]
   60 |   if (!tr2[cur][chr[ch]]) tr2[cur][chr[ch]] = ++lst2;
      |                     ^~
selling_rna.cpp:60:40: warning: array subscript has type 'char' [-Wchar-subscripts]
   60 |   if (!tr2[cur][chr[ch]]) tr2[cur][chr[ch]] = ++lst2;
      |                                        ^~
selling_rna.cpp:61:22: warning: array subscript has type 'char' [-Wchar-subscripts]
   61 |   cur = tr2[cur][chr[ch]];
      |                      ^~
selling_rna.cpp: In function 'void add(const string&, const ll&)':
selling_rna.cpp:69:20: warning: array subscript has type 'char' [-Wchar-subscripts]
   69 |   if (!tr[cur][chr[ch]]) tr[cur][chr[ch]] = ++lst;
      |                    ^~
selling_rna.cpp:69:38: warning: array subscript has type 'char' [-Wchar-subscripts]
   69 |   if (!tr[cur][chr[ch]]) tr[cur][chr[ch]] = ++lst;
      |                                      ^~
selling_rna.cpp:70:21: warning: array subscript has type 'char' [-Wchar-subscripts]
   70 |   cur = tr[cur][chr[ch]];
      |                     ^~
selling_rna.cpp: In function 'll get_id(const string&)':
selling_rna.cpp:88:20: warning: array subscript has type 'char' [-Wchar-subscripts]
   88 |   if (!tr[cur][chr[ch]]) return -1;
      |                    ^~
selling_rna.cpp:89:21: warning: array subscript has type 'char' [-Wchar-subscripts]
   89 |   cur = tr[cur][chr[ch]];
      |                     ^~
selling_rna.cpp: In function 'll get_id2(const string&)':
selling_rna.cpp:97:21: warning: array subscript has type 'char' [-Wchar-subscripts]
   97 |   if (!tr2[cur][chr[ch]]) return -1;
      |                     ^~
selling_rna.cpp:98:22: warning: array subscript has type 'char' [-Wchar-subscripts]
   98 |   cur = tr2[cur][chr[ch]];
      |                      ^~
# Verdict Execution time Memory Grader output
1 Incorrect 57 ms 97404 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 228 ms 196376 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 79 ms 99268 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 57 ms 97404 KB Output isn't correct
2 Halted 0 ms 0 KB -