给健忘的自己的板子(持续更新ing)

加速输入输出流

ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);

map嵌套pair

typedef pair<int,int> p;
map<p,int> mp;

mp[p(dx,dy)]++;

逆元板子

ll ny(ll a,ll k,ll mod){
    
     
    ll res=1;
    while(k){
    
    
        if(k&1) res=(ll)res*a%mod;
        a=(ll)a*a%mod;
        k/=2;
    }
    return res;
}

//调用为ny(a,mod-2,mod) 其中a为分母

pair排序

//根据first的值升序排序
bool cmp1(pair<int,int>a,pair<int,int>b)
{
    
    
    return a.first < b.first;
}
 
//根据second的值升序排序
bool cmp2(pair<int, int>a, pair<int, int>b)
{
    
    
    return a.second < b.second;
}

vector<pair<int, int>>vec;

sort(vec.begin(), vec.end(), cmp1);
cout << "根据first的值升序排序:" << endl;
for (auto it = vec.begin();it != vec.end();it++)
    cout << "(" << it->first << "," << it->second << ")" << endl;

sort(vec.begin(), vec.end(), cmp2);
cout << "根据second的值升序排序:" << endl;
for (auto it = vec.begin();it != vec.end();it++)
    cout << "(" << it->first << "," << it->second << ")" << endl;



1ll

ll 即为 long long ,用于将某 int 类型转化为 long long 类型的操作,类似于 2 / 3.0 为将 2 转化为 double 类型后再进行运算。

重载运算符

//例:按区间的左端点排序

struct Range
{
    
    
    int l, r;
    bool operator< (const Range &W)const
    {
    
    
        return r < W.r;
    }
}range[N];

//------------------
scanf("%d%d", &range[i].l, &range[i].r);
sort(range, range + n);

文件读取输入

	freopen("C:\\Users\\86189\\Desktop\\tt.txt","r",stdin);
	freopen("C:\\Users\\86189\\Desktop\\out.txt","w",stdout);

邻接表

除h外开两倍大小

void add(int a,int b,int c){
    
    
e[idx]=b;w[idx] = c;ne[idx]=h[a];h[a]=idx++;
}
//其中c为边权

for (int i = h[t] ; i ! = -1;  i = ne [i] )
	int j=e [ i ]  // j 为当前点

二分查找函数

lower_bound( begin,end,num):从数组的begin位置到end-1位置二分查找第一个大于或等于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。

upper_bound( begin,end,num):从数组的begin位置到end-1位置二分查找第一个大于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。

在从大到小的排序数组中,重载lower_bound()upper_bound()

lower_bound( begin,end,num,greater() ):从数组的begin位置到end-1位置二分查找第一个小于或等于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。

upper_bound( begin,end,num,greater() ):从数组的begin位置到end-1位置二分查找第一个小于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。

猜你喜欢

转载自blog.csdn.net/laysan/article/details/120463658
今日推荐