#include<iostream>
#include<vector>
#include<algorithm>
#include<stdio.h>
using namespace std;
#define MOD 1000000007
#define BASE 257
typedef long long int lld;
typedef pair<int,int> pii;
class FT{
int FT[2000000];
int N;
public:
void init(int n){
N=n;
for(int i=0;i<=N;i++)FT[i]=0;
}
void upd(int pos){
for(int i=pos;i<=N;i+=i&(-i)){
FT[i]++;
}
}
int query(int pos){
int ans=0;
for(int i=pos;i>0;i-=i&(-i)){
ans+=FT[i];
}return ans;
}
};
struct Trie{
struct Trie *child[26];
vector<int> end;
};
void insert(Trie *T,string s, int index){
Trie *S=T;
for(int i=0;i<s.length();i++){
int u=s[i]-'A';
if(!S->child[u]){
S->child[u]=new Trie();
for(int j=0;j<26;j++)S->child[u]->child[j]=NULL;
}
S=S->child[u];
}
S->end.push_back(index);
}
pair<int,int> Query1[1000000];
vector<int> EulerTour1;
pair<int,int> Query2[1000000];
vector<int> EulerTour2;
void DFS1(Trie *T){
sort(T->end.begin(),T->end.end());
for(int i=0;i<T->end.size();i++){
if(T->end[i]<=0){
Query1[-T->end[i]].first=EulerTour1.size();
}else EulerTour1.push_back(T->end[i]-1);
//cout<<T->end[i]<<" ";
}
//cout<<endl;
for(int i=0;i<26;i++){
if(T->child[i]){
DFS1(T->child[i]);
}
}
for(int i=0;i<T->end.size();i++){
if(T->end[i]<=0){
Query1[-T->end[i]].second=EulerTour1.size();
}
//cout<<T->end[i]<<" ";
}
}
void DFS2(Trie *T){
sort(T->end.begin(),T->end.end());
for(int i=0;i<T->end.size();i++){
if(T->end[i]<=0){
Query2[-T->end[i]].first=EulerTour2.size();
}else EulerTour2.push_back(T->end[i]-1);
//cout<<T->end[i]<<" ";
}
//cout<<endl;
for(int i=0;i<26;i++){
if(T->child[i]){
DFS2(T->child[i]);
}
}
for(int i=0;i<T->end.size();i++){
if(T->end[i]<=0){
Query2[-T->end[i]].second=EulerTour2.size();
}
//cout<<T->end[i]<<" ";
}
}
int main(){
int n,m;
cin>>n>>m;
string arr[n];
Trie *T=new Trie();
for(int i=0;i<n;i++){
cin>>arr[i];
insert(T,arr[i],i+1);
}
string Queries1[m];
string Queries2[m];
for(int i=0;i<m;i++){
cin>>Queries1[i]>>Queries2[i];
insert(T,Queries1[i],-i);
}
DFS1(T);
/*for(int i=0;i<n;i++){
cout<<EulerTour1[i]<<endl;
}
for(int i=0;i<m;i++){
cout<<Query1[i].first<<" "<<Query1[i].second<<endl;
}*/
T=new Trie();
for(int i=0;i<n;i++){
reverse(arr[i].begin(),arr[i].end());
insert(T,arr[i],i+1);
}
for(int i=0;i<m;i++){
reverse(Queries2[i].begin(),Queries2[i].end());
insert(T,Queries2[i],-i);
}
DFS2(T);
/*for(int i=0;i<m;i++){
cout<<Query2[i].first<<" "<<Query2[i].second<<endl;
}*/
pair<int,int> ET[n];
for(int i=0;i<n;i++){
ET[EulerTour1[i]].first=i;
ET[EulerTour2[i]].second=i;
}sort(ET,ET+n);
int answer[4*m];
vector<pair<pii,int> >Queries;
for(int i=0;i<m;i++){
Queries.push_back(pair<pii,int>(pii(Query1[i].first,Query2[i].first),4*i));
Queries.push_back(pair<pii,int>(pii(Query1[i].first,Query2[i].second),4*i+1));
Queries.push_back(pair<pii,int>(pii(Query1[i].second,Query2[i].first),4*i+2));
Queries.push_back(pair<pii,int>(pii(Query1[i].second,Query2[i].second),4*i+3));
}
sort(Queries.begin(),Queries.end());
int pnt=0;
FT *F=new FT();
F->init(n);
for(int i=0;i<4*m;i++){
while(pnt<Queries[i].first.first){
pnt++;
F->upd(ET[pnt-1].second+1);
}
answer[Queries[i].second]=F->query(Queries[i].first.second);
}
for(int i=0;i<m;i++){
cout<<answer[4*i]+answer[4*i+3]-answer[4*i+1]-answer[4*i+2]<<endl;
}
//cout<<'G'-'A'<<endl;
return 0;
}
Compilation message
selling_rna.cpp: In function 'void insert(Trie*, std::__cxx11::string, int)':
selling_rna.cpp:36:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<s.length();i++){
~^~~~~~~~~~~
selling_rna.cpp: In function 'void DFS1(Trie*)':
selling_rna.cpp:52:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<T->end.size();i++){
~^~~~~~~~~~~~~~
selling_rna.cpp:64:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<T->end.size();i++){
~^~~~~~~~~~~~~~
selling_rna.cpp: In function 'void DFS2(Trie*)':
selling_rna.cpp:73:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<T->end.size();i++){
~^~~~~~~~~~~~~~
selling_rna.cpp:85:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<T->end.size();i++){
~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
8312 KB |
Output is correct |
2 |
Correct |
10 ms |
8408 KB |
Output is correct |
3 |
Correct |
10 ms |
8408 KB |
Output is correct |
4 |
Correct |
9 ms |
8408 KB |
Output is correct |
5 |
Correct |
10 ms |
8612 KB |
Output is correct |
6 |
Correct |
9 ms |
8612 KB |
Output is correct |
7 |
Correct |
9 ms |
8612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
935 ms |
477976 KB |
Output is correct |
2 |
Correct |
922 ms |
477976 KB |
Output is correct |
3 |
Correct |
907 ms |
477976 KB |
Output is correct |
4 |
Correct |
926 ms |
477976 KB |
Output is correct |
5 |
Correct |
1280 ms |
590276 KB |
Output is correct |
6 |
Correct |
1189 ms |
599376 KB |
Output is correct |
7 |
Correct |
300 ms |
599376 KB |
Output is correct |
8 |
Correct |
1084 ms |
599376 KB |
Output is correct |
9 |
Correct |
909 ms |
599376 KB |
Output is correct |
10 |
Correct |
637 ms |
599376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
175 ms |
599376 KB |
Output is correct |
2 |
Correct |
116 ms |
599376 KB |
Output is correct |
3 |
Correct |
159 ms |
599376 KB |
Output is correct |
4 |
Correct |
135 ms |
599376 KB |
Output is correct |
5 |
Correct |
135 ms |
599376 KB |
Output is correct |
6 |
Correct |
156 ms |
599376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
8312 KB |
Output is correct |
2 |
Correct |
10 ms |
8408 KB |
Output is correct |
3 |
Correct |
10 ms |
8408 KB |
Output is correct |
4 |
Correct |
9 ms |
8408 KB |
Output is correct |
5 |
Correct |
10 ms |
8612 KB |
Output is correct |
6 |
Correct |
9 ms |
8612 KB |
Output is correct |
7 |
Correct |
9 ms |
8612 KB |
Output is correct |
8 |
Correct |
935 ms |
477976 KB |
Output is correct |
9 |
Correct |
922 ms |
477976 KB |
Output is correct |
10 |
Correct |
907 ms |
477976 KB |
Output is correct |
11 |
Correct |
926 ms |
477976 KB |
Output is correct |
12 |
Correct |
1280 ms |
590276 KB |
Output is correct |
13 |
Correct |
1189 ms |
599376 KB |
Output is correct |
14 |
Correct |
300 ms |
599376 KB |
Output is correct |
15 |
Correct |
1084 ms |
599376 KB |
Output is correct |
16 |
Correct |
909 ms |
599376 KB |
Output is correct |
17 |
Correct |
637 ms |
599376 KB |
Output is correct |
18 |
Correct |
175 ms |
599376 KB |
Output is correct |
19 |
Correct |
116 ms |
599376 KB |
Output is correct |
20 |
Correct |
159 ms |
599376 KB |
Output is correct |
21 |
Correct |
135 ms |
599376 KB |
Output is correct |
22 |
Correct |
135 ms |
599376 KB |
Output is correct |
23 |
Correct |
156 ms |
599376 KB |
Output is correct |
24 |
Correct |
936 ms |
599376 KB |
Output is correct |
25 |
Correct |
1016 ms |
599376 KB |
Output is correct |
26 |
Correct |
835 ms |
599376 KB |
Output is correct |
27 |
Correct |
818 ms |
599376 KB |
Output is correct |
28 |
Correct |
779 ms |
599376 KB |
Output is correct |
29 |
Correct |
460 ms |
599376 KB |
Output is correct |