This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;
#define debug(s) cout<< #s <<" = "<< s <<endl
#define all(v) (v).begin(), (v).end()
#define KeepUnique(v) (v).erase( unique(all(v)), v.end() )
#define MEMSET(a,val) memset(a, val, sizeof a)
#define PB push_back
#define endl '\n'
typedef long long ll;
inline int myrand(int l, int r) {
int ret = rand(); ret <<= 15; ret ^= rand();
if(ret < 0) ret = -ret; ret %= (r-l+1); ret += l;
return ret;
}
template <typename F, typename S>
ostream& operator << (ostream& os, const pair< F, S>& p) {
return os<<"(" <<p.first<<", "<<p.second<<")"; }
typedef pair<int, int> ii;
template<typename T> using min_pq =
priority_queue<T, vector<T>, greater<T> >;
//int dx[] = {-1, +0, +1, +0};
//int dy[] = {+0, +1, +0, -1};
int n, m;
vector<string> ok, rev;
vector<int> v;
namespace mst {
vector<int> t[100005 * 4];
void build(int u, int b, int e, vector<int> &vec) {
for(int i = b; i <= e; i++) t[u].push_back(vec[i]);
sort(all(t[u]));
if(b == e) return;
int mid = b + e >> 1;
build(u << 1, b, mid, vec);
build(u << 1 | 1, mid + 1, e, vec);
}
int query(int u, int b, int e, int i, int j, int l, int r) {
if(b > j or e < i) return 0;
if(i <= b and e <= j) return upper_bound(all(t[u]), r) - lower_bound(all(t[u]), l);
int mid = b + e >> 1;
return query(u << 1, b, mid, i, j, l, r) + query(u << 1 | 1, mid + 1, e, i, j, l, r);
}
int query(int i, int j, int l, int r) {
if(i > j or l > r) return 0;
return query(1, 0, n - 1, i, j, l, r);
}
};
bool IsPrefix(string &s, string &pre) {
if(pre.size() > s.size()) return false;
for(int i = 0; i < pre.size(); i++) {
if(pre[i] != s[i]) return false;
} return true;
}
int32_t main () {
// freopen("in", "r", stdin);
cin >> n >> m;
ok.resize(n), rev.resize(n);
for(string &b : ok) cin >> b;
sort(all(ok));
for(int i = 0; i < n; i++) {
rev[i] = ok[i];
reverse(all(rev[i]));
v.push_back(i);
}
sort(all(v), [](int x, int y){return rev[x] < rev[y];});
sort(all(rev));
mst::build(1, 0, n - 1, v);
while(m--) {
int lo, hi;
string pre, suf;
cin >> pre >> suf;
reverse(all(suf));
int l = lower_bound(all(ok), pre) - ok.begin(), r;
if(l >= ok.size() or !IsPrefix(ok[l], pre)) r = l - 1;
else {
lo = l, hi = ok.size() - 1;
while(lo <= hi) {
int mid = lo + hi >> 1;
if(IsPrefix(ok[mid], pre)) {
lo = mid + 1;
r = mid;
} else {
hi = mid - 1;
}
}
}
int lit = lower_bound(all(rev), suf) - rev.begin(), rit;
if(lit >= ok.size() or !IsPrefix(rev[lit], suf)) rit = lit - 1;
else {
lo = lit, hi = rev.size() - 1;
while(lo <= hi) {
int mid = lo + hi >> 1;
if(IsPrefix(rev[mid], suf)) {
lo = mid + 1;
rit = mid;
} else {
hi = mid - 1;
}
}
}
cout << mst::query(lit, rit, l, r) << endl;
}
}
Compilation message (stderr)
selling_rna.cpp: In function 'int myrand(int, int)':
selling_rna.cpp:17:2: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
if(ret < 0) ret = -ret; ret %= (r-l+1); ret += l;
^~
selling_rna.cpp:17:26: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
if(ret < 0) ret = -ret; ret %= (r-l+1); ret += l;
^~~
selling_rna.cpp: In function 'void mst::build(int, int, int, std::vector<int>&)':
selling_rna.cpp:42:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid = b + e >> 1;
~~^~~
selling_rna.cpp: In function 'int mst::query(int, int, int, int, int, int, int)':
selling_rna.cpp:49:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid = b + e >> 1;
~~^~~
selling_rna.cpp: In function 'bool IsPrefix(std::__cxx11::string&, std::__cxx11::string&)':
selling_rna.cpp:60:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < pre.size(); i++) {
~~^~~~~~~~~~~~
selling_rna.cpp: In function 'int32_t main()':
selling_rna.cpp:87:8: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(l >= ok.size() or !IsPrefix(ok[l], pre)) r = l - 1;
~~^~~~~~~~~~~~
selling_rna.cpp:91:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid = lo + hi >> 1;
~~~^~~~
selling_rna.cpp:101:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(lit >= ok.size() or !IsPrefix(rev[lit], suf)) rit = lit - 1;
~~~~^~~~~~~~~~~~
selling_rna.cpp:105:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid = lo + hi >> 1;
~~~^~~~
selling_rna.cpp:54:39: warning: 'rit' may be used uninitialized in this function [-Wmaybe-uninitialized]
return query(1, 0, n - 1, i, j, l, r);
^
selling_rna.cpp:100:55: note: 'rit' was declared here
int lit = lower_bound(all(rev), suf) - rev.begin(), rit;
^~~
selling_rna.cpp:54:39: warning: 'r' may be used uninitialized in this function [-Wmaybe-uninitialized]
return query(1, 0, n - 1, i, j, l, r);
^
selling_rna.cpp:86:51: note: 'r' was declared here
int l = lower_bound(all(ok), pre) - ok.begin(), r;
^
# | 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... |