#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int B = 500;
int n, q;
ll s[100001], t[100001], sum_compressed[100001];
vector<ll> has_sum;
pair<ll, int> by_s[100001], by_t[100001];
tuple<ll, ll, ll, int> queries[100001];
int bit[100001];
int ans[100001];
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];
has_sum.push_back(s[i] + t[i]);
by_s[i] = {s[i], i};
by_t[i] = {t[i], i};
}
for (int i = 0; i < q; i++) {
ll a, b, c;
cin >> a >> b >> c;
queries[i] = {a, b, c, i};
}
sort(has_sum.begin(), has_sum.end());
has_sum.erase(unique(has_sum.begin(), has_sum.end()), has_sum.end());
for (int i = 0; i < n; i++) {
sum_compressed[i] = lower_bound(has_sum.begin(), has_sum.end(), s[i] + t[i]) - has_sum.begin() + 1;
}
sort(by_s, by_s + n);
sort(by_t, by_t + n);
sort(queries, queries + q, greater<tuple<ll, ll, ll, int>>());
map<int, int> good_cnt;
for (int i = 0, sptr = n, tptr = n; i < q; i += B) {
sort(queries + i, queries + min(q, i + B), [](tuple<ll, ll, ll, int> A, tuple<ll, ll, ll, int> B) {
return get<1>(A) > get<1>(B);
});
for (int j = i; j < min(q, i + B); j++) {
ll a, b, c, idx;
tie(a, b, c, idx) = queries[j];
while (sptr && by_s[sptr - 1].first >= a) {
good_cnt[by_s[sptr - 1].second]++;
if (good_cnt[by_s[sptr - 1].second] == 2) {
update(sum_compressed[by_s[sptr - 1].second], 1);
}
sptr--;
}
while (sptr != n && by_s[sptr].first < a) {
good_cnt[by_s[sptr].second]--;
if (good_cnt[by_s[sptr].second] == 1) {
update(sum_compressed[by_s[sptr].second], -1);
}
sptr++;
}
while (tptr && by_t[tptr - 1].first >= b) {
good_cnt[by_t[tptr - 1].second]++;
if (good_cnt[by_t[tptr - 1].second] == 2) {
update(sum_compressed[by_t[tptr - 1].second], 1);
}
tptr--;
}
while (tptr != n && by_t[tptr].first < b) {
good_cnt[by_t[tptr].second]--;
if (good_cnt[by_t[tptr].second] == 1) {
update(sum_compressed[by_t[tptr].second], -1);
}
tptr++;
}
int ub = lower_bound(has_sum.begin(), has_sum.end(), c) - has_sum.begin() + 1;
ans[idx] = query(ub, n);
}
}
for (int i = 0; i < q; i++) cout << ans[i] << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
85 ms |
876 KB |
Output is correct |
8 |
Correct |
90 ms |
876 KB |
Output is correct |
9 |
Correct |
86 ms |
876 KB |
Output is correct |
10 |
Correct |
80 ms |
876 KB |
Output is correct |
11 |
Correct |
56 ms |
876 KB |
Output is correct |
12 |
Correct |
19 ms |
880 KB |
Output is correct |
13 |
Correct |
84 ms |
876 KB |
Output is correct |
14 |
Correct |
88 ms |
876 KB |
Output is correct |
15 |
Correct |
88 ms |
1004 KB |
Output is correct |
16 |
Correct |
7 ms |
876 KB |
Output is correct |
17 |
Correct |
43 ms |
876 KB |
Output is correct |
18 |
Correct |
6 ms |
876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3064 ms |
15216 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3064 ms |
15216 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
85 ms |
876 KB |
Output is correct |
8 |
Correct |
90 ms |
876 KB |
Output is correct |
9 |
Correct |
86 ms |
876 KB |
Output is correct |
10 |
Correct |
80 ms |
876 KB |
Output is correct |
11 |
Correct |
56 ms |
876 KB |
Output is correct |
12 |
Correct |
19 ms |
880 KB |
Output is correct |
13 |
Correct |
84 ms |
876 KB |
Output is correct |
14 |
Correct |
88 ms |
876 KB |
Output is correct |
15 |
Correct |
88 ms |
1004 KB |
Output is correct |
16 |
Correct |
7 ms |
876 KB |
Output is correct |
17 |
Correct |
43 ms |
876 KB |
Output is correct |
18 |
Correct |
6 ms |
876 KB |
Output is correct |
19 |
Execution timed out |
3064 ms |
15216 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |