#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MASK(i) (1LL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
#define block_of_code if(true)
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll gcd(ll a, ll b){return __gcd(a, b);}
ll lcm(ll a, ll b){return a / gcd(a, b) * b;}
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ll mask){return __builtin_popcountll(mask);}
int ctz(ull mask){return __builtin_ctzll(mask);}
int logOf(ull mask){return 63 - __builtin_clzll(mask);}
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
double rngesus_d(double l, double r){
double cur = rngesus(0, MASK(60) - 1);
cur /= MASK(60) - 1;
return l + cur * (r - l);
}
template <class T1, class T2>
bool maximize(T1 &a, T2 b){
if (a < b) {a = b; return true;}
return false;
}
template <class T1, class T2>
bool minimize(T1 &a, T2 b){
if (a > b) {a = b; return true;}
return false;
}
template <class T>
void printArr(T container, string separator = " ", string finish = "\n", ostream &out = cout){
for(auto item: container) out << item << separator;
out << finish;
}
template <class T>
void remove_dup(vector<T> &a){
sort(ALL(a));
a.resize(unique(ALL(a)) - a.begin());
}
struct FenwickTree{
int n;
vector<int> a;
FenwickTree(int _n){
n = _n;
a.resize(n+1);
}
void update(int i, int v){
while(i <= n){
a[i] += v;
i += LASTBIT(i);
}
}
int get(int i){
int ans = 0;
while(i > 0){
ans += a[i];
i -= LASTBIT(i);
}
return ans;
}
};
bool cmp(const pair<string, int> &x, const pair<string, int> &y){
// cout << x.first << " " << x.second << "\n";
// cout << y.first << " " << y.second << "\n";
int len = min(y.first.size(), x.first.size());
if (x.second == 0){
// cout << (x.first < y.first.substr(0, len)) << "\n";
return x.first < y.first.substr(0, len);
}
else {
// cout << (x.first < y.first.substr(0, len)) << "\n";
return x.first.substr(0, len) < y.first;
}
}
const int N = 1e5 + 69;
int ans[N];
vector<array<int, 3>> query[N];
int main(void){
ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
clock_t start = clock();
int n, q; cin >> n >> q;
vector<pair<string, int>> s(n);
for(int i = 0; i<n; ++i)
cin >> s[i].first;
sort(ALL(s));
for(int i = 0; i<n; ++i) s[i].second = i + 1;
vector<pair<string, int>> _s = s;
for(auto &i: _s) reverse(ALL(i.first));
sort(ALL(_s));
for(int i = 1; i<=q; ++i){
string pref, suff;
cin >> pref >> suff;
reverse(ALL(suff));
int l1 = lower_bound(ALL(s), make_pair(pref, 0), cmp) - s.begin() + 1;
int r1 = upper_bound(ALL(s), make_pair(pref, 0), cmp) - s.begin();
int l2 = lower_bound(ALL(_s), make_pair(suff, 0), cmp) - _s.begin() + 1;
int r2 = upper_bound(ALL(_s), make_pair(suff, 0), cmp) - _s.begin();
// cout << l1 << " " << r1 << " " << l2 << " " << r2 << "\n";
if (l1 <= r1 && l2 <= r2){
query[l1-1].push_back({{l2, r2, -i}});
query[r1].push_back({{l2, r2, i}});
}
}
vector<int> perm(n+1);
for(int i = 0; i<n; ++i) perm[_s[i].second] = i + 1;
FenwickTree bit(n);
for(int i = 1; i<=n; ++i){
bit.update(perm[i], 1);
for(array<int, 3> j: query[i]){
int cur = bit.get(j[1]) - bit.get(j[0] - 1);
if (j[2] < 0) ans[-j[2]] -= cur;
else ans[j[2]] += cur;
}
}
for(int i = 1; i<=q; ++i) cout << ans[i] << "\n";
cerr << "Time elapsed: " << clock() - start << " ms\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2644 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
7260 KB |
Output is correct |
2 |
Correct |
22 ms |
7516 KB |
Output is correct |
3 |
Correct |
22 ms |
7260 KB |
Output is correct |
4 |
Correct |
24 ms |
7512 KB |
Output is correct |
5 |
Correct |
20 ms |
5960 KB |
Output is correct |
6 |
Correct |
21 ms |
5980 KB |
Output is correct |
7 |
Correct |
21 ms |
6232 KB |
Output is correct |
8 |
Correct |
37 ms |
7508 KB |
Output is correct |
9 |
Correct |
32 ms |
7516 KB |
Output is correct |
10 |
Correct |
22 ms |
7328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
74 ms |
8724 KB |
Output is correct |
2 |
Correct |
42 ms |
6736 KB |
Output is correct |
3 |
Correct |
56 ms |
7844 KB |
Output is correct |
4 |
Correct |
47 ms |
6992 KB |
Output is correct |
5 |
Correct |
42 ms |
6488 KB |
Output is correct |
6 |
Correct |
55 ms |
7744 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2644 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
16 ms |
7260 KB |
Output is correct |
9 |
Correct |
22 ms |
7516 KB |
Output is correct |
10 |
Correct |
22 ms |
7260 KB |
Output is correct |
11 |
Correct |
24 ms |
7512 KB |
Output is correct |
12 |
Correct |
20 ms |
5960 KB |
Output is correct |
13 |
Correct |
21 ms |
5980 KB |
Output is correct |
14 |
Correct |
21 ms |
6232 KB |
Output is correct |
15 |
Correct |
37 ms |
7508 KB |
Output is correct |
16 |
Correct |
32 ms |
7516 KB |
Output is correct |
17 |
Correct |
22 ms |
7328 KB |
Output is correct |
18 |
Correct |
74 ms |
8724 KB |
Output is correct |
19 |
Correct |
42 ms |
6736 KB |
Output is correct |
20 |
Correct |
56 ms |
7844 KB |
Output is correct |
21 |
Correct |
47 ms |
6992 KB |
Output is correct |
22 |
Correct |
42 ms |
6488 KB |
Output is correct |
23 |
Correct |
55 ms |
7744 KB |
Output is correct |
24 |
Correct |
52 ms |
8788 KB |
Output is correct |
25 |
Correct |
90 ms |
10176 KB |
Output is correct |
26 |
Correct |
33 ms |
8280 KB |
Output is correct |
27 |
Correct |
52 ms |
8672 KB |
Output is correct |
28 |
Correct |
213 ms |
20820 KB |
Output is correct |
29 |
Correct |
189 ms |
15188 KB |
Output is correct |