#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define pb push_back
#define ar array
#define all(x) x.begin(), x.end()
#define siz(x) (int)x.size()
#define FOR(x, y, z) for(int x = (y); x < (z); x++)
#define ROF(x, z, y) for(int x = (y-1); x >= (z); x--)
#define F0R(x, z) FOR(x, 0, z)
#define R0F(x, z) ROF(x, 0, z)
#define trav(x, y) for(auto&x:y)
using ll = long long;
using vi = vector<int>;
using vl = vector<long long>;
using pii = pair<int, int>;
using vpii = vector<pair<int, int>>;
template<class T> inline bool ckmin(T&a, T b) {return b < a ? a = b, 1 : 0;}
template<class T> inline bool ckmax(T&a, T b) {return b > a ? a = b, 1 : 0;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const char nl = '\n';
const int mxN = 1e5 + 10;
const int MOD = 1e9 + 7;
const long long infLL = 1e18;
int LB, RB;
struct Tree {
using T = int;
using U = vector<int>;
vector<U> vals; int n;
Tree(int n = 0) : vals(2*n), n(n) {}
int qry(int pos){
return lower_bound(all(vals[pos]), RB) - lower_bound(all(vals[pos]), LB);
}
void update(int pos, T val) {
for (pos+=n; pos ; pos/=2){
vals[pos].pb(val);
}
}
void normalize(){
trav(x, vals){
sort(all(x));
}
}
T query(int b, int e) { // query [b, e)
T ans = 0;
for (b += n, e += n; b < e; b /= 2, e /= 2) {
if( b % 2 ) ans += qry(b++);
if( e % 2 ) ans += qry(--e);
}
return ans;
}
};
int n, m;
string s[mxN], p[mxN], q[mxN];
int lba[mxN], uba[mxN], lbb[mxN], ubb[mxN], pos[mxN];
vector<pair<string, int>> tot;
int32_t main(){
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> m;
Tree seg(n);
F0R(i, n){
cin >> s[i];
}
sort(s, s+n);
F0R(i, m){
cin >> p[i] >> q[i];
reverse(all(q[i]));
lba[i] = lower_bound(s, s+n, p[i])- s;
p[i].back()++;
uba[i] = lower_bound(s, s+n, p[i]) -s;
}
F0R(i, n){
reverse(all(s[i]));
tot.pb({s[i], i});
}
sort(all(tot));
sort(s, s+n);
F0R(i, m){
lbb[i] = lower_bound(s, s+n, q[i])-s;
q[i].back()++;
ubb[i] = lower_bound(s, s+n, q[i])-s;
}
F0R(i, n){
pos[tot[i].s] = i;
}
F0R(i, n){
seg.update(i, pos[i]);
}
seg.normalize();
F0R(i, m){
LB = lbb[i], RB = ubb[i];
cout << seg.query(lba[i], uba[i]) << nl;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9728 KB |
Output is correct |
2 |
Correct |
7 ms |
9728 KB |
Output is correct |
3 |
Correct |
7 ms |
9856 KB |
Output is correct |
4 |
Correct |
7 ms |
9856 KB |
Output is correct |
5 |
Correct |
8 ms |
9728 KB |
Output is correct |
6 |
Correct |
7 ms |
9728 KB |
Output is correct |
7 |
Correct |
7 ms |
9728 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
16376 KB |
Output is correct |
2 |
Correct |
45 ms |
16944 KB |
Output is correct |
3 |
Correct |
40 ms |
16632 KB |
Output is correct |
4 |
Correct |
46 ms |
16980 KB |
Output is correct |
5 |
Correct |
36 ms |
14804 KB |
Output is correct |
6 |
Correct |
38 ms |
14804 KB |
Output is correct |
7 |
Correct |
38 ms |
17408 KB |
Output is correct |
8 |
Correct |
52 ms |
19028 KB |
Output is correct |
9 |
Correct |
52 ms |
19028 KB |
Output is correct |
10 |
Correct |
37 ms |
16596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
128 ms |
21100 KB |
Output is correct |
2 |
Correct |
91 ms |
16368 KB |
Output is correct |
3 |
Correct |
116 ms |
18668 KB |
Output is correct |
4 |
Correct |
88 ms |
17512 KB |
Output is correct |
5 |
Correct |
84 ms |
16364 KB |
Output is correct |
6 |
Correct |
108 ms |
18604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9728 KB |
Output is correct |
2 |
Correct |
7 ms |
9728 KB |
Output is correct |
3 |
Correct |
7 ms |
9856 KB |
Output is correct |
4 |
Correct |
7 ms |
9856 KB |
Output is correct |
5 |
Correct |
8 ms |
9728 KB |
Output is correct |
6 |
Correct |
7 ms |
9728 KB |
Output is correct |
7 |
Correct |
7 ms |
9728 KB |
Output is correct |
8 |
Correct |
29 ms |
16376 KB |
Output is correct |
9 |
Correct |
45 ms |
16944 KB |
Output is correct |
10 |
Correct |
40 ms |
16632 KB |
Output is correct |
11 |
Correct |
46 ms |
16980 KB |
Output is correct |
12 |
Correct |
36 ms |
14804 KB |
Output is correct |
13 |
Correct |
38 ms |
14804 KB |
Output is correct |
14 |
Correct |
38 ms |
17408 KB |
Output is correct |
15 |
Correct |
52 ms |
19028 KB |
Output is correct |
16 |
Correct |
52 ms |
19028 KB |
Output is correct |
17 |
Correct |
37 ms |
16596 KB |
Output is correct |
18 |
Correct |
128 ms |
21100 KB |
Output is correct |
19 |
Correct |
91 ms |
16368 KB |
Output is correct |
20 |
Correct |
116 ms |
18668 KB |
Output is correct |
21 |
Correct |
88 ms |
17512 KB |
Output is correct |
22 |
Correct |
84 ms |
16364 KB |
Output is correct |
23 |
Correct |
108 ms |
18604 KB |
Output is correct |
24 |
Correct |
95 ms |
18576 KB |
Output is correct |
25 |
Correct |
154 ms |
19600 KB |
Output is correct |
26 |
Correct |
72 ms |
18320 KB |
Output is correct |
27 |
Correct |
87 ms |
18576 KB |
Output is correct |
28 |
Correct |
493 ms |
40416 KB |
Output is correct |
29 |
Correct |
349 ms |
32608 KB |
Output is correct |