-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge sort tree.cpp
More file actions
28 lines (28 loc) · 982 Bytes
/
Copy pathmerge sort tree.cpp
File metadata and controls
28 lines (28 loc) · 982 Bytes
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
// Returns the number of elements less than or equal to a particular number
// in an unsorted array in a given range for multiple queries
// in n*log n for building and log n for answerig each query
#define all(v) v.begin(),v.end()
const int N=1e5;
vector<int> tree[4*N];
int n,m,arr[100005];
void build(int node,int l,int r)
{
if(l==r)
{
tree[node].push_back(arr[l]);
return;
}
int mid=l+(r-l)/2;
build(2*node,l,mid);
build(2*node+1,mid+1,r);
merge(all (tree[2*node]),all (tree[2*node+1]),back_inserter(tree[node]));
}
int query(int node,int start,int end,int l,int r,int val)
{
if(start>r||end<l)
return 0;
if(l<=start&&end<=r)
return lower_bound(all(tree[node]),val)-tree[node].begin();
ll mid=start+(end-start)/2;
return query(2*node,start,mid,l,r,val)+query(2*node+1,mid+1,end,l,r,val);
}