```cpp
#include<bits/stdc++.h>  
  
using namespace std;  
  
int main() {  
    int n;  
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);  
    cin>>n;  
//    long long ans=0;    //维护逆序对  
    vector<int>a(n);  
    for(int i=0;i<n;i++)  
        cin>>a[i];  
    auto merge=[&](vector<int>&a,vector<int>&b)->vector<int>  
    {  
        vector<int>res;  
        size_t ai=0,bi=0;  
        while(ai<a.size()&&bi<b.size()){  
            if(b[bi]<a[ai])  
            {  
                res.push_back(b[bi]);  
                bi++;  
//                ans+=a.size()-ai;         //更新逆序对  
            }  
            else {  
                res.push_back(a[ai]);  
                ai++;  
            }  
        }  
        if(ai==a.size())  
            res.insert(res.end(),b.begin()+bi,b.end());  
        else if(bi==b.size())  
            res.insert(res.end(),a.begin()+ai,a.end());  
        return res;  
    };  
    auto merge_sort=[&](auto &&self,vector<int>&arr)->vector<int>{  
        if(arr.size()<=1)return arr;  
        const size_t mid=arr.size()>>1;  
        vector<int> left(arr.begin(),arr.begin()+mid);  
        vector<int> right(arr.begin()+mid,arr.end());  
        left=self(self,left);  
        right=self(self,right);  
        return merge(left,right);  
    };  
    a=merge_sort(merge_sort,a);  
    for(auto i:a)  
        cout<<i<<" ";  
//    cout<<ans;        //输出逆序对  
}
```


```cpp
struct sgt
{
    int w,l,r;
};
void dfssgt(int p,vector<sgt>&a)
{
    printf("[%d,%d]=(%d,%d,%d)\n",a[p].l,a[p].r,a[p].w,0,0);
    dfssgt(p*2,a);
    dfssgt(p*2+1,a);
}
void sgtdbg(vector<sgt>&a)
{
    printf("Tree{\n");
    dfssgt(1,a);
    printf("}\n");
}
void build(int p,vector<sgt>&tr)
{
    if(tr[p].l==tr[p].r)return;
    int m=tr[p].l+tr[p].r>>1;
    tr[2*p].l=tr[p].l,tr[2*p].r=m,tr[p*2+1].l=m+1,tr[p*2+1].r=tr[p].r;
    build(2*p,tr);build(2*p+1,tr);
}
void modify(int n,int x,int p,vector<sgt>&tr)
{
    if(tr[p].l==tr[p].r){
        tr[p].w=x;
        return;
    }
    if(n<=tr[2*p].r)modify(n,x,p*2,tr);
    else modify(n,x,p*2+1,tr);
    tr[p].w=max(tr[2*p].w,tr[2*p+1].w);
}
int querys(int l,int r,int p,vector<sgt>&tr)
{
    int s=tr[p].l,t=tr[p].r;
    if(l<=s&&t<=r)return tr[p].w;
    int maxx=0;
    if(l<=tr[2*p].r)maxx=max(maxx,querys(l,r,2*p,tr));
    if(r>=tr[2*p+1].l)maxx=max(maxx,querys(l,r,2*p+1,tr));
    return maxx;
}
```


线段交
```cpp
struct pt {
    long long x, y;
    pt() {}
    pt(long long _x, long long _y) : x(_x), y(_y) {}
    pt operator-(const pt& p) const { return pt(x - p.x, y - p.y); }
    long long cross(const pt& p) const { return x * p.y - y * p.x; }
    long long cross(const pt& a, const pt& b) const { return (a - *this).cross(b - *this); }
};

int sgn(const long long& x) { return x >= 0 ? x ? 1 : 0 : -1; }

bool inter1(long long a, long long b, long long c, long long d) {
    if (a > b)
        swap(a, b);
    if (c > d)
        swap(c, d);
    return max(a, c) <= min(b, d);
}

bool check_inter(const pt& a, const pt& b, const pt& c, const pt& d) {
    if (c.cross(a, d) == 0 && c.cross(b, d) == 0)
        return inter1(a.x, b.x, c.x, d.x) && inter1(a.y, b.y, c.y, d.y);
    return sgn(a.cross(b, c)) != sgn(a.cross(b, d)) &&
           sgn(c.cross(d, a)) != sgn(c.cross(d, b));
}
```