-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNested_Ranges_Count.cpp
More file actions
110 lines (91 loc) · 2.88 KB
/
Nested_Ranges_Count.cpp
File metadata and controls
110 lines (91 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Mohit Verma "mohitvdx"
// problem: Count Containing and Contained Ranges
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int MOD = 1e9 + 7;
const int INF = LLONG_MAX >> 1;
struct Range {
int left, right, index;
};
// Sorting criteria for "contains" query
bool compareContains(const Range &r1, const Range &r2) {
if (r1.left != r2.left) return r1.left < r2.left;
return r1.right > r2.right; // Sort by decreasing right for same left
}
// Sorting criteria for "contained by" query
bool compareContainedBy(const pair<int, int> &p1, const pair<int, int> &p2) {
if (p1.first != p2.first) return p1.first < p2.first;
return p1.second > p2.second;
}
// Fenwick Tree (Binary Indexed Tree)
void update(int idx, vector<int> &bit, int n, int value) {
for (; idx <= n; idx += idx & (-idx))
bit[idx] += value;
}
int query(int idx, vector<int> &bit) {
int sum = 0;
for (; idx > 0; idx -= idx & (-idx))
sum += bit[idx];
return sum;
}
void solve() {
int n;
cin >> n;
vector<Range> ranges(n);
for (int i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
ranges[i] = {x, y, i};
}
// Sort for "contains" count
sort(ranges.begin(), ranges.end(), compareContains);
vector<pair<int, int>> opening(n), closing(n);
for (int i = 0; i < n; i++) {
opening[i] = {ranges[i].left, ranges[i].index};
closing[i] = {ranges[i].right, i};
}
sort(closing.begin(), closing.end(), compareContainedBy);
vector<int> contains(n, 0), containedBy(n, 0);
vector<int> BIT1(n + 1, 0), BIT2(n + 1, 0);
// Compute "contains" counts
int i = 0, j = 0;
while (i < n || j < n) {
if (i < n && opening[i].first < closing[j].first) {
update(i + 1, BIT1, n, 1);
i++;
} else {
update(closing[j].second + 1, BIT1, n, -1);
contains[opening[closing[j].second].second] = query(closing[j].second + 1, BIT1);
j++;
}
}
// Compute "contained by" counts
i = 0, j = 0;
while (i < n || j < n) {
if (i < n && opening[i].first < closing[j].first) {
i++;
} else {
containedBy[opening[closing[j].second].second] = query(closing[j].second + 1, BIT2);
update(1, BIT2, n, 1);
if (closing[j].second + 2 <= n)
update(closing[j].second + 2, BIT2, n, -1);
j++;
}
}
// Print results
for (int x : containedBy) cout << x << " ";
cout << "\n";
for (int x : contains) cout << x << " ";
cout << "\n";
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(NULL); // fast IO
solve();
}
/*
- Uses Fenwick Tree (BIT) for efficient range count queries.
- Sorted by left (asc) and right (desc) for containment calculation.
- Processed efficiently using BIT with O(n log n) complexity.
*/