日常有用的代码记录

发布时间 2023-10-24 14:34:06作者: LightYT

快读

inline int read()
{
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-')
            f=-1;
        ch=getchar();
    }
    while(ch>='0' && ch<='9')
        x=x*10+ch-'0',ch=getchar();
    return x*f;
}

归并排序

void msort(int left ,int right)
{
    
    int mid=(left+right)/2;
    int pos_l=left;
    int pos_r=mid+1;
    int pos=left;
    if(left>=right)
        return;
    msort(left,mid);
    msort(mid+1,right);
    while(pos_l<=mid && pos_r<=right)
    {
        if(a[pos_l] <= a[pos_r])
            temp[pos++]=a[pos_l++];
        else
            temp[pos++]=a[pos_r++];
    }
    while(pos_l<=mid)
        temp[pos++]=a[pos_l++];
    while(pos_r<=right)
        temp[pos++]=a[pos_r++];
    while(left<=right)
    {
        a[left]=temp[left];
        left++;
    }
}

O2优化(吸氧)

#pragma GCC optimize(2)