#include <bits/stdc++.h>
using namespace std;
#define sz(x) int(x.size())
#define all(x) begin(x), end(x)
struct Query {
int a, b, c, idx;
};
struct FTree {
int n;
vector<int> ft;
FTree(int _n) : n(_n + 5), ft(n, 0) {}
void update(int p) {
for (++p; p < n; p += p & -p) ft[p]++;
}
int query(int p) {
int s = 0;
for (++p; p > 0; p -= p & -p) s += ft[p];
return s;
}
int query(int l, int r) {
return query(r) - query(l - 1);
}
};
int compress(const vector<int> &v, int x) {
return int(lower_bound(all(v), x) - begin(v));
}
vector<int> compress(const vector<int> &x, vector<int> &v) {
sort(all(v)); v.erase(unique(all(v)), end(v));
vector<int> c(sz(x));
for (int i = 0; i < sz(x); i++) {
c[i] = compress(v, x[i]);
}
return c;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n, q;
cin >> n >> q;
vector<int> s(n), t(n);
for (int i = 0; i < n; i++) {
cin >> s[i] >> t[i];
}
vector<int> v_s = s, v_t = t;
vector<Query> v[2];
for (int i = 0; i < q; i++) {
Query x; cin >> x.a >> x.b >> x.c; x.idx = i;
v_s.push_back(x.a); v_t.push_back(x.b);
v[x.a + x.b < x.c].push_back(x);
}
vector<int> s_comp = compress(s, v_s), t_comp = compress(t, v_t);
const int M = max(sz(v_s), sz(v_t));
vector<int> res(q);
//v[0] -> la diagonal queda abajo
//es lo mismo que s[i] >= x.a y t[i] >= x.b
//lo primero uso sweep line y lo segundo un fenwick
sort(all(v[0]), [](const Query &lhs, const Query &rhs) {
return lhs.a > rhs.a;
});
vector<int> p(n); iota(all(p), 0);
sort(all(p), [&](const int &lhs, const int &rhs) {
return s[lhs] > s[rhs];
});
FTree ft(M);
int i = 0;
for (Query x : v[0]) {
while (i < n && s[p[i]] >= x.a) {
ft.update(t_comp[p[i]]);
i++;
}
res[x.idx] = ft.query(compress(v_t, x.b), M);
}
//v[1] -> la diagonal queda arriba
//si s[i] + t[i] >= x.c > x.a + x.b entonces s[i] >= x.a (1) ó t[i] >= x.b (2)
//total = (1) + (2) - (1 y 2)
//(1 y 2) = (1) + (2) - total
FTree ftA(M), ftB(M);
sort(all(v[1]), [](const Query &lhs, const Query &rhs) {
return lhs.c > rhs.c;
});
sort(all(p), [&](const int &lhs, const int &rhs) {
return s[lhs] + t[lhs] > s[rhs] + t[rhs];
});
i = 0;
for (Query x : v[1]) {
while (i < n && s[p[i]] + t[p[i]] >= x.c) {
ftA.update(s_comp[p[i]]);
ftB.update(t_comp[p[i]]);
i++;
}
res[x.idx] = ftA.query(compress(v_s, x.a), M) + ftB.query(compress(v_t, x.b), M) - i;
}
for (int i = 0; i < q; i++) {
cout << res[i] << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
6 ms |
848 KB |
Output is correct |
8 |
Correct |
6 ms |
848 KB |
Output is correct |
9 |
Correct |
4 ms |
848 KB |
Output is correct |
10 |
Correct |
4 ms |
592 KB |
Output is correct |
11 |
Correct |
4 ms |
592 KB |
Output is correct |
12 |
Correct |
3 ms |
592 KB |
Output is correct |
13 |
Correct |
4 ms |
848 KB |
Output is correct |
14 |
Correct |
4 ms |
848 KB |
Output is correct |
15 |
Correct |
4 ms |
848 KB |
Output is correct |
16 |
Correct |
3 ms |
592 KB |
Output is correct |
17 |
Correct |
4 ms |
592 KB |
Output is correct |
18 |
Correct |
3 ms |
676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
123 ms |
7932 KB |
Output is correct |
2 |
Correct |
123 ms |
7988 KB |
Output is correct |
3 |
Correct |
124 ms |
10288 KB |
Output is correct |
4 |
Correct |
92 ms |
9692 KB |
Output is correct |
5 |
Correct |
93 ms |
9520 KB |
Output is correct |
6 |
Correct |
58 ms |
7996 KB |
Output is correct |
7 |
Correct |
127 ms |
10288 KB |
Output is correct |
8 |
Correct |
117 ms |
10288 KB |
Output is correct |
9 |
Correct |
116 ms |
10032 KB |
Output is correct |
10 |
Correct |
93 ms |
9532 KB |
Output is correct |
11 |
Correct |
86 ms |
9520 KB |
Output is correct |
12 |
Correct |
48 ms |
7420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
123 ms |
7932 KB |
Output is correct |
2 |
Correct |
123 ms |
7988 KB |
Output is correct |
3 |
Correct |
124 ms |
10288 KB |
Output is correct |
4 |
Correct |
92 ms |
9692 KB |
Output is correct |
5 |
Correct |
93 ms |
9520 KB |
Output is correct |
6 |
Correct |
58 ms |
7996 KB |
Output is correct |
7 |
Correct |
127 ms |
10288 KB |
Output is correct |
8 |
Correct |
117 ms |
10288 KB |
Output is correct |
9 |
Correct |
116 ms |
10032 KB |
Output is correct |
10 |
Correct |
93 ms |
9532 KB |
Output is correct |
11 |
Correct |
86 ms |
9520 KB |
Output is correct |
12 |
Correct |
48 ms |
7420 KB |
Output is correct |
13 |
Correct |
139 ms |
10300 KB |
Output is correct |
14 |
Correct |
129 ms |
11028 KB |
Output is correct |
15 |
Correct |
128 ms |
10436 KB |
Output is correct |
16 |
Correct |
104 ms |
9536 KB |
Output is correct |
17 |
Correct |
105 ms |
9528 KB |
Output is correct |
18 |
Correct |
62 ms |
7984 KB |
Output is correct |
19 |
Correct |
146 ms |
10332 KB |
Output is correct |
20 |
Correct |
137 ms |
9268 KB |
Output is correct |
21 |
Correct |
140 ms |
11312 KB |
Output is correct |
22 |
Correct |
88 ms |
9548 KB |
Output is correct |
23 |
Correct |
77 ms |
8752 KB |
Output is correct |
24 |
Correct |
39 ms |
7560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
6 ms |
848 KB |
Output is correct |
8 |
Correct |
6 ms |
848 KB |
Output is correct |
9 |
Correct |
4 ms |
848 KB |
Output is correct |
10 |
Correct |
4 ms |
592 KB |
Output is correct |
11 |
Correct |
4 ms |
592 KB |
Output is correct |
12 |
Correct |
3 ms |
592 KB |
Output is correct |
13 |
Correct |
4 ms |
848 KB |
Output is correct |
14 |
Correct |
4 ms |
848 KB |
Output is correct |
15 |
Correct |
4 ms |
848 KB |
Output is correct |
16 |
Correct |
3 ms |
592 KB |
Output is correct |
17 |
Correct |
4 ms |
592 KB |
Output is correct |
18 |
Correct |
3 ms |
676 KB |
Output is correct |
19 |
Correct |
123 ms |
7932 KB |
Output is correct |
20 |
Correct |
123 ms |
7988 KB |
Output is correct |
21 |
Correct |
124 ms |
10288 KB |
Output is correct |
22 |
Correct |
92 ms |
9692 KB |
Output is correct |
23 |
Correct |
93 ms |
9520 KB |
Output is correct |
24 |
Correct |
58 ms |
7996 KB |
Output is correct |
25 |
Correct |
127 ms |
10288 KB |
Output is correct |
26 |
Correct |
117 ms |
10288 KB |
Output is correct |
27 |
Correct |
116 ms |
10032 KB |
Output is correct |
28 |
Correct |
93 ms |
9532 KB |
Output is correct |
29 |
Correct |
86 ms |
9520 KB |
Output is correct |
30 |
Correct |
48 ms |
7420 KB |
Output is correct |
31 |
Correct |
139 ms |
10300 KB |
Output is correct |
32 |
Correct |
129 ms |
11028 KB |
Output is correct |
33 |
Correct |
128 ms |
10436 KB |
Output is correct |
34 |
Correct |
104 ms |
9536 KB |
Output is correct |
35 |
Correct |
105 ms |
9528 KB |
Output is correct |
36 |
Correct |
62 ms |
7984 KB |
Output is correct |
37 |
Correct |
146 ms |
10332 KB |
Output is correct |
38 |
Correct |
137 ms |
9268 KB |
Output is correct |
39 |
Correct |
140 ms |
11312 KB |
Output is correct |
40 |
Correct |
88 ms |
9548 KB |
Output is correct |
41 |
Correct |
77 ms |
8752 KB |
Output is correct |
42 |
Correct |
39 ms |
7560 KB |
Output is correct |
43 |
Correct |
146 ms |
13564 KB |
Output is correct |
44 |
Correct |
146 ms |
13620 KB |
Output is correct |
45 |
Correct |
140 ms |
14396 KB |
Output is correct |
46 |
Correct |
104 ms |
12096 KB |
Output is correct |
47 |
Correct |
112 ms |
11932 KB |
Output is correct |
48 |
Correct |
57 ms |
8240 KB |
Output is correct |
49 |
Correct |
141 ms |
13620 KB |
Output is correct |
50 |
Correct |
147 ms |
14544 KB |
Output is correct |
51 |
Correct |
152 ms |
14384 KB |
Output is correct |
52 |
Correct |
102 ms |
11828 KB |
Output is correct |
53 |
Correct |
84 ms |
11568 KB |
Output is correct |