#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define all(v) v.begin(), v.end()
#define pii pair<int, int>
const int N = 1e5 + 5;
struct node {
int sum, l, r;
node(int sum = 0) {}
node(int sum, int l, int r) : sum(sum), l(l), r(r) {}
};
int n, m, ver[N], nNode;
string s[N], rs[N];
vector<pair<string, int>> rev;
node st[(1 << 21) + 5];
void refine(int cur) {
st[cur].sum = st[st[cur].l].sum + st[st[cur].r].sum;
}
int update(int pos, int val, int old, int l = 1, int r = n) {
int cur = ++nNode;
if(l == r) {
st[cur] = node(val, 0, 0);
return cur;
}
int m = l + r >> 1;
if(pos <= m) {
st[cur].l = update(pos, val, st[old].l, l, m);
st[cur].r = st[old].r;
}
else {
st[cur].l = st[old].l;
st[cur].r = update(pos, val, st[old].r, m + 1, r);
}
refine(cur);
return cur;
}
int get(int ver1, int ver2, int u, int v, int l = 1, int r = n) {
if(u > r || v < l) return 0;
if(u <= l && r <= v) return st[ver2].sum - st[ver1].sum;
int m = l + r >> 1;
return get(st[ver1].l, st[ver2].l, u, v, l, m) + get(st[ver1].r, st[ver2].r, u, v, m + 1, r);
}
pii get_range(string x, string *S) {
pii res;
res.first = lower_bound(S + 1, S + n + 1, x) - S;
x.back()++;
res.second = lower_bound(S + 1, S + n + 1, x) - S - 1;
return res;
}
void solve() {
cin >> n >> m;
for(int i = 1; i <= n; ++i) {
cin >> s[i];
}
sort(s + 1, s + n + 1);
for(int i = 1; i <= n; ++i) {
rs[i] = s[i];
reverse(all(rs[i]));
rev.emplace_back(rs[i], i);
}
sort(rs + 1, rs + n + 1);
sort(all(rev));
for(int i = 1; i <= n; ++i) {
auto [_, j] = rev[i - 1];
ver[i] = update(j, 1, ver[i - 1]);
}
for(int i = 1; i <= m; ++i) {
string P, Q; cin >> P >> Q;
auto [l1, r1] = get_range(P, s);
reverse(all(Q));
auto [l2, r2] = get_range(Q, rs);
cout << get(ver[l2 - 1], ver[r2], l1, r1) << '\n';
}
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int test = 1;
// cin >> test;
while(test--) solve();
return 0;
}
// https://oj.uz/problem/view/JOI16_selling_rna
Compilation message
selling_rna.cpp: In function 'int update(int, int, int, int, int)':
selling_rna.cpp:29:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
29 | int m = l + r >> 1;
| ~~^~~
selling_rna.cpp: In function 'int get(int, int, int, int, int, int)':
selling_rna.cpp:45:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
45 | int m = l + r >> 1;
| ~~^~~
selling_rna.cpp: In function 'void solve()':
selling_rna.cpp:71:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
71 | auto [_, j] = rev[i - 1];
| ^
selling_rna.cpp:76:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
76 | auto [l1, r1] = get_range(P, s);
| ^
selling_rna.cpp:78:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
78 | auto [l2, r2] = get_range(Q, rs);
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8536 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
8540 KB |
Output is correct |
4 |
Correct |
2 ms |
8792 KB |
Output is correct |
5 |
Correct |
2 ms |
8632 KB |
Output is correct |
6 |
Correct |
2 ms |
8540 KB |
Output is correct |
7 |
Correct |
2 ms |
8540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
16944 KB |
Output is correct |
2 |
Correct |
21 ms |
17304 KB |
Output is correct |
3 |
Correct |
21 ms |
16988 KB |
Output is correct |
4 |
Correct |
23 ms |
17080 KB |
Output is correct |
5 |
Correct |
18 ms |
14784 KB |
Output is correct |
6 |
Correct |
19 ms |
15032 KB |
Output is correct |
7 |
Correct |
18 ms |
15536 KB |
Output is correct |
8 |
Correct |
27 ms |
17080 KB |
Output is correct |
9 |
Correct |
26 ms |
17252 KB |
Output is correct |
10 |
Correct |
21 ms |
17080 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
58 ms |
21672 KB |
Output is correct |
2 |
Correct |
41 ms |
16432 KB |
Output is correct |
3 |
Correct |
54 ms |
19660 KB |
Output is correct |
4 |
Correct |
41 ms |
16564 KB |
Output is correct |
5 |
Correct |
41 ms |
16368 KB |
Output is correct |
6 |
Correct |
53 ms |
18880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8536 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
8540 KB |
Output is correct |
4 |
Correct |
2 ms |
8792 KB |
Output is correct |
5 |
Correct |
2 ms |
8632 KB |
Output is correct |
6 |
Correct |
2 ms |
8540 KB |
Output is correct |
7 |
Correct |
2 ms |
8540 KB |
Output is correct |
8 |
Correct |
15 ms |
16944 KB |
Output is correct |
9 |
Correct |
21 ms |
17304 KB |
Output is correct |
10 |
Correct |
21 ms |
16988 KB |
Output is correct |
11 |
Correct |
23 ms |
17080 KB |
Output is correct |
12 |
Correct |
18 ms |
14784 KB |
Output is correct |
13 |
Correct |
19 ms |
15032 KB |
Output is correct |
14 |
Correct |
18 ms |
15536 KB |
Output is correct |
15 |
Correct |
27 ms |
17080 KB |
Output is correct |
16 |
Correct |
26 ms |
17252 KB |
Output is correct |
17 |
Correct |
21 ms |
17080 KB |
Output is correct |
18 |
Correct |
58 ms |
21672 KB |
Output is correct |
19 |
Correct |
41 ms |
16432 KB |
Output is correct |
20 |
Correct |
54 ms |
19660 KB |
Output is correct |
21 |
Correct |
41 ms |
16564 KB |
Output is correct |
22 |
Correct |
41 ms |
16368 KB |
Output is correct |
23 |
Correct |
53 ms |
18880 KB |
Output is correct |
24 |
Correct |
46 ms |
17520 KB |
Output is correct |
25 |
Correct |
70 ms |
17764 KB |
Output is correct |
26 |
Correct |
35 ms |
17520 KB |
Output is correct |
27 |
Correct |
43 ms |
17520 KB |
Output is correct |
28 |
Correct |
207 ms |
44320 KB |
Output is correct |
29 |
Correct |
162 ms |
36508 KB |
Output is correct |