답안 #1083713

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1083713 2024-09-03T21:10:04 Z idiotcomputer Selling RNA Strands (JOI16_selling_rna) C++11
35 / 100
568 ms 1048576 KB
#include <bits/stdc++.h>
using namespace std;
#define ll long long int 
#define sz(x) (int) x.size()
#define pb push_back

const int mxN = 1e5+5;
const int mC = 3e7;
int n;

int v[mC];
int sum[mC];
vector<int> adj[mC];
int roots[4*mxN+1];
int cnt = 0;

int dfs(int node, int idx, string &s){
	if (idx == 0) return sum[node];
	for (int i : adj[node]){
		if (v[i] == s[idx-1]-'A') return dfs(i,idx-1,s);
	}
	return 0;
}

void add(int node, int idx, string &s){
	sum[node]++;
	if (idx == 0) return;
	
	//cout << node << " " << (char) (v[node] + 'A') << ": ";
	//for (int i : adj[node]) cout << i << " ";
	//cout << '\n';
	
	for (int i : adj[node]){
		if (v[i] == s[idx-1]-'A') return add(i,idx-1,s);
	}
	v[cnt] = s[idx-1] - 'A';
	sum[cnt] = 0;
	adj[node].pb(cnt);
	cnt++;
	return add(cnt-1,idx-1,s);
}

void upd(int t, string &cv, int l = 0, int r = n-1, int idx = 0){
	if (l > t || r < t) return;
	//cout << t << "," << cv << " " << l << "," << r << ": ";
	if (roots[idx] == -1){
		roots[idx] = cnt;
		sum[cnt] = 0;
		v[cnt] = 1;
		cnt++;
	}
	//cout << roots[idx] << '\n';
	add(roots[idx],sz(cv)-1,cv);
	if (l == r) return;
	int m = (l+r)/2;
	upd(t,cv,l,m,2*idx+1);
	upd(t,cv,m+1,r,2*idx+2);
}

int query(int tl, int tr, string &cv, int l = 0, int r = n-1, int idx = 0){
	if (tl > tr) return 0;
	if (l > tr || r < tl) return 0;
	//cout << cv << " " << l << " " << r << " " << tl << " " << tr << '\n';
	if (l >= tl && r <= tr) return dfs(roots[idx],sz(cv)-1,cv);
	int m = (l+r)/2;
	return query(tl,tr,cv,l,m,2*idx+1) + query(tl,tr,cv,m+1,r,2*idx+2);
}


int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	int m;
	cin >> n >> m;

	vector<string> all(n);
	for (int i = 0; i < n; i++) cin >> all[i];
	sort(all.begin(),all.end());

	for (int i = 0; i < 4*n+1; i++) roots[i] = -1;
	string temp;
	for (int i = 0; i < n; i++){
		//cout << all[i] << " ";
		temp = all[i]+'B';
		upd(i,temp);
	} 
	//cout << '\n';

	

	int l,r;
	string a,b;
	//return 0;
	for (int i = 0; i < m; i++){
		cin >> a >> b;
		l = lower_bound(all.begin(),all.end(), a) - all.begin();
		r = lower_bound(all.begin(),all.end(), a+'Z') - all.begin();
		temp = b + 'B';
		//cout << a << " " << b << '\n';
		//cout << ("AU" < "AUGC") << '\n';
		//cout << l << " " << r << ' ' << temp << '\n';
		cout << query(l,r-1,temp) << '\n';
	}
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 275 ms 706408 KB Output is correct
2 Correct 339 ms 706384 KB Output is correct
3 Correct 277 ms 706384 KB Output is correct
4 Correct 281 ms 706640 KB Output is correct
5 Correct 278 ms 706332 KB Output is correct
6 Correct 270 ms 706276 KB Output is correct
7 Correct 276 ms 706388 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 568 ms 1048576 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 320 ms 717400 KB Output is correct
2 Correct 328 ms 718864 KB Output is correct
3 Correct 321 ms 718160 KB Output is correct
4 Correct 345 ms 716368 KB Output is correct
5 Correct 312 ms 717140 KB Output is correct
6 Correct 348 ms 717496 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 275 ms 706408 KB Output is correct
2 Correct 339 ms 706384 KB Output is correct
3 Correct 277 ms 706384 KB Output is correct
4 Correct 281 ms 706640 KB Output is correct
5 Correct 278 ms 706332 KB Output is correct
6 Correct 270 ms 706276 KB Output is correct
7 Correct 276 ms 706388 KB Output is correct
8 Runtime error 568 ms 1048576 KB Execution killed with signal 9
9 Halted 0 ms 0 KB -