-
Notifications
You must be signed in to change notification settings - Fork 0
/
intersecting_segments_segment_tree.cpp
76 lines (58 loc) · 1.41 KB
/
intersecting_segments_segment_tree.cpp
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
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 2e5+2, MOD = 1e9+7;
int tree[4*N];
struct Triplet {
int l, r, idx;
};
int query(int node, int st, int end, int l, int r) {
if(st>r || end<l)
return 0;
if(l <= st && r >= end)
return tree[node];
int mid = (st + end) / 2;
int q1 = query(2*node, st, mid, l, r);
int q2 = query(2*node+1, mid+1, end, l, r);
return q1 + q2;
}
void update(int node, int st, int end, int idx, int val) {
if(st == end) {
tree[node] = val;
return;
}
int mid = (st + end) / 2;
if(idx <= mid)
update(2*node, st, mid, idx, val);
else
update(2*node+1, mid+1, end, idx, val);
tree[node] = tree[2*node] + tree[2*node+1];
}
bool compare(Triplet t1, Triplet t2) {
return t1.r < t2.r;
}
signed main() {
int n;
cin>>n;
Triplet t1;
t1.l = t1.r = -1;
vector<Triplet> t(n, t1);
int x;
for(int i=0; i<2*n; i++) {
cin>>x;
if(t[x-1].l == -1)
t[x-1].l = i;
else
t[x-1].r = i;
t[x-1].idx = x;
}
sort(t.begin(), t.end(), compare);
vector<int> ans(n);
for(int i=0; i<n; i++) {
ans[t[i].idx - 1] = (t[i].r - t[i].l - 1) - 2*query(1, 0, 2*n-1, t[i].l, t[i].r);
update(1, 0, 2*n-1, t[i].l, 1);
}
for(int i=0; i<n; i++)
cout<<ans[i]<<" ";
return 0;
}