[模板] 杂项:二分/离散化/随机数/其他
离散化
int *ptr[nsz],pp=0,inv[nsz],pi=0;
bool cmp(int *a,int *b){return (*a)<(*b);}
void uniq(){
sort(ptr+1,ptr+pp+1,cmp);
inv[pi]=-1;
rep(i,1,pp){
if((*ptr[i])!=inv[pi])inv[++pi]=ptr[i];
*ptr[i]=pi;
}
}
二分
int sol(){
int l=l0,r=r0,mid,ans=0;
while(l<=r){
mid=(l+r)>>1;
if(jud(mid))ans=mid,r=mid-1;
else l=mid+1;
}
return ans;
}
精确时间
#include<sys/time.h>
timeval a;
gettimeofday(&a,0);
ll t0 = a.tv_sec*1000000+a.tv_usec; //microsecond
随机数
用微秒来设随机数种子.
获取随机数取模即可.
当RAND_MAX较小 (如32768) 时, 可以分别随机高位和低位.
void sr(){
timeval a;
gettimeofday(&a,0);
srand(a.tv_sec*1000000+a.tv_usec);
}
int getr(int l,int r){return rand()%(r-l+1)+l;}
int rand1(int l,int r){return rand()*RAND_MAX+rand();}
指定精度输出
#include<iomanip>
cout<<fixed<<setprecision(5);
//cout<<(double)...
取整
#include<cmath>
floor(v);
ceil(v);
round(v);
//using (a-1)/b+1 is wrong when a/b is negative
//neither (a+(b/2))/b
//round(xxx) to avoid '4.99 -> 4'
改变栈大小
# Windows
## the default stack size limit is 2MB
## gcc mingw
### linker commands:
### unit: byte
-Wl,--stack=1024000000
## MSVC
#pragma comment(linker, "/STACK:16777216")
# linux
## show the default stack size limit; which is 8MB
ulimit -s
## set size; unit: KB
ulimit -s 102400
ulimit -s ulimited
## set memory size; unit: KB
ulimit -v 512000