This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |