1. 程式人生 > 其它 >求根節點到葉子節點數字之和

求根節點到葉子節點數字之和

板子

  • 有些東西很重要,但又不常用
  • 如果考試的時候忘了,又恰巧考了,那不就 GG 了
  • 所以我就把這類東西記到這裡

離散化

/*************************************************************************
    > File Name: lsh.cpp
    > Author: Typedef 
    > Mail: [email protected] 
    > Created Time: 2021-08-05 11:08:46
    > blog: https://www.cnblogs.com/Illyasviel
 ************************************************************************/
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+7;
int n,m;
int a[N],b[N],c[N];
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]),b[i]=a[i];
    sort(b+1,b+n+1);
    int m=unique(b+1,b+n+1)-b-1;
    for(int i=1;i<=n;i++) c[i]=lower_bound(b+1,b+m+1,a[i])-b;
    for(int i=1;i<=n;i++) printf("%d ",c[i]);
    puts("");
    return 0;
}

快讀

template<class T>void qread(T &x){
    x=0;bool f=0;char c=getchar();
    while(c<'0'||c>'9'){if(c=='-') f=1;c=getchar();}
    while(c>='0'&&c<='9'){x=(x<<3)+(x<<1)+(c^48);c=getchar();}
    if(f) x=-x;
}

矩陣

struct Matrix{
    int n;
    int a[N][N];
    Matrix(int _n){n=_n;memset(a,0,sizeof(n));}
    void init(){
        for(int i=0;i<n;i++) a[i][i]=1;
    }
    friend Matrix operator*(const Matrix &x,const Matrix &y){
        Matrix res(x.n);
        for(int k=0;k<x.n;k++)
            for(int i=0;i<x.n;i++)
                for(int j=0;j<x.n;j++)
                    res.a[i][j]=(res.a[i][j]+x.a[i][k]*y.a[k][j]%mod)%mod;
        return res;
    }
};
Matrix qpow(Matrix a,ll b){
    Matrix res(a.n);
    res.init();
    while(b){
        if(b&1) res=res*a;
        a=a*a;
        b>>=1ll;
    }
    return res;
}

高精

struct HP{
	int p[M],len;
	HP(){
		memset(p,0,sizeof(p));
		len=0;
	}
	void print(){
		printf("%d",p[len]);
		for(int i=len-1;i>0;i--){
			if(!p[i]){
				printf("0000");
				continue;
			}
			for(int k=10;k*p[i]<mod;k*=10)
				printf("0");
			printf("%d",p[i]);
		}
	}
};
HP operator + (const HP &a,const HP &b){
	HP c;
	c.len=max(a.len,b.len);
	int x=0;
	for(int i=1;i<=c.len;i++){
		c.p[i]=a.p[i]+b.p[i]+x;
		x=c.p[i]/mod;
		c.p[i]%=mod;
	}
	if(x) c.p[++c.len]=x;
	return c;
}
HP operator * (const HP &a,const int &b){
	HP c;
	c.len=a.len;
	int x=0;
	for(int i=1;i<=c.len;i++){
		c.p[i]=a.p[i]*b+x;
		x=c.p[i]/mod;
		c.p[i]%=mod;
	}
	while(x) c.p[++c.len]=x%mod,x/=mod;
	return c;
}
HP max(const HP &a,const HP &b){
	if(a.len>b.len) return a;
	else if(a.len<b.len) return b;
	for(int i=a.len;i>0;i--)
		if(a.p[i]>b.p[i]) return a;
		else if(a.p[i]<b.p[i]) return b;
	return a;
}