常用模板

· · 个人记录

快读

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <vector>
using namespace std;
template <typename T>
inline void read(T& x) {
  x = 0; int f = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -f; c = getchar(); }
  while (c >= '0' && c <= '9') { x = (x << 3) + (x << 1) + (c ^ 48), c = getchar(); } x *= f;
}
template <typename T, typename... Args>
inline void read (T &x, Args&... Arg) { read (x), read (Arg...); }
int n,m;
int main(){

    read(n,m);
    printf("%d\n%d\n",n,m);

    return 0;
}

支持连续读入

快快读

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
struct FastIO {
    static const int base = 1000000;
    char c() {
        static char buf[base],*S = buf,*T = buf;
            if (S == T){
            T = (S = buf) + fread(buf, 1, base, stdin);
            if (S == T) return EOF;
        }
        return *S++;
    }
    int operator()() {
        int x=0; char ch=c();
        if (ch == EOF) return 0;
        while (!isdigit(ch)) ch = c();
        while (isdigit(ch)) x = (x << 3) + (x << 1) + (ch ^ 48), ch = c();
        return x;
    }
} read;
int n;
int main(){

    n = read();

    return 0;
}

快写(一般用在 \_\_int128 时)

inline void write(__int128_t x){
    if(x < 0)   putchar('-'),x = -x;
    if(x > 9)   write(x / 10);
    putchar(x % 10 + '0');
}

火车头

#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("avx2,fma")

高精度算法

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
struct bignum
{
    int len,s[9000];//len为当前的长度,s数组记录当前的各位数字 
    bignum()//每次调用bignum初始化 
    {
        memset(s,0,sizeof(s));
        len=1;
    }
    bignum (int sum) {*this=sum;}//让this指针指向当前整形数字 
    bignum (const char *sum) {*this=sum;}//让this指针指向当前字符串
    bignum operator = (const int sum)//重载等号,方便赋值,注意等号必须在重载加减号之前,减号加号必须在重载乘除号之前, 
    {
        char s[9000];
        sprintf(s,"%d",sum);//将整形数组s映射到字符串当中去 
        *this=s;
        return *this; //将字符串转递到下面字符串赋值的函数中
    }
    bignum operator = (const char *sum) //字符串赋值函数 
    {
        for(int i=0;sum[i]=='0';sum++);
        len=strlen(sum);
        for(int i=0;i<len;i++)
        s[i]=sum[len-i-1]-'0';//方便计算反向存数 
        return *this;
    }
    bignum operator + (const bignum &b)//重载加法,按位相加,注意进位 
    {
        bignum c;
        c.len=0;
        for(int i=0,g=0;g||i<max(len,b.len);i++)
        {
            int x=g;
            if(i<len) x+=s[i];
            if(i<b.len) x+=b.s[i];
            c.s[c.len++]=x%10;
            g=x/10;
        }
        return c;
    }
    void clean()//在高精操作中,可能会出现前导0,需要删去 
    {
        while(len>1&&!s[len-1])
        len--;
    }
    bignum operator * (const bignum &b) //高精乘高精
    {
        bignum c;
        c.len=len+b.len;
        for(int i=0;i<len;i++)
        {
            for(int j=0;j<b.len;j++)
            {
                c.s[i+j]+=s[i]*b.s[j];
            }
        }
        for(int i=0;i<c.len;i++)//进位 
        {
            c.s[i+1]+=c.s[i]/10;
            c.s[i]=c.s[i]%10;
        }
        c.clean();
        return c;
    }
    bignum operator - (const bignum &b) //减法 
    { 
        bignum c;  
        c.len=0;  
        for(int i=0,g=0;i<len;i++)  
        {  
            int x=s[i]-g;  
            if(i<b.len) x-=b.s[i];  
            if(x>=0) g=0;  
            else  
            {  
                g=1;  
                x+=10;  
            }  
            c.s[c.len++]=x;  
        }  
        c.clean();  
        return c;   
    }
    bool operator < (const bignum &b) //利用字符串来比较 
    {  
        if(len!=b.len) return len<b.len;  //如果字符串长度不一样,必定长度长更大 
        for(int i=len-1;i!=-1;i--)  
        {  
            if(s[i]!=b.s[i]) return s[i]<b.s[i];  //相等的话从字符串最后一位(相当于数的首位)开始逐位比较 
        }  
        return false;  
    }  
    bool operator > (const bignum &b)  //同理
    {  
        if(len != b.len) return len > b.len;  
        for(int i = len-1; i != -1; i--)  
        {  
            if(s[i] != b.s[i]) return s[i] > b.s[i];  
        }  
        return false;  
    }
    bool operator == (const bignum &b)  //等于
    {  
        return !(*this>b)&&!(*this<b);  
    }    
    bignum operator / (const bignum &b) //高精除 ,实质上是利用减法的操作 
    {
        bignum c,f=0; 
        for(int i=len-1;i>=0;i--) 
        { 
            f=f*10;
            f.s[0]=s[i];
            while(f==b||f>b)
            {
                f=f-b;
                c.s[i]++;
            }
        }
        c.len=len; 
        c.clean();
        return c; 
    }
    string str() const  //为下面的输出做准备 
    {  
        string temp="";  
        for(int i=0;i<len;i++) temp=char(s[i]+48)+temp;  
        return temp;
    } 
};
istream& operator >> (istream &in,bignum &x) //重载 >> 用cin输入 
{  
    string s;  
    in>>s;  
    x=s.c_str();  //string转化为char
    return in;  
}  
ostream& operator << (ostream &out,const bignum &x)  //重载 << 用cout输出 
{  
    out<<x.str();  
    return out;  
}
int main(){
    bignum a;
    bignum b;
    cin >> a >> b;
    cout << a + b << endl << a - b << endl << a * b << endl << a / b << endl;
}

__int128的读入与输出

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <queue>
using namespace std;

inline __int128 read(){                 //快读
    __int128 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;
}
inline void write(__int128 x){          //快写            
    if(x < 0){
        putchar('-');
        x = -x;
    }
    if(x > 9)
        write(x / 10);
    putchar(x % 10 + '0');
}

int main(){

    __int128 a,b;
    a = read();
    b = read();
    write(a * b);

    return 0;
}

优先队列模拟 multiset

支持特定元素的 pop 和 push

struct multiset {                                                    
    priority_queue < int, vector <int>, greater <int> > q,del;  //小根堆

    inline void upd() {
        while(!del.empty() && q.top()==del.top())
            q.pop(), del.pop();
    }
    inline int top() { return upd(),q.top(); }
    inline int size() { return upd(),q.size()-del.size(); }
    inline void push(int x) { q.push(x); }
    inline void pop(int x) { del.push(x); }
    inline void clear(){
        while(!q.empty())   q.pop();
        while(!del.empty()) del.pop();
    }
};

vs\ code 终端下常用指令

g++ XX.cpp -o A -std=c++14 -Wall -O2 "-Wl,--stack=536870912"

可以开启栈空间至 512MB。

点赞

//
//⠀⠀⠀             ⠀⢸⣿⣿⣿⠀⣼⣿⣿⣦⡀
//⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⠀⠀⠀ ⠀⢸⣿⣿⡟⢰⣿⣿⣿⠟⠁
//⠀⠀⠀⠀⠀⠀⠀⢰⣿⠿⢿⣦⣀⠀⠘⠛⠛⠃⠸⠿⠟⣫⣴⣶⣾⡆
//⠀⠀⠀⠀⠀⠀⠀⠸⣿⡀⠀⠉⢿⣦⡀⠀⠀⠀⠀⠀⠀ ⠛⠿⠿⣿⠃
//⠀⠀⠀⠀⠀⠀⠀⠀⠙⢿⣦⠀⠀⠹⣿⣶⡾⠛⠛⢷⣦⣄⠀
//⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⣧⠀⠀⠈⠉⣀⡀⠀ ⠀⠙⢿⡇
//⠀⠀⠀⠀⠀⠀⢀⣠⣴⡿⠟⠋⠀⠀⢠⣾⠟⠃⠀⠀⠀⢸⣿⡆
//⠀⠀⠀⢀⣠⣶⡿⠛⠉⠀⠀⠀⠀⠀⣾⡇⠀⠀⠀⠀⠀⢸⣿⠇
//⢀⣠⣾⠿⠛⠁⠀⠀⠀⠀⠀⠀⠀⢀⣼⣧⣀⠀⠀⠀⢀⣼⠇ 
//⠈⠋⠁⠀⠀⠀⠀⠀⠀⠀⠀⢀⣴⡿⠋⠙⠛⠛⠛⠛⠛⠁
//⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⣾⡿⠋⠀
//⠀⠀⠀⠀⠀⠀⠀⠀⢾⠿⠋⠀
//