整理一些常用的东西。
1、cincout读入优化
ios::sync_with_stdio(false);
2、“万能头文件”,别忘了命名空间
#include<bits/stdc++.h> using namespace std;
等于
#include <iostream> #include <cstdio> #include <fstream> #include <algorithm> #include <cmath> #include <deque> #include <vector> #include <queue> #include <string> #include <cstring> #include <map> #include <stack> #include <set> using namespace std;
3、随机数函数
srand((unsigned)time(NULL));
n = rand();
srand和rand属于#include<algorithm>
time属于#include<ctime>
4、读入优化
1 int read() 2 { 3 int x=0,f=1;char ch=getchar(); 4 while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();} 5 while(ch>='0'&& ch<='9'){x=x*10+ch-'0';ch=getchar();} 6 return x*f; 7 }
fread的读入优化
inline char nc() { static char buf[100000],*p1 = buf,*p2 = buf; return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2) ? EOF :*p1++; } inline int read() { int x = 0,f = 1;char ch=nc(); for (; ch<'0'||ch>'9'; ch = nc()) if (ch == '-') f = -1; for (; ch>='0'&&ch<='9'; ch = nc()) x = x*10+ch-'0'; return x * f; }
带返回值的读入优化
inline char nc() { static char buf[100000],*p1 = buf,*p2 = buf; return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2) ? EOF :*p1++; } inline bool read(int &x) { x = 0; int f = 1;char ch=nc(); if (ch==EOF) return false; for (; ch<'0'||ch>'9'; ch = nc()) if (ch == '-') f = -1; for (; ch>='0'&&ch<='9'; ch = nc()) x = x*10+ch-'0'; x = x * f; return true; }
自己yy了一种读入优化,跑的飞快、、、
char buf[100000],*p1 = buf,*p2 = buf,ch; #define nc() p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2) ? EOF :*p1++; #define read(x) x=0;ch=nc(); while(!isdigit(ch)) ch=nc(); while(isdigit(ch))x=x*10+ch-'0',ch=nc();
char buf[100000],*_p1 = buf,*_p2 = buf; #define nc() (_p1==_p2&&(_p2=(_p1=buf)+fread(buf,1,100000,stdin),_p1==_p2) ? EOF :*_p1++) inline int read() { int x=0,f=1;char ch=nc();for(;!isdigit(ch);ch=nc())if(ch=='-')f=-1; for (;isdigit(ch);ch=nc())x=x*10+ch-'0';return x*f; }
小数读入优化
double read() { double x = 0, f = 1, y = 0.1; char ch = getchar(); for (; !isdigit(ch)&&ch!='.'; ch=getchar()) if (ch=='-') f = -1; for (; isdigit(ch); ch=getchar()) x = x * 10 + ch - '0'; if (ch == '.') ch = getchar(); for (; isdigit(ch); ch=getchar()) x += (ch - '0') * y, y *= 0.1; return x * f; }
5、c++STL堆定义
1 priority_queue<int>heap;//大根堆 2 3 priority_queue<int, vector<int>, greater<int> >heap;//小根堆
小根堆
struct Node{ int x,dis; bool operator < (const Node &a) const { return dis > a.dis; } Node() {} Node(int a,int b) {x = a,dis = b;} };
大根堆
struct Node{ int x,dis; bool operator < (const Node &a) const { return dis < a.dis; } Node() {} Node(int a,int b) {x = a,dis = b;} };
6、Windows与Linux的long long类型的输入输出。
1 #ifdef WIN32 2 #define LL "%I64d" 3 #else 4 #define LL "%Ild" 5 #endif
7、结构体赋值,赋初值
快速赋值
struct Edge{ int to,w,nxt; Edge(){} Edge(int x,int y,int z){to = x,w = y,nxt = z;} }e[M];
赋初值
struct Point { double x,y; Point() {x=1;y=2;} };
8、二进制统计0/1常用函数
__builtin_popcount(unsigned int n) 二进制下1个个数
__builtin_ffs(unsigned int n) 末尾最后一个1的位置
__builtin_ctz(unsigned int n) 末尾0的个数
__builtin_clz(unsigned int n) 前导0的个数
9、枚举子集
for (int i = s; i; i = (i - 1) &s)
复杂度$3^n$
10、O(1)统计2^30一个数的二进制下1的个数。
int Calc(int x) { return num[x >> 15] + num[x & ((1 << 15) - 1)]; } void init() { for (int i = 1; i < (1 << 13); ++i) num[i] = num[i >> 1] + (i & 1); }
11、莫队的神奇排序方法
bool operator < (const Que &A,const Que &B) { return bel[A.l] < bel[B.l] || (bel[A.l] == bel[B.l] && ((bel[A.l] & 1) ? A.r < B.r : A.r > B.r)); }
----------------------------------------------------------------------------