代码库

· · 个人记录

如此强力,令人震撼

复数:

struct cmp{
    using ld=long double; 
    int r,i;//实部 虚部
    cmp(int aa=0,int bb=0){r=aa,i=bb;}
    friend cmp operator+(cmp x,cmp y){
        int a=x.r,b=x.i,c=y.r,d=y.i;
        return cmp(a+c,b+d  );
    } 
    friend cmp operator-(cmp x,cmp y){
        int a=x.r,b=x.i,c=y.r,d=y.i;
        return cmp(a-c,b-d);
    } 
    friend cmp operator*(cmp x,cmp y){
        int a=x.r,b=x.i,c=y.r,d=y.i;
        return cmp(a*c-b*d,a*d+b*c);
    } 
    friend cmp operator/(cmp x,int y){
        ld a=x.r,b=x.i,Y=y;
        return cmp(round(a/Y),round(b/Y));
    }
    friend cmp operator/(cmp x,cmp y){
        int c=y.r,d=y.i;
        return x*(y.bar())/(c*c+d*d);
    } 
    friend cmp operator%(cmp x,cmp y){
        cmp tmp=(x/y)*y;
        return x-tmp;
    }
    friend bool operator==(cmp x,cmp y){
        int a=x.r,b=x.i,c=y.r,d=y.i;
        return (a==c) && (b==d);
    }
    int len(){return r*r+i*i;}//长度 
    cmp bar(){return cmp(r,-i);}//共轭 
};

凸包

struct node{//存储点
    double x,y;
    friend bool operator<(node a,node b){
        if (a.x==b.x) return a.y<b.y;
        return a.x<b.x;
    }
    friend bool operator==(node a,node b){
        return a.x==b.x && a.y==b.y;
    }
};
struct vec{//向量
    double x,y;
    vec(double a,double b){
        x=a,y=b;
    }
    bool check(vec t){
        return (x*t.y-y*t.x)>0;//是否为左转
    }
};
double dist(node a,node b){//两点间距离
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
bool duel(node a,node b,node p){//以a点为线段的一个端点,AB和AP是否顺时针转过了一个角度
    vec ab(b.x-a.x,b.y-a.y);
    vec ap(p.x-a.x,p.y-a.y);
    return ab.check(ap);
}
node p[maxn];
node sta[maxn];
int top;
void Andrew(){//Andrew算法求凸包,凸包存储在sta[1]到sta[top]之间(因为闭合,所以sta[1]=sta[top])
    sort(p+1,p+1+n);
    sta[1]=p[1];
    sta[2]=p[2];
    top=2;
    for (int i=3;i<=n;i++){//升序下凸壳
        while (top>1 && !duel(sta[top-1],sta[top],p[i])) top--;
        sta[++top]=p[i];
    }
    int ttop=top;
    sta[++top]=p[n-1];
    for (int i=n-2;i;i--){//降序下凸壳
        while (top>ttop && !duel(sta[top-1],sta[top],p[i])) top--;
        sta[++top]=p[i];
    }
    return;
}

向量

struct vec{
    double x,y;
    friend vec operator+(vec a,vec b){
        return {a.x+b.x,a.y+b.y};
    }
    friend vec operator-(vec a,vec b){
        return {a.x-b.x,a.y-b.y};
    }
    friend vec operator*(vec a,vec b){//点乘 
        return a.x*b.x-a.y*b.y;
    }
    friend bool operator==(vec a,vec b){
        return a.x==b.x && a.y==b.y;
    }
};

LCT

struct LCT{
    #define ls(x) c[x][0]
    #define rs(x) c[x][1]
    int f[maxn],c[maxn][3];
    stack<int> sta;
    bool r[maxn];
    bool nroot(int x){
        return !(ls(f[x])!=x && rs(f[x])!=x);
    }
    void pushup(int x){
        //write something
        return;
    }
    void pushr(int x){
        swap(ls(x),rs(x));
        r[x]^=1;
        return;
    }
    void pushdown(int x){
        if (r[x]){
            if (ls(x)) pushr(ls(x));
            if (rs(x)) pushr(rs(x));
            r[x]=0;
        }
        return;
    }

    void rot(int x){
        int y=f[x],z=f[y],k=rs(y)==x,w=c[x][!k];
        if (nroot(y)) c[z][rs(z)==y]=x;
        c[x][!k]=y;
        c[y][k]=w;
        if (w) f[w]=y;
        f[y]=x;f[x]=z;
        pushup(y);pushup(x);
        return;
    }
    void splay(int x){
        int y=x;
        for (;!sta.empty();sta.pop());
        sta.push(x);
        for (;nroot(y);sta.push(y=f[y])); 
        for (;!sta.empty();pushdown(sta.top()),sta.pop());
        for (;nroot(x);){
            int y=f[x],z=f[y];
            if (nroot(y)) rot(((x==rs(y))^(y==rs(z)))?x:y);
            rot(x);
        }
        pushup(x);
        return;
    }
    void access(int x){
        for (int y=0;x;x=f[y=x]){
            splay(x);
            rs(x)=y;
            pushup(x);
        }
        return;
    }
    void makeroot(int x){
        access(x);splay(x);
        pushr(x);
        return;
    }
    int findroot(int x){
        access(x);splay(x);
        for (;ls(x);pushdown(x),x=ls(x));
        splay(x);
        return x;
    }
    void split(int x,int y){
        makeroot(x);access(y);
        splay(y);
        return;
    }
    void link(int x,int y){
        makeroot(x);
        if (findroot(y)!=x) f[x]=y;
        return;
    }
    void cut(int x,int y){
        makeroot(x);
        if (findroot(y)==x && f[y]==x && !ls(y)){
            f[y]=rs(x)=0;
            pushup(x);
        }
        return;
    }
  #undef ls
  #undef rs
};

矩阵

struct matrix{
    static const int ea = 3;
    int n, m; // 行 列
    vector<vector<int>> mat;

    vector<int>& operator[](int i) { return mat[i]; }
    const vector<int>& operator[](int i) const { return mat[i]; }

    matrix(int N = 0, int M = 0) {
        n = N; m = M;
        mat.assign(n + ea, vector<int>(m + ea, 0));
    }

    void clear() {
        for (auto &row : mat) fill(row.begin(), row.end(), 0);
    }

    void be_one() {
        n = m = max(n, m);
        mat.assign(n + ea, vector<int>(m + ea, 0));
        for (int i = 1; i <= n; i++) mat[i][i] = 1;
    }

    friend matrix operator+(const matrix& a, const matrix& b) {
        matrix c(max(a.n, b.n), max(a.m, b.m));
        for (int i = 1; i <= c.n; i++) {
            for (int j = 1; j <= c.m; j++) {
                if (i <= a.n && j <= a.m) c[i][j] += a[i][j];
                if (i <= b.n && j <= b.m) c[i][j] += b[i][j];
                // c[i][j] %= mod;
            }
        }
        return c;
    }

    friend matrix operator-(const matrix& a, const matrix& b) {
        matrix c(max(a.n, b.n), max(a.m, b.m));
        for (int i = 1; i <= c.n; i++) {
            for (int j = 1; j <= c.m; j++) {
                if (i <= a.n && j <= a.m) c[i][j] += a[i][j];
                if (i <= b.n && j <= b.m) c[i][j] -= b[i][j];
                // c[i][j] = (c[i][j] % mod + mod) % mod;
            }
        }
        return c;
    }

    friend matrix operator*(const matrix& a, const matrix& b) {
        if (a.m != b.n) return a;
        matrix c(a.n, b.m);
        for (int i = 1; i <= a.n; i++) {
            for (int k = 1; k <= a.m; k++) {
                if (a[i][k] == 0) continue;
                for (int j = 1; j <= b.m; j++) {
                    c[i][j] = c[i][j] + 1LL * a[i][k] * b[k][j];
                    // c[i][j] = (c[i][j] + 1LL * a[i][k] * b[k][j]) % mod;
                }
            }
        }
        return c;
    }

    void init_by_vector(const vector<int>& v) {
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                int idx = (i - 1) * m + j - 1;
                if (idx >= (int)v.size()) mat[i][j] = 0;
                else mat[i][j] = v[idx];
            }
        }
    }

    bool iszero() const {
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= m; j++)
                if (mat[i][j] != 0) return false;
        return true;
    }
    friend matrix operator^(matrix a, long long T) {
        matrix c(a.n, a.m); c.be_one();
        for (; T; a = a * a, T >>= 1) {
            if (T & 1) c = c * a;
        }
        return c;
    }
};