#include "bits/stdc++.h"
using namespace std;
const int maxn = 1e5 + 5;
const int inf = 1e9;
int n, q, s[maxn], t[maxn];
int a[maxn], b[maxn];
int ord[maxn];
vector<int> bit[maxn];
int ans[maxn];
struct query {
int x, y, z, id;
bool operator<(const query &o) {
return z > o.z;
}
} que[maxn];
vector<int> cx, cy;
#define gb(x) (x) & (-x)
struct fenwick_tree {
int n;
vector<int> bit;
fenwick_tree() {}
fenwick_tree(int n) : n(n), bit(n + 5) {}
inline void update(int x, int val) {
for(int i = x; i <= n; i += gb(i)) bit[i] = bit[i] + val;
}
inline int get(int x) {
int ans = 0;
for(int i = x; i > 0; i -= gb(i)) ans = ans + bit[i];
return ans;
}
} tree[maxn];
vector<int> sz[maxn];
struct segment_tree {
int n;
segment_tree() {}
segment_tree(int n) : n(n) {}
inline void resize_tree(int pos, int fen_pos) {
for(int i = pos; i <= n; i += gb(i)) {
sz[i].push_back(fen_pos);
}
}
inline void refine() {
for(int ind = 1; ind <= n; ++ind) {
if(sz[ind].empty()) continue;
sort(sz[ind].begin(), sz[ind].end());
sz[ind].erase(unique(sz[ind].begin(), sz[ind].end()), sz[ind].end());
tree[ind] = fenwick_tree((int)sz[ind].size() + 2);
}
}
inline void update(int pos, int fen_pos, int val) {
int p;
for(int i = pos; i <= n; i += gb(i)) {
p = lower_bound(sz[i].begin(), sz[i].end(), fen_pos) - sz[i].begin() + 1;
tree[i].update(p, val);
}
}
inline int get(int pos, int fen_pos) {
int ans = 0;
int p;
for(int i = pos; i > 0; i -= gb(i)) {
p = upper_bound(sz[i].begin(), sz[i].end(), fen_pos) - sz[i].begin();
if(p == 0) continue;
ans = ans + tree[i].get(p);
}
return ans;
}
int get(int l, int r, int pos) {
return get(r, pos) - get(l - 1, pos);
}
} st;
void solve() {
cin >> n >> q;
for(int i = 1; i <= n; ++i) {
cin >> s[i] >> t[i];
}
for(int i = 1; i <= q; ++i) {
cin >> que[i].x >> que[i].y >> que[i].z;
que[i].id = i;
}
iota(ord + 1, ord + n + 1, 1);
sort(ord + 1, ord + n + 1, [](const int &x, const int &y) -> bool {
return (s[x] + t[x]) > (s[y] + t[y]);
});
for(int i = 1; i <= n; ++i) {
cx.push_back(s[i]);
cy.push_back(t[i]);
}
sort(cx.begin(), cx.end());
sort(cy.begin(), cy.end());
cx.erase(unique(cx.begin(), cx.end()), cx.end());
cy.erase(unique(cy.begin(), cy.end()), cy.end());
st = segment_tree((int)cx.size());
for(int i = 1; i <= n; ++i) {
a[i] = lower_bound(cx.begin(), cx.end(), s[i]) - cx.begin() + 1;
b[i] = lower_bound(cy.begin(), cy.end(), t[i]) - cy.begin() + 1;
st.resize_tree(a[i], t[i]);
}
st.refine();
sort(que + 1, que + q + 1);
int j = 1;
for(int i = 1; i <= q; ++i) {
while(j <= n and s[ord[j]] + t[ord[j]] >= que[i].z) {
st.update(a[ord[j]], t[ord[j]], 1);
++j;
}
int p = lower_bound(cx.begin(), cx.end(), que[i].x) - cx.begin() + 1;
ans[que[i].id] = st.get(p, (int)cx.size(), inf);
ans[que[i].id] -= st.get(p, (int)cx.size(), que[i].y - 1);
}
for(int i = 1; i <= q; ++i) {
cout << ans[i] << '\n';
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
10844 KB |
Output is correct |
2 |
Correct |
3 ms |
10680 KB |
Output is correct |
3 |
Correct |
3 ms |
9820 KB |
Output is correct |
4 |
Correct |
4 ms |
10844 KB |
Output is correct |
5 |
Correct |
3 ms |
10844 KB |
Output is correct |
6 |
Correct |
3 ms |
10844 KB |
Output is correct |
7 |
Correct |
8 ms |
11468 KB |
Output is correct |
8 |
Correct |
8 ms |
11460 KB |
Output is correct |
9 |
Correct |
8 ms |
11420 KB |
Output is correct |
10 |
Correct |
9 ms |
11356 KB |
Output is correct |
11 |
Correct |
6 ms |
10332 KB |
Output is correct |
12 |
Correct |
4 ms |
10076 KB |
Output is correct |
13 |
Correct |
8 ms |
10468 KB |
Output is correct |
14 |
Correct |
10 ms |
11356 KB |
Output is correct |
15 |
Correct |
9 ms |
10588 KB |
Output is correct |
16 |
Correct |
5 ms |
10844 KB |
Output is correct |
17 |
Correct |
5 ms |
11356 KB |
Output is correct |
18 |
Correct |
4 ms |
10844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
365 ms |
26804 KB |
Output is correct |
2 |
Correct |
342 ms |
26816 KB |
Output is correct |
3 |
Correct |
371 ms |
26820 KB |
Output is correct |
4 |
Correct |
145 ms |
23644 KB |
Output is correct |
5 |
Correct |
155 ms |
17020 KB |
Output is correct |
6 |
Correct |
56 ms |
16072 KB |
Output is correct |
7 |
Correct |
302 ms |
26692 KB |
Output is correct |
8 |
Correct |
320 ms |
26568 KB |
Output is correct |
9 |
Correct |
280 ms |
26580 KB |
Output is correct |
10 |
Correct |
87 ms |
15684 KB |
Output is correct |
11 |
Correct |
132 ms |
22988 KB |
Output is correct |
12 |
Correct |
36 ms |
14668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
365 ms |
26804 KB |
Output is correct |
2 |
Correct |
342 ms |
26816 KB |
Output is correct |
3 |
Correct |
371 ms |
26820 KB |
Output is correct |
4 |
Correct |
145 ms |
23644 KB |
Output is correct |
5 |
Correct |
155 ms |
17020 KB |
Output is correct |
6 |
Correct |
56 ms |
16072 KB |
Output is correct |
7 |
Correct |
302 ms |
26692 KB |
Output is correct |
8 |
Correct |
320 ms |
26568 KB |
Output is correct |
9 |
Correct |
280 ms |
26580 KB |
Output is correct |
10 |
Correct |
87 ms |
15684 KB |
Output is correct |
11 |
Correct |
132 ms |
22988 KB |
Output is correct |
12 |
Correct |
36 ms |
14668 KB |
Output is correct |
13 |
Correct |
381 ms |
27348 KB |
Output is correct |
14 |
Correct |
372 ms |
27080 KB |
Output is correct |
15 |
Correct |
355 ms |
26760 KB |
Output is correct |
16 |
Correct |
158 ms |
24004 KB |
Output is correct |
17 |
Correct |
164 ms |
17608 KB |
Output is correct |
18 |
Correct |
57 ms |
15808 KB |
Output is correct |
19 |
Correct |
383 ms |
27232 KB |
Output is correct |
20 |
Correct |
368 ms |
27076 KB |
Output is correct |
21 |
Correct |
367 ms |
27140 KB |
Output is correct |
22 |
Correct |
85 ms |
15708 KB |
Output is correct |
23 |
Correct |
110 ms |
23024 KB |
Output is correct |
24 |
Correct |
35 ms |
14736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
10844 KB |
Output is correct |
2 |
Correct |
3 ms |
10680 KB |
Output is correct |
3 |
Correct |
3 ms |
9820 KB |
Output is correct |
4 |
Correct |
4 ms |
10844 KB |
Output is correct |
5 |
Correct |
3 ms |
10844 KB |
Output is correct |
6 |
Correct |
3 ms |
10844 KB |
Output is correct |
7 |
Correct |
8 ms |
11468 KB |
Output is correct |
8 |
Correct |
8 ms |
11460 KB |
Output is correct |
9 |
Correct |
8 ms |
11420 KB |
Output is correct |
10 |
Correct |
9 ms |
11356 KB |
Output is correct |
11 |
Correct |
6 ms |
10332 KB |
Output is correct |
12 |
Correct |
4 ms |
10076 KB |
Output is correct |
13 |
Correct |
8 ms |
10468 KB |
Output is correct |
14 |
Correct |
10 ms |
11356 KB |
Output is correct |
15 |
Correct |
9 ms |
10588 KB |
Output is correct |
16 |
Correct |
5 ms |
10844 KB |
Output is correct |
17 |
Correct |
5 ms |
11356 KB |
Output is correct |
18 |
Correct |
4 ms |
10844 KB |
Output is correct |
19 |
Correct |
365 ms |
26804 KB |
Output is correct |
20 |
Correct |
342 ms |
26816 KB |
Output is correct |
21 |
Correct |
371 ms |
26820 KB |
Output is correct |
22 |
Correct |
145 ms |
23644 KB |
Output is correct |
23 |
Correct |
155 ms |
17020 KB |
Output is correct |
24 |
Correct |
56 ms |
16072 KB |
Output is correct |
25 |
Correct |
302 ms |
26692 KB |
Output is correct |
26 |
Correct |
320 ms |
26568 KB |
Output is correct |
27 |
Correct |
280 ms |
26580 KB |
Output is correct |
28 |
Correct |
87 ms |
15684 KB |
Output is correct |
29 |
Correct |
132 ms |
22988 KB |
Output is correct |
30 |
Correct |
36 ms |
14668 KB |
Output is correct |
31 |
Correct |
381 ms |
27348 KB |
Output is correct |
32 |
Correct |
372 ms |
27080 KB |
Output is correct |
33 |
Correct |
355 ms |
26760 KB |
Output is correct |
34 |
Correct |
158 ms |
24004 KB |
Output is correct |
35 |
Correct |
164 ms |
17608 KB |
Output is correct |
36 |
Correct |
57 ms |
15808 KB |
Output is correct |
37 |
Correct |
383 ms |
27232 KB |
Output is correct |
38 |
Correct |
368 ms |
27076 KB |
Output is correct |
39 |
Correct |
367 ms |
27140 KB |
Output is correct |
40 |
Correct |
85 ms |
15708 KB |
Output is correct |
41 |
Correct |
110 ms |
23024 KB |
Output is correct |
42 |
Correct |
35 ms |
14736 KB |
Output is correct |
43 |
Correct |
414 ms |
32240 KB |
Output is correct |
44 |
Correct |
381 ms |
32128 KB |
Output is correct |
45 |
Correct |
358 ms |
32228 KB |
Output is correct |
46 |
Correct |
171 ms |
27844 KB |
Output is correct |
47 |
Correct |
152 ms |
18628 KB |
Output is correct |
48 |
Correct |
57 ms |
15812 KB |
Output is correct |
49 |
Correct |
326 ms |
31944 KB |
Output is correct |
50 |
Correct |
328 ms |
31944 KB |
Output is correct |
51 |
Correct |
298 ms |
32116 KB |
Output is correct |
52 |
Correct |
104 ms |
17740 KB |
Output is correct |
53 |
Correct |
116 ms |
26460 KB |
Output is correct |