#include<bits/stdc++.h>
#define ll int
#define pb push_back
#define mp make_pair
#define ld long double
#define F first
#define S second
#define pii pair<ll,ll>
using namespace :: std;
const ll mod=1e9+7;
const ll maxn=1e5+500;
const ll inf=1e9+900;
ll A[maxn];
ll ans[maxn];
vector<ll> seg[4*maxn];
ll l[maxn];
ll r[maxn];
ll ls[maxn];
ll rs[maxn];
void bild(ll l,ll r,ll node){
for(ll i=l;i<r;i++){
seg[node].pb(A[i]);
}
sort(seg[node].begin(),seg[node].end());
if(l+1==r)return;
ll mid=(l+r)/2;
bild(l,mid,2*node);
bild(mid,r,2*node+1);
}
ll find_ans(ll L,ll R,ll node,ll x){
if(ls[x]<=L && R<=rs[x]){
auto it=lower_bound(seg[node].begin(),seg[node].end(),l[x]);
auto itt=lower_bound(seg[node].begin(),seg[node].end(),r[x]);
return itt-it;
}
if(R<=ls[x] || rs[x]<=L){
return 0;
}
ll mid=(L+R)/2;
return find_ans(L,mid,2*node,x)+find_ans(mid,R,2*node+1,x);
}
string s[maxn];
pair<string,ll> ru[maxn];
string re[maxn];
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
ll n,q;
cin>>n>>q;
for(ll i=0;i<n;i++){
cin>>s[i];
}
sort(s,s+n);
for(ll i=0;i<n;i++){
ru[i]=mp(s[i],i);
reverse(ru[i].F.begin(),ru[i].F.end());
}
sort(ru,ru+n);
for(ll i=0;i<n;i++){
re[i]=ru[i].F;
A[i]=ru[i].S;
}
for(ll i=0;i<q;i++){
string a,b;
cin>>a>>b;
l[i]=lower_bound(s,s+n,a)-s;
a.back()++;
r[i]=lower_bound(s,s+n,a)-s;
reverse(b.begin(),b.end());
ls[i]=lower_bound(re,re+n,b)-re;
b.back()++;
rs[i]=lower_bound(re,re+n,b)-re;
}
bild(0,n,1);
for(ll i=0;i<q;i++){
ans[i]=find_ans(0,n,1,i);
}
for(ll i=0;i<q;i++){
cout<<ans[i]<<endl;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
19968 KB |
Output is correct |
2 |
Correct |
17 ms |
19968 KB |
Output is correct |
3 |
Correct |
17 ms |
20096 KB |
Output is correct |
4 |
Correct |
18 ms |
20096 KB |
Output is correct |
5 |
Correct |
18 ms |
20096 KB |
Output is correct |
6 |
Correct |
17 ms |
19968 KB |
Output is correct |
7 |
Correct |
17 ms |
20096 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
26360 KB |
Output is correct |
2 |
Correct |
59 ms |
26988 KB |
Output is correct |
3 |
Correct |
54 ms |
26592 KB |
Output is correct |
4 |
Correct |
59 ms |
26848 KB |
Output is correct |
5 |
Correct |
50 ms |
24616 KB |
Output is correct |
6 |
Correct |
51 ms |
24696 KB |
Output is correct |
7 |
Correct |
51 ms |
24824 KB |
Output is correct |
8 |
Correct |
67 ms |
26900 KB |
Output is correct |
9 |
Correct |
66 ms |
26872 KB |
Output is correct |
10 |
Correct |
50 ms |
26872 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
165 ms |
27768 KB |
Output is correct |
2 |
Correct |
114 ms |
24568 KB |
Output is correct |
3 |
Correct |
148 ms |
27144 KB |
Output is correct |
4 |
Correct |
125 ms |
26124 KB |
Output is correct |
5 |
Correct |
121 ms |
24572 KB |
Output is correct |
6 |
Correct |
157 ms |
27124 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
19968 KB |
Output is correct |
2 |
Correct |
17 ms |
19968 KB |
Output is correct |
3 |
Correct |
17 ms |
20096 KB |
Output is correct |
4 |
Correct |
18 ms |
20096 KB |
Output is correct |
5 |
Correct |
18 ms |
20096 KB |
Output is correct |
6 |
Correct |
17 ms |
19968 KB |
Output is correct |
7 |
Correct |
17 ms |
20096 KB |
Output is correct |
8 |
Correct |
40 ms |
26360 KB |
Output is correct |
9 |
Correct |
59 ms |
26988 KB |
Output is correct |
10 |
Correct |
54 ms |
26592 KB |
Output is correct |
11 |
Correct |
59 ms |
26848 KB |
Output is correct |
12 |
Correct |
50 ms |
24616 KB |
Output is correct |
13 |
Correct |
51 ms |
24696 KB |
Output is correct |
14 |
Correct |
51 ms |
24824 KB |
Output is correct |
15 |
Correct |
67 ms |
26900 KB |
Output is correct |
16 |
Correct |
66 ms |
26872 KB |
Output is correct |
17 |
Correct |
50 ms |
26872 KB |
Output is correct |
18 |
Correct |
165 ms |
27768 KB |
Output is correct |
19 |
Correct |
114 ms |
24568 KB |
Output is correct |
20 |
Correct |
148 ms |
27144 KB |
Output is correct |
21 |
Correct |
125 ms |
26124 KB |
Output is correct |
22 |
Correct |
121 ms |
24572 KB |
Output is correct |
23 |
Correct |
157 ms |
27124 KB |
Output is correct |
24 |
Correct |
123 ms |
32196 KB |
Output is correct |
25 |
Correct |
191 ms |
33016 KB |
Output is correct |
26 |
Correct |
91 ms |
32008 KB |
Output is correct |
27 |
Correct |
129 ms |
32092 KB |
Output is correct |
28 |
Correct |
517 ms |
47760 KB |
Output is correct |
29 |
Correct |
416 ms |
39156 KB |
Output is correct |