vector - 手写链表
vector和手写链表的时间对比
push_back时间如下
.. | 不开O2 | 开O2 |
---|---|---|
vector | 8s | 6s |
手写链表 | 4s | 4s |
遍历一次的时间都是2s
详见代码
#pragma GCC optimize(2)
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<ctime>
#define aut(v,j) for (unsigned int j=0;j<v.size();j++)
#define rep(a,b,c) for (int a=b;a<=c;a++)
#define per(a,b,c) for (int a=b;a>=c;a--)
#define go(u) for (int o=ft[u],v;v=E[o].t;o=E[o].n)
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef pair<int,int> par;
typedef double dob;
const int lim=3000000,n=100000;
unsigned int seed=19260817;
vector <int> to[n+10];
inline int num(){
seed=(seed<<17)^(seed<<5)^seed;
return seed%n+1;
}
#define gettime printf("%d\n",time(0))
int main(){
gettime;
rep(_,1,100){
rep(i,1,n) to[i].clear();
rep(i,1,lim) to[num()].push_back(num());
}
gettime;
rep(_,1,1000){
rep(i,1,n) aut(to[i],j) to[i][j]=233;
}
gettime;
return 0;
}
#pragma GCC optimize(2)
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<ctime>
#define aut(v,j) for (unsigned int j=0;j<v.size();j++)
#define rep(a,b,c) for (int a=b;a<=c;a++)
#define per(a,b,c) for (int a=b;a>=c;a--)
#define go(u) for (int o=ft[u],v;v=E[o].t;o=E[o].n)
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef pair<int,int> par;
typedef double dob;
const int lim=3000000,n=100000;
unsigned int seed=19260817;
struct edge{int t,n;}E[lim+10];
int vis[n+10],ft[n+10],tot;
inline int num(){
seed=(seed<<17)^(seed<<5)^seed;
return seed%n+1;
}
void add(int x,int y){
E[++tot]=(edge){y,ft[x]},ft[x]=tot;
}
#define gettime printf("%d\n",time(0))
int main(){
gettime;
rep(_,1,100){
tot=0;
memset(ft,0,sizeof(ft));
rep(i,1,lim) add(num(),num());
}
gettime;
rep(_,1,1000){
tot=0;
rep(i,1,n) go(i) vis[v]=233;
}
gettime;
return 0;
}
预处理log - math库log
一开始担心math库的log太慢,拖慢rmq速度
然而测试了一下,速度几乎一样,在1e次随机查询下都是9s
开O2都没有变快
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<ctime>
#include<cmath>
#define rep(a,b,c) for (int a=b;a<=c;a++)
#define per(a,b,c) for (int a=b;a>=c;a--)
#define go(u) for (int o=ft[u],v;v=E[o].t;o=E[o].n)
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef pair<int,int> par;
typedef double dob;
int lo[1000010];
inline int rd(){return rand()*10000+rand();}
int main(){
int lim=100000000,n=1000000;
printf("%d\n",time(0));
rep(i,2,n) lo[i]=lo[i>>1]+1;
rep(i,1,lim){
int x=lo[(rd()%n+n)%n+1];
i+=x;
i-=x;
}
printf("%d\n",time(0));
rep(i,1,lim){
int x=(rd()%lim+lim)%lim+1,
y=(int)(log((double)(x))/log(2.0));
i+=y;
i-=y;
}
printf("%d\n",time(0));
return 0;
}