답안 #26513

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
26513 2017-07-01T20:30:25 Z rubabredwan Selling RNA Strands (JOI16_selling_rna) C++14
60 / 100
559 ms 849740 KB
/*  Bismillahir Rahmanir Rahim  */

#include <bits/stdc++.h>

using namespace std;

const int N = 100005;
const int M = 2000005;

int mp[M], from[M];

struct TRIE{

	map <char, int> trie[M];
	int in[M], out[M], cnt[M];
	int idx, t;

	void init(){
		memset(cnt, 0, sizeof(cnt));
		idx = 1;
		t = 0;
	}

	int insert(string str, int ot){
		int cur = 1;
		for(auto c : str){
			if(trie[cur].count(c) == 0) trie[cur][c] = ++idx;
			cur = trie[cur][c];
		}
		from[cur] = ot;
		++cnt[cur];
		return cur;
	}

	int query(string str){
		int cur = 1;
		for(auto c : str){
			if(trie[cur].count(c) == 0) return -1;
			cur = trie[cur][c];
		}
		return cur;
	}

	void dfs(int at){
		in[at] = ++t;
		mp[t] = at;
		for(auto c : {'A', 'C', 'G', 'U'}){
			if(trie[at].count(c)){ 
				dfs(trie[at][c]);
			}
		}
		out[at] = t;
	}

};

int n, m, k;
int nn, mm;
string rna[N];
TRIE t1, t2;
int id1[N], id2[N];

int root[M];
int lf[20 * M], rf[20 * M];
int sum[20 * M];
int idx;

int update(int b, int e, int node, int pos, int val){
	int cur = ++idx;
	sum[cur] = sum[node] + val;
	if(b == e) return cur;
	int mid = (b + e) / 2;
	lf[cur] = lf[node];
	rf[cur] = rf[node];
	if(pos <= mid) lf[cur] = update(b, mid, lf[node], pos, val);
	else rf[cur] = update(mid + 1, e, rf[node], pos, val);
	return cur;
}

int get(int b, int e, int node, int x, int y){
	if(y < b || e < x || node == 0) return 0;
	if(b >= x && e <= y) return sum[node];
	int mid = (b + e) / 2;
	return get(b, mid, lf[node], x, y) + get(mid + 1, e, rf[node], x, y);
}

void init(){
	t1.init();
	for(int i = 1; i <= n; ++i){
		id1[i] = t1.insert(rna[i], 0);
	}	
	t1.dfs(1);
	nn = t1.idx;
	for(int i = 1; i <= n; ++i) reverse(rna[i].begin(), rna[i].end());
	for(int i = 1; i <= n; ++i){
		id2[i] = t2.insert(rna[i], t1.in[ id1[i] ]);
	}	
	t2.dfs(1);
	mm = t2.idx;
	for(int i = 1; i <= mm; ++i){
		int at = mp[i];
		if(from[at] == 0) root[i] = root[i-1];
		else{
			root[i] = update(1, nn, root[i-1], from[at], t2.cnt[at]);
		}
	}
}

int query(string p, string q){
	int a = t1.query(p);
	int b = t2.query(q);
	if(a == -1 || b == -1) return 0;
	int aa = get(1, nn, root[ t2.out[b] ], t1.in[a], t1.out[a]);
	int bb = get(1, nn, root[ t2.in[b] - 1 ], t1.in[a], t1.out[a]);
	return aa - bb;
}

int main(){
	ios_base::sync_with_stdio(false);
	t1.init();
	t2.init();
	cin >> n >> m;
	for(int i = 1; i <= n; ++i){
		cin >> rna[i];
	}
	init();
	string p, q;
	for(int i = 1; i <= m; ++i){
		cin >> p >> q;
		reverse(q.begin(), q.end());
		cout << query(p, q) << '\n';
	}
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 66 ms 732656 KB Output is correct
2 Correct 53 ms 732656 KB Output is correct
3 Correct 46 ms 732656 KB Output is correct
4 Correct 53 ms 732656 KB Output is correct
5 Correct 59 ms 732656 KB Output is correct
6 Correct 46 ms 732656 KB Output is correct
7 Correct 43 ms 732656 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 419 ms 827240 KB Output is correct
2 Correct 446 ms 822144 KB Output is correct
3 Correct 436 ms 825840 KB Output is correct
4 Correct 459 ms 821356 KB Output is correct
5 Correct 509 ms 848024 KB Output is correct
6 Correct 483 ms 849740 KB Output is correct
7 Correct 146 ms 734372 KB Output is correct
8 Correct 453 ms 801560 KB Output is correct
9 Correct 506 ms 790736 KB Output is correct
10 Correct 269 ms 789152 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 156 ms 732656 KB Output is correct
2 Correct 123 ms 732920 KB Output is correct
3 Correct 156 ms 732788 KB Output is correct
4 Correct 109 ms 732656 KB Output is correct
5 Correct 196 ms 732920 KB Output is correct
6 Correct 243 ms 732788 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 66 ms 732656 KB Output is correct
2 Correct 53 ms 732656 KB Output is correct
3 Correct 46 ms 732656 KB Output is correct
4 Correct 53 ms 732656 KB Output is correct
5 Correct 59 ms 732656 KB Output is correct
6 Correct 46 ms 732656 KB Output is correct
7 Correct 43 ms 732656 KB Output is correct
8 Correct 419 ms 827240 KB Output is correct
9 Correct 446 ms 822144 KB Output is correct
10 Correct 436 ms 825840 KB Output is correct
11 Correct 459 ms 821356 KB Output is correct
12 Correct 509 ms 848024 KB Output is correct
13 Correct 483 ms 849740 KB Output is correct
14 Correct 146 ms 734372 KB Output is correct
15 Correct 453 ms 801560 KB Output is correct
16 Correct 506 ms 790736 KB Output is correct
17 Correct 269 ms 789152 KB Output is correct
18 Correct 156 ms 732656 KB Output is correct
19 Correct 123 ms 732920 KB Output is correct
20 Correct 156 ms 732788 KB Output is correct
21 Correct 109 ms 732656 KB Output is correct
22 Correct 196 ms 732920 KB Output is correct
23 Correct 243 ms 732788 KB Output is correct
24 Correct 489 ms 810124 KB Output is correct
25 Runtime error 559 ms 810120 KB Execution timed out (wall clock limit exceeded)
26 Halted 0 ms 0 KB -