NOIp常用黑科技

· · 个人记录

前言

黑科技,在这里指的是C++STL里的函数,以及各种奇妙的优化方法。

吸氧

吸氧,顾名思义开O_x优化,当然这在联赛里是禁用的,但在平常练习中用用也可以。

#pragma GCC optimize("O1")
#pragma GCC optimize("O2")
#pragma GCC optimize("O3")

分别是三个O优化的代码,牺牲编译时间换运行速度,正好弥补了STL的慢速度。

小标志

inline标志

在函数名前面加上关键词inline即可。

inline int doing();

不要试着把这东西用到main函数里去。

这个东西最好用于max,min,lowbit等常用函数中。

register标志

在循环变量名前加上这个东西就可以。

for (register int i=1;i<=n;++i)

快读

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

加快读入速度,比scanf快多了。

常用的函数

lowbit

树状数组专用。

int lowbit(x){
    return x&(-x);
}

max,min

这个总不用说了吧。

深度遍历

void getdepth(int x,int fa){
    depth[x]=depth[fa]+1;
    for (int i=c[x];i;i=b[i]){
        getdepth(a[i],x);
    }
}

调用

getdepth(root,0);

邻接表

void add(int x,int y,int z){
    tot++;a[tot]=y;b[tot]=c[x];c[x]=tot;d[tot]=z;
}

用的时候

for (int i=c[x];i;i=b[i])

这样就能遍历一遍,a[i]表示所通向的点,d[i]表示边权。

加边

add(开始点,结束点,边权);

上面的深度遍历就是基于邻接表的。

万能头

#include <bits/stdc++.h>

常用头

#include <queue>
#include <map>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstdio>
#include <set>

map常用用法

map是一种STL库中的函数。是在内部建立红黑树来储存数据。

定义(Map是变量名)

map<int,int> Map;

可以将它看成一个巨大的计数数组。直接可以调用或者修改Map[i]。

其他的函数需要一些可怕的迭代器操作,所以就懒得讲喽!

queue常用用法

queue是一种STL库中的函数。

定义(q是变量名)

queue<int> q;

插入元素

q.push(data);

弹出元素

q.pop();

使用队头元素

print("%d",q.front());

使用队尾元素

print("%d",q.back());

判断是否为空

if (q.empty()==true){

}

查询队伍长度

print("%d",q.size());

sort用法

sort是一种STL库中的排序函数。

以后打

自带快排

sort(a+1,a+n+1);

对a的1~n的数值进行排序

手打的快排

void qsort(int l,int r){
    int i=l,j=r,x,y;
    x=a[(l+r)/2];
    do{
        while (a[i]<x) i++;
        while (x<a[j]) j--;
        if (!(i>j)){
            y=a[i];a[i]=a[j];a[j]=y;
            i++;j--;
        }
    }while (!(i>j));
    if (l<j) qsort(l,j);
    if (i<r) qsort(i,r);
}

set用法

set是一种STL库中的函数。

以后打

heap用法

大根堆定义

priority_queue <int> h;

小根堆定义

priority_queue< int,vector<int>,greater<int> > h 

函数和queue的差不多,只是少了back函数。

还有

h.top();

代替了

h.front();//这是queue的用法
### __int128 一种特别大佬的类型定义。 ~~以后打~~ ### 其他玄学方法 #### 模拟退火中的随机函数 ``` srand(某大佬生日); ``` 比如 ``` srand(20050808); ``` #### 常用的大质数 $10^9+7