#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 query {
int l1, r1, l2, r2;
};
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;
vector<query> qrs;
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:34:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
34 | int m = l + r >> 1;
| ~~^~~
selling_rna.cpp: In function 'int get(int, int, int, int, int, int)':
selling_rna.cpp:50:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
50 | int m = l + r >> 1;
| ~~^~~
selling_rna.cpp: In function 'void solve()':
selling_rna.cpp:76:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
76 | auto [_, j] = rev[i - 1];
| ^
selling_rna.cpp:81:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
81 | auto [l1, r1] = get_range(P, s);
| ^
selling_rna.cpp:83:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
83 | auto [l2, r2] = get_range(Q, rs);
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8536 KB |
Output is correct |
2 |
Correct |
2 ms |
8536 KB |
Output is correct |
3 |
Correct |
2 ms |
8536 KB |
Output is correct |
4 |
Correct |
3 ms |
8540 KB |
Output is correct |
5 |
Correct |
2 ms |
8540 KB |
Output is correct |
6 |
Correct |
2 ms |
8536 KB |
Output is correct |
7 |
Correct |
2 ms |
8792 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
17084 KB |
Output is correct |
2 |
Correct |
22 ms |
17080 KB |
Output is correct |
3 |
Correct |
21 ms |
16988 KB |
Output is correct |
4 |
Correct |
23 ms |
17080 KB |
Output is correct |
5 |
Correct |
17 ms |
14776 KB |
Output is correct |
6 |
Correct |
17 ms |
15032 KB |
Output is correct |
7 |
Correct |
18 ms |
15452 KB |
Output is correct |
8 |
Correct |
28 ms |
17080 KB |
Output is correct |
9 |
Correct |
27 ms |
17080 KB |
Output is correct |
10 |
Correct |
21 ms |
17080 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
60 ms |
21616 KB |
Output is correct |
2 |
Correct |
41 ms |
16336 KB |
Output is correct |
3 |
Correct |
52 ms |
19396 KB |
Output is correct |
4 |
Correct |
45 ms |
16588 KB |
Output is correct |
5 |
Correct |
41 ms |
16432 KB |
Output is correct |
6 |
Correct |
52 ms |
19388 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8536 KB |
Output is correct |
2 |
Correct |
2 ms |
8536 KB |
Output is correct |
3 |
Correct |
2 ms |
8536 KB |
Output is correct |
4 |
Correct |
3 ms |
8540 KB |
Output is correct |
5 |
Correct |
2 ms |
8540 KB |
Output is correct |
6 |
Correct |
2 ms |
8536 KB |
Output is correct |
7 |
Correct |
2 ms |
8792 KB |
Output is correct |
8 |
Correct |
15 ms |
17084 KB |
Output is correct |
9 |
Correct |
22 ms |
17080 KB |
Output is correct |
10 |
Correct |
21 ms |
16988 KB |
Output is correct |
11 |
Correct |
23 ms |
17080 KB |
Output is correct |
12 |
Correct |
17 ms |
14776 KB |
Output is correct |
13 |
Correct |
17 ms |
15032 KB |
Output is correct |
14 |
Correct |
18 ms |
15452 KB |
Output is correct |
15 |
Correct |
28 ms |
17080 KB |
Output is correct |
16 |
Correct |
27 ms |
17080 KB |
Output is correct |
17 |
Correct |
21 ms |
17080 KB |
Output is correct |
18 |
Correct |
60 ms |
21616 KB |
Output is correct |
19 |
Correct |
41 ms |
16336 KB |
Output is correct |
20 |
Correct |
52 ms |
19396 KB |
Output is correct |
21 |
Correct |
45 ms |
16588 KB |
Output is correct |
22 |
Correct |
41 ms |
16432 KB |
Output is correct |
23 |
Correct |
52 ms |
19388 KB |
Output is correct |
24 |
Correct |
41 ms |
17520 KB |
Output is correct |
25 |
Correct |
71 ms |
17740 KB |
Output is correct |
26 |
Correct |
33 ms |
17608 KB |
Output is correct |
27 |
Correct |
44 ms |
17520 KB |
Output is correct |
28 |
Correct |
207 ms |
46260 KB |
Output is correct |
29 |
Correct |
160 ms |
40280 KB |
Output is correct |