Submission #963921

# Submission time Handle Problem Language Result Execution time Memory
963921 2024-04-16T03:28:22 Z LCJLY Selling RNA Strands (JOI16_selling_rna) C++14
100 / 100
203 ms 165004 KB
#include <bits/stdc++.h>
using namespace std;	
 
#define int long long 
#define ld long double
#define show(x,y) cout << y << " " << #x << endl;
#define show2(x,y,i,j) cout << y << " " << #x << "  " << j << " " << #i << endl;
#define show3(x,y,i,j,p,q) cout << y << " " << #x << "  " << j << " " << #i << "  " << q << " " << #p << endl;
#define show4(x,y) for(auto it:y) cout << it << " "; cout << #x << endl;
typedef pair<long long,int>pii;
typedef pair<int,pii>pi2;
 
struct trie{	
	trie *next[4];
	int in=0;
	int out=0;
	trie(){
		next[0]=next[1]=next[2]=next[3]=NULL;
	}	
};
 
auto rt=new trie();
auto rev=new trie();
 
int f(char a){
	if(a=='A') return 0;
	else if(a=='C') return 1;
	else if(a=='G') return 2;
	else return 3;
}
 
int ptr=0;
 
void dfs(trie*cur){
	cur->in=++ptr;
	for(int x=0;x<4;x++){
		if(cur->next[x]!=NULL){
			dfs(cur->next[x]);
		}
	}
	cur->out=ptr;
}
 
int fw[1000005];
 
void upd(int x, int y){	
	while(x<1000005){
		fw[x]+=y;
		x+=x&(-x);
	}	
}
 
int sum(int x){
	int counter=0;
	while(x>0){
		counter+=fw[x];
		x-=x&(-x);
	}
	return counter;
}
 
void rangeUpd(int x, int y, int c){		
	upd(x,c);
	upd(x+1,-c);
}
 
int query(int x, int y){
	return sum(y)-sum(x-1);
}
 
int ans[100005];
vector<array<int,4>>storage[200005];
 
void solve(){
	int n,m;
	cin >> n >> m;
	string arr[n];
	for(int x=0;x<n;x++){
		cin >> arr[x];
		//front
		auto cur=rt;
		for(int y=0;y<(int)arr[x].length();y++){
			int hold=f(arr[x][y]);
			if(cur->next[hold]==NULL) cur->next[hold]=new trie();
			cur=cur->next[hold];
		}
		
		//back
		string s=arr[x];
		reverse(s.begin(),s.end());
		cur=rev;
		for(int y=0;y<(int)s.length();y++){
			int hold=f(s[y]);
			if(cur->next[hold]==NULL) cur->next[hold]=new trie();
			cur=cur->next[hold];
		}
	}
	
	dfs(rt);
	ptr=0;
	dfs(rev);
	
	vector<int>dis;
	vector<int>dis2;
	
	vector<pii>v;
	for(int x=0;x<n;x++){
		string s=arr[x];
		reverse(s.begin(),s.end());
		int sz=s.length();
		
		auto cur=rt;
		auto cur2=rev;
		for(int y=0;y<sz;y++){
			int hold=f(arr[x][y]);
			int hold2=f(s[y]);
			cur=cur->next[hold];
			cur2=cur2->next[hold2];
		}
		
		v.push_back({cur->in,cur2->in});
		dis.push_back(cur2->in);
		dis2.push_back(cur->in);
	}
	
	string temp,temp2;
	vector<array<int,5>>bruh;
	for(int x=0;x<m;x++){
		cin >> temp >> temp2;
		//front
		auto cur=rt;
		int sz=temp.size();
		bool amos=true;
		for(int y=0;y<sz;y++){
			int hold=f(temp[y]);
			if(cur->next[hold]==NULL){
				amos=false;
				break;
			}
			cur=cur->next[hold];
		}
		
		reverse(temp2.begin(),temp2.end());
		auto cur2=rev;
		sz=temp2.size();
		for(int y=0;y<sz;y++){
			int hold=f(temp2[y]);
			if(cur2->next[hold]==NULL){
				amos=false;
				break;
			}
			cur2=cur2->next[hold];
		}
		
		if(!amos) continue;
		else{
			//storage[cur->in-1].push_back({cur2->in,cur2->out,x,-1});
			//storage[cur->out].push_back({cur2->in,cur2->out,x,1});
			dis.push_back(cur2->in);
			dis.push_back(cur2->out);
			dis2.push_back(cur->in-1);
			dis2.push_back(cur->out);
			bruh.push_back({cur->in-1,cur2->in,cur2->out,x,-1});
			bruh.push_back({cur->out,cur2->in,cur2->out,x,1});
		}
	}
	
	//l r index sign
	
	sort(v.begin(),v.end());
	sort(dis.begin(),dis.end());
	sort(dis2.begin(),dis2.end());
	dis2.resize(unique(dis2.begin(),dis2.end())-dis2.begin());
	dis.resize(unique(dis.begin(),dis.end())-dis.begin());
	for(auto &it:v){
		it.first=lower_bound(dis2.begin(),dis2.end(),it.first)-dis2.begin()+1;
	}
	for(auto &it:bruh){
		it[0]=lower_bound(dis2.begin(),dis2.end(),it[0])-dis2.begin()+1;
		storage[it[0]].push_back({it[1],it[2],it[3],it[4]});
	}
	int swp=0;
	for(int x=0;x<150005;x++){
		while(swp<(int)v.size()&&v[swp].first<=x){
			v[swp].second=lower_bound(dis.begin(),dis.end(),v[swp].second)-dis.begin()+1;
			upd(v[swp].second,1);
			swp++;
		}
		
		for(auto it:storage[x]){
			int l=lower_bound(dis.begin(),dis.end(),it[0])-dis.begin()+1;
			int r=lower_bound(dis.begin(),dis.end(),it[1])-dis.begin()+1;
			ans[it[2]]+=(query(l,r)*it[3]);
		}
	}
	
	for(int x=0;x<m;x++){
		cout << ans[x] << "\n";
	}
}
 
int32_t main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	int t=1;
	//cin >> t;
	while(t--){
		solve();
	}
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5212 KB Output is correct
2 Correct 3 ms 5212 KB Output is correct
3 Correct 3 ms 5212 KB Output is correct
4 Correct 3 ms 5212 KB Output is correct
5 Correct 4 ms 5212 KB Output is correct
6 Correct 3 ms 5172 KB Output is correct
7 Correct 3 ms 5212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 170 ms 135312 KB Output is correct
2 Correct 154 ms 129384 KB Output is correct
3 Correct 165 ms 134084 KB Output is correct
4 Correct 165 ms 128252 KB Output is correct
5 Correct 203 ms 162664 KB Output is correct
6 Correct 203 ms 165004 KB Output is correct
7 Correct 50 ms 13560 KB Output is correct
8 Correct 143 ms 103992 KB Output is correct
9 Correct 132 ms 89312 KB Output is correct
10 Correct 103 ms 84748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 20220 KB Output is correct
2 Correct 32 ms 14364 KB Output is correct
3 Correct 40 ms 17124 KB Output is correct
4 Correct 30 ms 14852 KB Output is correct
5 Correct 29 ms 14072 KB Output is correct
6 Correct 36 ms 16124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5212 KB Output is correct
2 Correct 3 ms 5212 KB Output is correct
3 Correct 3 ms 5212 KB Output is correct
4 Correct 3 ms 5212 KB Output is correct
5 Correct 4 ms 5212 KB Output is correct
6 Correct 3 ms 5172 KB Output is correct
7 Correct 3 ms 5212 KB Output is correct
8 Correct 170 ms 135312 KB Output is correct
9 Correct 154 ms 129384 KB Output is correct
10 Correct 165 ms 134084 KB Output is correct
11 Correct 165 ms 128252 KB Output is correct
12 Correct 203 ms 162664 KB Output is correct
13 Correct 203 ms 165004 KB Output is correct
14 Correct 50 ms 13560 KB Output is correct
15 Correct 143 ms 103992 KB Output is correct
16 Correct 132 ms 89312 KB Output is correct
17 Correct 103 ms 84748 KB Output is correct
18 Correct 37 ms 20220 KB Output is correct
19 Correct 32 ms 14364 KB Output is correct
20 Correct 40 ms 17124 KB Output is correct
21 Correct 30 ms 14852 KB Output is correct
22 Correct 29 ms 14072 KB Output is correct
23 Correct 36 ms 16124 KB Output is correct
24 Correct 170 ms 117268 KB Output is correct
25 Correct 178 ms 123524 KB Output is correct
26 Correct 149 ms 113568 KB Output is correct
27 Correct 154 ms 115348 KB Output is correct
28 Correct 168 ms 53456 KB Output is correct
29 Correct 98 ms 34976 KB Output is correct