#pragma GCC optimize("O2")
#include <bits/stdc++.h>
using namespace std;
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define traverse(c, it) for(auto it = (c).begin(); it != (c).end(); ++it)
#define rep(i, N) for(int i = 0; i < (N); ++i)
#define rrep(i, N) for(int i = (N) - 1; i >= 0; --i)
#define rep1(i, N) for(int i = 1; i <= (N); ++i)
#define rep2(i, s, e) for(int i = (s); i <= (e); ++i)
#define rep3(i, s, e, d) for(int i = (s); (d) >= 0 ? i <= (e) : i >= (e); i += (d))
#ifdef DEBUG
#define debug(x...) { \
++dbg::depth; \
string dbg_vals = dbg::to_string(x); \
--dbg::depth; \
dbg::fprint(__func__, __LINE__, #x, dbg_vals); \
}
#define light_debug(x) { \
dbg::light = true; \
dbg::dout << __func__ << ":" << __LINE__; \
dbg::dout << " " << #x << " = " << x << endl; \
dbg::light = false; \
}
#else
#define debug(x...) 42
#define light_debug(x) 42
#endif
using ll = long long;
template<typename T>
inline T& ckmin(T& a, T b) { return a = a > b ? b : a; }
template<typename T>
inline T& ckmax(T& a, T b) { return a = a < b ? b : a; }
struct fenwick_tree {
int n;
vector<int> a;
vector<int> u;
fenwick_tree(int n_ = 0) : n{n_}, a(n + 1) {}
void update(int i) {
for(++i; i <= n; i += i & -i)
++a[i], u.push_back(i);
}
int pref(int i) const {
int s{};
for(++i; i > 0; i -= i & -i)
s += a[i];
return s;
}
int query(int s, int e) const {
return pref(e) - pref(s - 1);
}
void clear() {
for(int x : u) a[x] = 0;
u.clear();
}
};
struct point {
int x, y, z, i{-1};
};
namespace std {
string to_string(const point& p) {
ostringstream ss;
ss << "{" << p.x << ", " << p.y << ", " << p.z << ", " << p.i << "}";
return ss.str();
}
};
#ifdef DEBUG
#include "debug.hpp"
#endif
int N, Q, M{};
vector<int> ans;
vector<point> points;
fenwick_tree bit;
using it = vector<point>::iterator;
void cdq_dnc(const it s, const it e) {
if(e - s == 1) return;
it m = s + (e - s) / 2;
cdq_dnc(s, m), cdq_dnc(m, e);
vector<point> tmp; tmp.reserve(e - s);
for(auto i = s, j = m; i < m || j < e; ) {
if(i != m && (j == e || i->y >= j->y)) {
tmp.push_back(*i);
if(i->i == -1)
bit.update(i->z);
++i;
} else {
tmp.push_back(*j);
if(j->i >= 0)
ans[j->i] += bit.query(j->z, M - 1);
++j;
}
}
bit.clear();
for(auto i = tmp.begin(), j = s; j < e; ++i, ++j)
*j = *i;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
#ifdef DEBUG
freopen("debug", "w", stderr);
#endif
cin >> N >> Q; points.reserve(N + Q), ans.resize(Q);
rep(i, N) {
int S, T; cin >> S >> T;
points.push_back(point{S, T, S + T, -1});
}
rep(i, Q) {
int X, Y, Z; cin >> X >> Y >> Z;
points.push_back(point{X, Y, Z, i});
}
sort(all(points), [](const point& p, const point& q) {
return p.x > q.x || (p.x == q.x && p.y > q.y)
|| (p.x == q.x && p.y == q.y && p.z > q.z)
|| (p.x == q.x && p.y == q.y && p.z == q.z && p.i < q.i);
});
map<int, int> mp;
for(const auto& p : points) mp[p.z];
for(auto& p : mp) p.second = M++;
for(auto& p : points) p.z = mp[p.z];
bit = fenwick_tree(M);
cdq_dnc(all(points));
rep(i, Q) cout << ans[i] << '\n';
#ifdef DEBUG
dbg::dout << "\nExecution time: " << clock() * 1000 / CLOCKS_PER_SEC << "ms" << endl;
#endif
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
9 ms |
876 KB |
Output is correct |
8 |
Correct |
8 ms |
1004 KB |
Output is correct |
9 |
Correct |
8 ms |
876 KB |
Output is correct |
10 |
Correct |
7 ms |
876 KB |
Output is correct |
11 |
Correct |
7 ms |
876 KB |
Output is correct |
12 |
Correct |
4 ms |
620 KB |
Output is correct |
13 |
Correct |
8 ms |
876 KB |
Output is correct |
14 |
Correct |
7 ms |
944 KB |
Output is correct |
15 |
Correct |
7 ms |
1004 KB |
Output is correct |
16 |
Correct |
5 ms |
876 KB |
Output is correct |
17 |
Correct |
7 ms |
1132 KB |
Output is correct |
18 |
Correct |
3 ms |
620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
274 ms |
14048 KB |
Output is correct |
2 |
Correct |
277 ms |
13888 KB |
Output is correct |
3 |
Correct |
266 ms |
13920 KB |
Output is correct |
4 |
Correct |
217 ms |
13248 KB |
Output is correct |
5 |
Correct |
262 ms |
13408 KB |
Output is correct |
6 |
Correct |
118 ms |
9844 KB |
Output is correct |
7 |
Correct |
261 ms |
15524 KB |
Output is correct |
8 |
Correct |
256 ms |
13920 KB |
Output is correct |
9 |
Correct |
257 ms |
15436 KB |
Output is correct |
10 |
Correct |
227 ms |
12252 KB |
Output is correct |
11 |
Correct |
215 ms |
13276 KB |
Output is correct |
12 |
Correct |
95 ms |
9588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
274 ms |
14048 KB |
Output is correct |
2 |
Correct |
277 ms |
13888 KB |
Output is correct |
3 |
Correct |
266 ms |
13920 KB |
Output is correct |
4 |
Correct |
217 ms |
13248 KB |
Output is correct |
5 |
Correct |
262 ms |
13408 KB |
Output is correct |
6 |
Correct |
118 ms |
9844 KB |
Output is correct |
7 |
Correct |
261 ms |
15524 KB |
Output is correct |
8 |
Correct |
256 ms |
13920 KB |
Output is correct |
9 |
Correct |
257 ms |
15436 KB |
Output is correct |
10 |
Correct |
227 ms |
12252 KB |
Output is correct |
11 |
Correct |
215 ms |
13276 KB |
Output is correct |
12 |
Correct |
95 ms |
9588 KB |
Output is correct |
13 |
Correct |
392 ms |
16476 KB |
Output is correct |
14 |
Correct |
366 ms |
15840 KB |
Output is correct |
15 |
Correct |
267 ms |
14044 KB |
Output is correct |
16 |
Correct |
300 ms |
14564 KB |
Output is correct |
17 |
Correct |
299 ms |
14768 KB |
Output is correct |
18 |
Correct |
121 ms |
10868 KB |
Output is correct |
19 |
Correct |
377 ms |
19468 KB |
Output is correct |
20 |
Correct |
362 ms |
19040 KB |
Output is correct |
21 |
Correct |
372 ms |
21792 KB |
Output is correct |
22 |
Correct |
211 ms |
13812 KB |
Output is correct |
23 |
Correct |
218 ms |
14948 KB |
Output is correct |
24 |
Correct |
96 ms |
10484 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
9 ms |
876 KB |
Output is correct |
8 |
Correct |
8 ms |
1004 KB |
Output is correct |
9 |
Correct |
8 ms |
876 KB |
Output is correct |
10 |
Correct |
7 ms |
876 KB |
Output is correct |
11 |
Correct |
7 ms |
876 KB |
Output is correct |
12 |
Correct |
4 ms |
620 KB |
Output is correct |
13 |
Correct |
8 ms |
876 KB |
Output is correct |
14 |
Correct |
7 ms |
944 KB |
Output is correct |
15 |
Correct |
7 ms |
1004 KB |
Output is correct |
16 |
Correct |
5 ms |
876 KB |
Output is correct |
17 |
Correct |
7 ms |
1132 KB |
Output is correct |
18 |
Correct |
3 ms |
620 KB |
Output is correct |
19 |
Correct |
274 ms |
14048 KB |
Output is correct |
20 |
Correct |
277 ms |
13888 KB |
Output is correct |
21 |
Correct |
266 ms |
13920 KB |
Output is correct |
22 |
Correct |
217 ms |
13248 KB |
Output is correct |
23 |
Correct |
262 ms |
13408 KB |
Output is correct |
24 |
Correct |
118 ms |
9844 KB |
Output is correct |
25 |
Correct |
261 ms |
15524 KB |
Output is correct |
26 |
Correct |
256 ms |
13920 KB |
Output is correct |
27 |
Correct |
257 ms |
15436 KB |
Output is correct |
28 |
Correct |
227 ms |
12252 KB |
Output is correct |
29 |
Correct |
215 ms |
13276 KB |
Output is correct |
30 |
Correct |
95 ms |
9588 KB |
Output is correct |
31 |
Correct |
392 ms |
16476 KB |
Output is correct |
32 |
Correct |
366 ms |
15840 KB |
Output is correct |
33 |
Correct |
267 ms |
14044 KB |
Output is correct |
34 |
Correct |
300 ms |
14564 KB |
Output is correct |
35 |
Correct |
299 ms |
14768 KB |
Output is correct |
36 |
Correct |
121 ms |
10868 KB |
Output is correct |
37 |
Correct |
377 ms |
19468 KB |
Output is correct |
38 |
Correct |
362 ms |
19040 KB |
Output is correct |
39 |
Correct |
372 ms |
21792 KB |
Output is correct |
40 |
Correct |
211 ms |
13812 KB |
Output is correct |
41 |
Correct |
218 ms |
14948 KB |
Output is correct |
42 |
Correct |
96 ms |
10484 KB |
Output is correct |
43 |
Correct |
463 ms |
25436 KB |
Output is correct |
44 |
Correct |
490 ms |
25344 KB |
Output is correct |
45 |
Correct |
448 ms |
25184 KB |
Output is correct |
46 |
Correct |
381 ms |
23776 KB |
Output is correct |
47 |
Correct |
389 ms |
23980 KB |
Output is correct |
48 |
Correct |
130 ms |
10728 KB |
Output is correct |
49 |
Correct |
436 ms |
26860 KB |
Output is correct |
50 |
Correct |
456 ms |
25312 KB |
Output is correct |
51 |
Correct |
438 ms |
26524 KB |
Output is correct |
52 |
Correct |
363 ms |
23252 KB |
Output is correct |
53 |
Correct |
230 ms |
17764 KB |
Output is correct |