#include<bits/stdc++.h>
using namespace std;
using LL=long long;
#define FOR(i,l,r) for(int i=(l);i<=(r);++i)
#define REP(i,n) FOR(i,0,(n)-1)
#define ssize(x) int(x.size())
template<class A,class B>auto&operator<<(ostream&o,pair<A,B>p){return o<<'('<<p.first<<", "<<p.second<<')';}
template<class T>auto operator<<(ostream&o,T x)->decltype(x.end(),o){o<<'{';int i=0;for(auto e:x)o<<(", ")+2*!i++<<e;return o<<'}';}
#ifdef DEBUG
#define debug(x...) cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...)<<'\n';}(x)
#else
#define debug(...) {}
#endif
/*
* Opis: Drzewo potęgowe
* Czas: O(\log n)
* Użycie:
* wszystko indexowane od 0
* update(pos, val) dodaje val do elementu pos
* query(pos) zwraca sumę na przedziale [0, pos]
*/
struct Fenwick {
vector<int> s;
Fenwick(int n) : s(n) {}
void update(int pos, int val) {
for(; pos < ssize(s); pos |= pos + 1)
s[pos] += val;
}
int query(int pos) {
int ret = 0;
for(pos++; pos > 0; pos &= pos - 1)
ret += s[pos - 1];
return ret;
}
int query(int l, int r) {
return query(r) - query(l - 1);
}
};
int main() {
cin.tie(0)->sync_with_stdio(0);
int n, q;
cin >> n >> q;
vector S(n, 0), T(n, 0), K(n, 0);
vector<int> values;
REP (i, n) {
cin >> S[i] >> T[i];
values.emplace_back(S[i]);
values.emplace_back(T[i]);
K[i] = S[i] + T[i];
values.emplace_back(K[i]);
}
vector<int> X(q), Y(q), Z(q);
REP (i, q) {
cin >> X[i] >> Y[i] >> Z[i];
values.emplace_back(X[i]);
values.emplace_back(Y[i]);
values.emplace_back(Z[i]);
}
sort(values.begin(), values.end());
values.erase(unique(values.begin(), values.end()), values.end());
auto scale = [&](int x) {
return (int)(lower_bound(values.begin(), values.end(), x) - values.begin());
};
for (int &i : S)
i = scale(i);
for (int &i : T)
i = scale(i);
for (int &i : K)
i = scale(i);
for (int &i : X)
i = scale(i);
for (int &i : Y)
i = scale(i);
for (int &i : Z)
i = scale(i);
debug(S);
debug(T);
debug(K);
debug(X);
debug(Y);
debug(Z);
Fenwick tree(ssize(values));
vector<int> ans(q);
auto merge = [&](vector<int> point_id, vector<int> query_id) {
vector<tuple<int, int, int>> sr;
for (int id : point_id)
sr.emplace_back(-S[id], 0, id);
for (int id : query_id)
sr.emplace_back(-X[id], 1, id);
sort(sr.begin(), sr.end());
for (auto [val, type, id] : sr) {
if (type)
ans[id] += tree.query(Y[id], ssize(values) - 1);
else
tree.update(T[id], 1);
}
for (int id : point_id)
tree.update(T[id], -1);
};
function<void(int, int, vector<int>, vector<int>)> rek =
[&](int lf, int rg, vector<int> point_id, vector<int> query_id) {
if (lf == rg) {
merge(point_id, query_id);
return;
}
int mid = (lf + rg) / 2;
vector<int> left_point_id, right_point_id, left_query_id, right_query_id;
for (int i : point_id) {
if (K[i] <= mid)
left_point_id.emplace_back(i);
else
right_point_id.emplace_back(i);
}
for (int i : query_id) {
if (Z[i] <= mid)
left_query_id.emplace_back(i);
else
right_query_id.emplace_back(i);
}
merge(right_point_id, left_query_id);
rek(lf, mid, left_point_id, left_query_id);
rek(mid + 1, rg, right_point_id, right_query_id);
};
vector<int> point_id(n), query_id(q);
iota(point_id.begin(), point_id.end(), 0);
iota(query_id.begin(), query_id.end(), 0);
rek(0, ssize(values) - 1, point_id, query_id);
REP (i, q)
cout << ans[i] << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
13 ms |
812 KB |
Output is correct |
8 |
Correct |
14 ms |
724 KB |
Output is correct |
9 |
Correct |
16 ms |
740 KB |
Output is correct |
10 |
Correct |
12 ms |
716 KB |
Output is correct |
11 |
Correct |
12 ms |
724 KB |
Output is correct |
12 |
Correct |
5 ms |
596 KB |
Output is correct |
13 |
Correct |
13 ms |
1064 KB |
Output is correct |
14 |
Correct |
17 ms |
1068 KB |
Output is correct |
15 |
Correct |
13 ms |
1016 KB |
Output is correct |
16 |
Correct |
11 ms |
956 KB |
Output is correct |
17 |
Correct |
10 ms |
724 KB |
Output is correct |
18 |
Correct |
4 ms |
724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
533 ms |
28068 KB |
Output is correct |
2 |
Correct |
528 ms |
30228 KB |
Output is correct |
3 |
Correct |
544 ms |
30132 KB |
Output is correct |
4 |
Correct |
459 ms |
29192 KB |
Output is correct |
5 |
Correct |
489 ms |
29068 KB |
Output is correct |
6 |
Correct |
178 ms |
16868 KB |
Output is correct |
7 |
Correct |
543 ms |
30012 KB |
Output is correct |
8 |
Correct |
534 ms |
30208 KB |
Output is correct |
9 |
Correct |
524 ms |
29292 KB |
Output is correct |
10 |
Correct |
401 ms |
28876 KB |
Output is correct |
11 |
Correct |
436 ms |
29064 KB |
Output is correct |
12 |
Correct |
96 ms |
17552 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
533 ms |
28068 KB |
Output is correct |
2 |
Correct |
528 ms |
30228 KB |
Output is correct |
3 |
Correct |
544 ms |
30132 KB |
Output is correct |
4 |
Correct |
459 ms |
29192 KB |
Output is correct |
5 |
Correct |
489 ms |
29068 KB |
Output is correct |
6 |
Correct |
178 ms |
16868 KB |
Output is correct |
7 |
Correct |
543 ms |
30012 KB |
Output is correct |
8 |
Correct |
534 ms |
30208 KB |
Output is correct |
9 |
Correct |
524 ms |
29292 KB |
Output is correct |
10 |
Correct |
401 ms |
28876 KB |
Output is correct |
11 |
Correct |
436 ms |
29064 KB |
Output is correct |
12 |
Correct |
96 ms |
17552 KB |
Output is correct |
13 |
Correct |
459 ms |
15528 KB |
Output is correct |
14 |
Correct |
446 ms |
16700 KB |
Output is correct |
15 |
Correct |
561 ms |
30240 KB |
Output is correct |
16 |
Correct |
400 ms |
14068 KB |
Output is correct |
17 |
Correct |
418 ms |
14008 KB |
Output is correct |
18 |
Correct |
153 ms |
14076 KB |
Output is correct |
19 |
Correct |
452 ms |
15348 KB |
Output is correct |
20 |
Correct |
476 ms |
15388 KB |
Output is correct |
21 |
Correct |
488 ms |
15436 KB |
Output is correct |
22 |
Correct |
440 ms |
28916 KB |
Output is correct |
23 |
Correct |
444 ms |
29048 KB |
Output is correct |
24 |
Correct |
91 ms |
17596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
13 ms |
812 KB |
Output is correct |
8 |
Correct |
14 ms |
724 KB |
Output is correct |
9 |
Correct |
16 ms |
740 KB |
Output is correct |
10 |
Correct |
12 ms |
716 KB |
Output is correct |
11 |
Correct |
12 ms |
724 KB |
Output is correct |
12 |
Correct |
5 ms |
596 KB |
Output is correct |
13 |
Correct |
13 ms |
1064 KB |
Output is correct |
14 |
Correct |
17 ms |
1068 KB |
Output is correct |
15 |
Correct |
13 ms |
1016 KB |
Output is correct |
16 |
Correct |
11 ms |
956 KB |
Output is correct |
17 |
Correct |
10 ms |
724 KB |
Output is correct |
18 |
Correct |
4 ms |
724 KB |
Output is correct |
19 |
Correct |
533 ms |
28068 KB |
Output is correct |
20 |
Correct |
528 ms |
30228 KB |
Output is correct |
21 |
Correct |
544 ms |
30132 KB |
Output is correct |
22 |
Correct |
459 ms |
29192 KB |
Output is correct |
23 |
Correct |
489 ms |
29068 KB |
Output is correct |
24 |
Correct |
178 ms |
16868 KB |
Output is correct |
25 |
Correct |
543 ms |
30012 KB |
Output is correct |
26 |
Correct |
534 ms |
30208 KB |
Output is correct |
27 |
Correct |
524 ms |
29292 KB |
Output is correct |
28 |
Correct |
401 ms |
28876 KB |
Output is correct |
29 |
Correct |
436 ms |
29064 KB |
Output is correct |
30 |
Correct |
96 ms |
17552 KB |
Output is correct |
31 |
Correct |
459 ms |
15528 KB |
Output is correct |
32 |
Correct |
446 ms |
16700 KB |
Output is correct |
33 |
Correct |
561 ms |
30240 KB |
Output is correct |
34 |
Correct |
400 ms |
14068 KB |
Output is correct |
35 |
Correct |
418 ms |
14008 KB |
Output is correct |
36 |
Correct |
153 ms |
14076 KB |
Output is correct |
37 |
Correct |
452 ms |
15348 KB |
Output is correct |
38 |
Correct |
476 ms |
15388 KB |
Output is correct |
39 |
Correct |
488 ms |
15436 KB |
Output is correct |
40 |
Correct |
440 ms |
28916 KB |
Output is correct |
41 |
Correct |
444 ms |
29048 KB |
Output is correct |
42 |
Correct |
91 ms |
17596 KB |
Output is correct |
43 |
Correct |
586 ms |
19576 KB |
Output is correct |
44 |
Correct |
588 ms |
19608 KB |
Output is correct |
45 |
Correct |
594 ms |
20340 KB |
Output is correct |
46 |
Correct |
503 ms |
16312 KB |
Output is correct |
47 |
Correct |
509 ms |
16444 KB |
Output is correct |
48 |
Correct |
152 ms |
12860 KB |
Output is correct |
49 |
Correct |
561 ms |
20584 KB |
Output is correct |
50 |
Correct |
544 ms |
20012 KB |
Output is correct |
51 |
Correct |
552 ms |
20504 KB |
Output is correct |
52 |
Correct |
455 ms |
15860 KB |
Output is correct |
53 |
Correct |
453 ms |
31192 KB |
Output is correct |