Ordered Segment Tree - YessineJallouli/Competitive-Programming GitHub Wiki
#include <bits/stdc++.h>
#define ll long long
#define SaveTime ios_base::sync_with_stdio(false), cin.tie(0);
using namespace std;
vector<int> mrg(vector<int> &a, vector<int> &b) {
int i = 0, j = 0;
vector<int> res;
while (i < a.size() && j < b.size()) {
if (a[i] < b[j]) {
res.push_back(a[i++]);
}
else
res.push_back(b[j++]);
}
while (i < a.size())
res.push_back(a[i++]);
while (j < b.size())
res.push_back(b[j++]);
return res;
}
const int N = 1e5+7;
vector<int> tree[4*N];
int a[N];
int n;
void build(int id = 0, int ns = 0, int ne = n-1) {
if (ns == ne) {
tree[id].push_back(a[ns]);
return;
}
int md = (ns+ne)/2;
int l = 2*id+1;
int r = 2*id+2;
build(l, ns, md);
build(r, md+1, ne);
tree[id] = mrg(tree[l], tree[r]);
}
int main() {
SaveTime
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
build();
}
Problems :
https://codeforces.com/contest/813/problem/E