/*
JOI 2019 Examination
- The main idea is to use MO's algorithm
- Keep two pointers: one on S and one on T
- We can use an unordered map to check for students that satisfy both A and B
- Next, we can coordinate compress the sum of scores and then use a BIT to query
the number of students satisfying A and B that also satisfy C
- Complexity: O(N (sqrt N + log N))
*/
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
const int N = 100000, BCKT = 750;
int n, q;
int s[N], t[N], sums[N], sum_ptr[N];
pair<int, int> by_s[N], by_t[N];
vector<tuple<int, int, int, int>> queries[N / BCKT + 1];
int bit[N + 1];
int ans[N];
void update(int pos, int val) {
for (; pos <= n; pos += pos & -pos) bit[pos] += val;
}
int query(int l, int r) {
int ans = 0;
for (; r; r -= r & -r) ans += bit[r];
for (l--; l; l -= l & -l) ans -= bit[l];
return ans;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> q;
for (int i = 0; i < n; i++) {
cin >> s[i] >> t[i];
sums[i] = s[i] + t[i];
by_s[i] = {s[i], i};
by_t[i] = {t[i], i};
}
sort(sums, sums + n);
for (int i = 0; i < n; i++) {
sum_ptr[i] = lower_bound(sums, sums + n, s[i] + t[i]) - sums + 1;
}
sort(by_s, by_s + n);
sort(by_t, by_t + n);
for (int i = 0; i < q; i++) {
int a, b, c;
cin >> a >> b >> c;
a = lower_bound(by_s, by_s + n, make_pair(a, 0)) - by_s;
b = lower_bound(by_t, by_t + n, make_pair(b, 0)) - by_t;
c = lower_bound(sums, sums + n, c) - sums + 1;
queries[a / BCKT].push_back(tuple<int, int, int, int>(a, b, c, i));
}
gp_hash_table<int, int> good_cnt;
for (int i = N / BCKT, sptr = n, tptr = n; ~i; i--) {
sort(queries[i].begin(), queries[i].end(), [](tuple<int, int, int, int> A, tuple<int, int, int, int> B) {
return get<1>(A) > get<1>(B);
});
for (tuple<int, int, int, int> j : queries[i]) {
int a, b, c, idx;
tie(a, b, c, idx) = j;
while (sptr > a) {
good_cnt[by_s[sptr - 1].second]++;
if (good_cnt[by_s[sptr - 1].second] == 2) {
update(sum_ptr[by_s[sptr - 1].second], 1);
}
sptr--;
}
while (sptr < a) {
good_cnt[by_s[sptr].second]--;
if (good_cnt[by_s[sptr].second] == 1) {
update(sum_ptr[by_s[sptr].second], -1);
}
sptr++;
}
while (tptr > b) {
good_cnt[by_t[tptr - 1].second]++;
if (good_cnt[by_t[tptr - 1].second] == 2) {
update(sum_ptr[by_t[tptr - 1].second], 1);
}
tptr--;
}
while (tptr < b) {
good_cnt[by_t[tptr].second]--;
if (good_cnt[by_t[tptr].second] == 1) {
update(sum_ptr[by_t[tptr].second], -1);
}
tptr++;
}
ans[idx] = query(c, n);
}
}
for (int i = 0; i < q; i++) cout << ans[i] << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
9 ms |
668 KB |
Output is correct |
8 |
Correct |
9 ms |
748 KB |
Output is correct |
9 |
Correct |
9 ms |
748 KB |
Output is correct |
10 |
Correct |
10 ms |
724 KB |
Output is correct |
11 |
Correct |
8 ms |
636 KB |
Output is correct |
12 |
Correct |
7 ms |
724 KB |
Output is correct |
13 |
Correct |
9 ms |
724 KB |
Output is correct |
14 |
Correct |
9 ms |
744 KB |
Output is correct |
15 |
Correct |
9 ms |
724 KB |
Output is correct |
16 |
Correct |
3 ms |
724 KB |
Output is correct |
17 |
Correct |
8 ms |
748 KB |
Output is correct |
18 |
Correct |
2 ms |
724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
938 ms |
11744 KB |
Output is correct |
2 |
Correct |
946 ms |
11800 KB |
Output is correct |
3 |
Correct |
945 ms |
11772 KB |
Output is correct |
4 |
Correct |
746 ms |
11832 KB |
Output is correct |
5 |
Correct |
139 ms |
11188 KB |
Output is correct |
6 |
Correct |
100 ms |
11064 KB |
Output is correct |
7 |
Correct |
470 ms |
11300 KB |
Output is correct |
8 |
Correct |
529 ms |
11760 KB |
Output is correct |
9 |
Correct |
499 ms |
11288 KB |
Output is correct |
10 |
Correct |
93 ms |
10740 KB |
Output is correct |
11 |
Correct |
687 ms |
11496 KB |
Output is correct |
12 |
Correct |
52 ms |
11712 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
938 ms |
11744 KB |
Output is correct |
2 |
Correct |
946 ms |
11800 KB |
Output is correct |
3 |
Correct |
945 ms |
11772 KB |
Output is correct |
4 |
Correct |
746 ms |
11832 KB |
Output is correct |
5 |
Correct |
139 ms |
11188 KB |
Output is correct |
6 |
Correct |
100 ms |
11064 KB |
Output is correct |
7 |
Correct |
470 ms |
11300 KB |
Output is correct |
8 |
Correct |
529 ms |
11760 KB |
Output is correct |
9 |
Correct |
499 ms |
11288 KB |
Output is correct |
10 |
Correct |
93 ms |
10740 KB |
Output is correct |
11 |
Correct |
687 ms |
11496 KB |
Output is correct |
12 |
Correct |
52 ms |
11712 KB |
Output is correct |
13 |
Correct |
956 ms |
11812 KB |
Output is correct |
14 |
Correct |
961 ms |
11728 KB |
Output is correct |
15 |
Correct |
928 ms |
11800 KB |
Output is correct |
16 |
Correct |
753 ms |
11800 KB |
Output is correct |
17 |
Correct |
155 ms |
11280 KB |
Output is correct |
18 |
Correct |
112 ms |
11384 KB |
Output is correct |
19 |
Correct |
953 ms |
11800 KB |
Output is correct |
20 |
Correct |
969 ms |
11816 KB |
Output is correct |
21 |
Correct |
636 ms |
11648 KB |
Output is correct |
22 |
Correct |
92 ms |
10724 KB |
Output is correct |
23 |
Correct |
707 ms |
11612 KB |
Output is correct |
24 |
Correct |
51 ms |
11616 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
9 ms |
668 KB |
Output is correct |
8 |
Correct |
9 ms |
748 KB |
Output is correct |
9 |
Correct |
9 ms |
748 KB |
Output is correct |
10 |
Correct |
10 ms |
724 KB |
Output is correct |
11 |
Correct |
8 ms |
636 KB |
Output is correct |
12 |
Correct |
7 ms |
724 KB |
Output is correct |
13 |
Correct |
9 ms |
724 KB |
Output is correct |
14 |
Correct |
9 ms |
744 KB |
Output is correct |
15 |
Correct |
9 ms |
724 KB |
Output is correct |
16 |
Correct |
3 ms |
724 KB |
Output is correct |
17 |
Correct |
8 ms |
748 KB |
Output is correct |
18 |
Correct |
2 ms |
724 KB |
Output is correct |
19 |
Correct |
938 ms |
11744 KB |
Output is correct |
20 |
Correct |
946 ms |
11800 KB |
Output is correct |
21 |
Correct |
945 ms |
11772 KB |
Output is correct |
22 |
Correct |
746 ms |
11832 KB |
Output is correct |
23 |
Correct |
139 ms |
11188 KB |
Output is correct |
24 |
Correct |
100 ms |
11064 KB |
Output is correct |
25 |
Correct |
470 ms |
11300 KB |
Output is correct |
26 |
Correct |
529 ms |
11760 KB |
Output is correct |
27 |
Correct |
499 ms |
11288 KB |
Output is correct |
28 |
Correct |
93 ms |
10740 KB |
Output is correct |
29 |
Correct |
687 ms |
11496 KB |
Output is correct |
30 |
Correct |
52 ms |
11712 KB |
Output is correct |
31 |
Correct |
956 ms |
11812 KB |
Output is correct |
32 |
Correct |
961 ms |
11728 KB |
Output is correct |
33 |
Correct |
928 ms |
11800 KB |
Output is correct |
34 |
Correct |
753 ms |
11800 KB |
Output is correct |
35 |
Correct |
155 ms |
11280 KB |
Output is correct |
36 |
Correct |
112 ms |
11384 KB |
Output is correct |
37 |
Correct |
953 ms |
11800 KB |
Output is correct |
38 |
Correct |
969 ms |
11816 KB |
Output is correct |
39 |
Correct |
636 ms |
11648 KB |
Output is correct |
40 |
Correct |
92 ms |
10724 KB |
Output is correct |
41 |
Correct |
707 ms |
11612 KB |
Output is correct |
42 |
Correct |
51 ms |
11616 KB |
Output is correct |
43 |
Correct |
967 ms |
11756 KB |
Output is correct |
44 |
Correct |
951 ms |
11856 KB |
Output is correct |
45 |
Correct |
960 ms |
11612 KB |
Output is correct |
46 |
Correct |
751 ms |
11820 KB |
Output is correct |
47 |
Correct |
160 ms |
11160 KB |
Output is correct |
48 |
Correct |
102 ms |
10920 KB |
Output is correct |
49 |
Correct |
440 ms |
11108 KB |
Output is correct |
50 |
Correct |
593 ms |
11636 KB |
Output is correct |
51 |
Correct |
513 ms |
11260 KB |
Output is correct |
52 |
Correct |
114 ms |
10724 KB |
Output is correct |
53 |
Correct |
695 ms |
11604 KB |
Output is correct |