某天打了一下 CF,遇到了一道 https://codeforces.com/contest/1806/problem/E

这里需要卡常。

于是在 C++20(64) 下测出来了一些神奇的结果。

结果

都测了两回

序号 方法 时间 1(ms) 时间 2 (ms)
1 正常数组 + 正常函数 607 592
2 vector + lambda 1060 1154
3 正常数组 + lambda 638 733
4 array + 正常函数 577 623

可以简单地得出如下结论:

  • vector 在卡常题中请尽可能不要使用
  • lambda 函数与正常函数比有较小常数
  • array 居然可以在评测机抖动的时候比正常数组还快,可以近似认为一样快

一、正常数组

#include<bits/stdc++.h>
using namespace std; typedef long long ll;
typedef double db;
typedef long double ld; #define IL inline
#define fi first
#define se second
#define mk make_pair
#define pb push_back
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(), (x).end()
#define dbg1(x) cout << #x << " = " << x << ", "
#define dbg2(x) cout << #x << " = " << x << endl template<typename Tp> IL void read(Tp &x) {
x=0; int f=1; char ch=getchar();
while(!isdigit(ch)) {if(ch == '-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
x *= f;
}
int buf[42];
template<typename Tp> IL void write(Tp x) {
int p = 0;
if(x < 0) { putchar('-'); x=-x;}
if(x == 0) { putchar('0'); return;}
while(x) {
buf[++p] = x % 10;
x /= 10;
}
for(int i=p;i;i--) putchar('0' + buf[i]);
} const int N = 100000 + 5;
const int Block_sz = 325;
const int B = 320;
int a[N], pa[N], dep[N], cnt[N], id[N];
ll ump[N][Block_sz];
vector<int> G[N]; ll GetAns(int u, int v) {
if(!u || !v) return 0;
if(u > v) swap(u, v);
if(cnt[dep[u]] <= B && ump[u][id[v]]) return ump[u][id[v]];
ll ret = GetAns(pa[u], pa[v]) + 1ll * a[u] * a[v];
if(cnt[dep[u]] <= B) ump[u][id[v]] = ret;
return ret;
}
void dfs(int u, int fa) {
dep[u] = dep[fa] + 1; id[u] = ++cnt[dep[u]];
for(int v : G[u]) {
dfs(v, u);
}
} void solve() {
int n, q; read(n); read(q);
for(int i=1;i<=n;i++) read(a[i]);
for(int i=2;i<=n;i++) {
read(pa[i]);
G[pa[i]].pb(i);
}
dfs(1, 0);
while(q--) {
int u, v; read(u); read(v);
write(GetAns(u, v)); putchar(10);
}
} int main() {
#ifdef LOCAL
freopen("test.in", "r", stdin);
// freopen("test.out", "w", stdout);
#endif
int T = 1;
// read(T);
while(T--) solve();
return 0;
}

二、vector + lambda 函数

#include<bits/stdc++.h>
using namespace std; typedef long long ll;
typedef double db;
typedef long double ld; #define IL inline
#define fi first
#define se second
#define mk make_pair
#define pb push_back
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(), (x).end()
#define dbg1(x) cout << #x << " = " << x << ", "
#define dbg2(x) cout << #x << " = " << x << endl template<typename Tp> IL void read(Tp &x) {
x=0; int f=1; char ch=getchar();
while(!isdigit(ch)) {if(ch == '-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
x *= f;
}
int buf[42];
template<typename Tp> IL void write(Tp x) {
int p = 0;
if(x < 0) { putchar('-'); x=-x;}
if(x == 0) { putchar('0'); return;}
while(x) {
buf[++p] = x % 10;
x /= 10;
}
for(int i=p;i;i--) putchar('0' + buf[i]);
} void solve() {
int n, q; read(n); read(q);
vector<int> a(n + 1, 0), pa(n + 1, 0), dep(n + 1, 0), cnt(n + 1, 0), id(n + 1, 0);
vector<vector<int> > G(n + 1);
for(int i=1;i<=n;i++) read(a[i]);
for(int i=2;i<=n;i++) {
read(pa[i]);
G[pa[i]].pb(i);
}
const int Block_sz = sqrt(100000);
vector<vector<ll> > ump(n + 1, vector<ll>(Block_sz + 5));
function<ll(int, int)> GetAns = [&](int u, int v) -> ll {
if(!u || !v) return 0;
if(u > v) swap(u, v);
if(cnt[dep[u]] <= Block_sz && ump[u][id[v]]) return ump[u][id[v]];
ll ret = GetAns(pa[u], pa[v]) + 1ll * a[u] * a[v];
if(cnt[dep[u]] <= Block_sz) ump[u][id[v]] = ret;
return ret;
};
function<void(int, int) > dfs = [&](int u, int fa) -> void {
dep[u] = dep[fa] + 1; id[u] = ++cnt[dep[u]];
for(int v : G[u]) {
dfs(v, u);
}
};
dfs(1, 0);
while(q--) {
int u, v; read(u); read(v);
write(GetAns(u, v)); putchar(10);
}
} int main() {
#ifdef LOCAL
freopen("test.in", "r", stdin);
// freopen("test.out", "w", stdout);
#endif
int T = 1;
// read(T);
while(T--) solve();
return 0;
}

三、正常数组 + lambda

#include<bits/stdc++.h>
using namespace std; typedef long long ll;
typedef double db;
typedef long double ld; #define IL inline
#define fi first
#define se second
#define mk make_pair
#define pb push_back
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(), (x).end()
#define dbg1(x) cout << #x << " = " << x << ", "
#define dbg2(x) cout << #x << " = " << x << endl template<typename Tp> IL void read(Tp &x) {
x=0; int f=1; char ch=getchar();
while(!isdigit(ch)) {if(ch == '-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
x *= f;
}
int buf[42];
template<typename Tp> IL void write(Tp x) {
int p = 0;
if(x < 0) { putchar('-'); x=-x;}
if(x == 0) { putchar('0'); return;}
while(x) {
buf[++p] = x % 10;
x /= 10;
}
for(int i=p;i;i--) putchar('0' + buf[i]);
}
const int N = 100000 + 5;
const int Block_sz = 320;
const int B = 320;
int n, q;
int a[N], pa[N], dep[N], cnt[N], id[N];
ll ump[N][Block_sz+5];
vector<int> G[N];
void solve() {
read(n); read(q);
for(int i=1;i<=n;i++) read(a[i]);
for(int i=2;i<=n;i++) {
read(pa[i]);
G[pa[i]].pb(i);
}
function<ll(int, int)> GetAns = [&](int u, int v) -> ll {
if(!u || !v) return 0;
if(u > v) swap(u, v);
if(cnt[dep[u]] <= Block_sz && ump[u][id[v]]) return ump[u][id[v]];
ll ret = GetAns(pa[u], pa[v]) + 1ll * a[u] * a[v];
if(cnt[dep[u]] <= Block_sz) ump[u][id[v]] = ret;
return ret;
};
function<void(int, int) > dfs = [&](int u, int fa) -> void {
dep[u] = dep[fa] + 1; id[u] = ++cnt[dep[u]];
for(int v : G[u]) {
dfs(v, u);
}
};
dfs(1, 0);
while(q--) {
int u, v; read(u); read(v);
write(GetAns(u, v)); putchar(10);
}
} int main() {
#ifdef LOCAL
freopen("test.in", "r", stdin);
// freopen("test.out", "w", stdout);
#endif
int T = 1;
// read(T);
while(T--) solve();
return 0;
}

四、array + 正常函数

#include<bits/stdc++.h>
using namespace std; typedef long long ll;
typedef double db;
typedef long double ld; #define IL inline
#define fi first
#define se second
#define mk make_pair
#define pb push_back
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(), (x).end()
#define dbg1(x) cout << #x << " = " << x << ", "
#define dbg2(x) cout << #x << " = " << x << endl template<typename Tp> IL void read(Tp &x) {
x=0; int f=1; char ch=getchar();
while(!isdigit(ch)) {if(ch == '-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
x *= f;
}
int buf[42];
template<typename Tp> IL void write(Tp x) {
int p = 0;
if(x < 0) { putchar('-'); x=-x;}
if(x == 0) { putchar('0'); return;}
while(x) {
buf[++p] = x % 10;
x /= 10;
}
for(int i=p;i;i--) putchar('0' + buf[i]);
} const int N = 100000 + 5;
const int Block_sz = 325;
const int B = 320;
array<int, N> a, pa, dep, cnt, id;
array<array<ll, Block_sz>, N> ump;
vector<int> G[N]; ll GetAns(int u, int v) {
if(!u || !v) return 0;
if(u > v) swap(u, v);
if(cnt[dep[u]] <= B && ump[u][id[v]]) return ump[u][id[v]];
ll ret = GetAns(pa[u], pa[v]) + 1ll * a[u] * a[v];
if(cnt[dep[u]] <= B) ump[u][id[v]] = ret;
return ret;
}
void dfs(int u, int fa) {
dep[u] = dep[fa] + 1; id[u] = ++cnt[dep[u]];
for(int v : G[u]) {
dfs(v, u);
}
} void solve() {
int n, q; read(n); read(q);
for(int i=1;i<=n;i++) read(a[i]);
for(int i=2;i<=n;i++) {
read(pa[i]);
G[pa[i]].pb(i);
}
dfs(1, 0);
while(q--) {
int u, v; read(u); read(v);
write(GetAns(u, v)); putchar(10);
}
} int main() {
#ifdef LOCAL
freopen("test.in", "r", stdin);
// freopen("test.out", "w", stdout);
#endif
int T = 1;
// read(T);
while(T--) solve();
return 0;
}

简单测下C++20 vector array lambda 的常数的更多相关文章

  1. C++中的数组array和vector,lambda表达式,C字符串加操作,C++中新类型数组(数组缓存),多元数组,new缓冲

     使用C++风格的数组.不须要管理内存. array要注意不要溢出,由于它是栈上开辟内存. array适用于不论什么类型 #include<iostream> #include< ...

  2. Array.apply(null, {length: 20})和Array(20)的理解

    话说今晚在学习Vue.js教程里:Render函数,这一章节是发现了一个问题,就是利用下面的这个render函数可以渲染20个重复的段落: render: function (createElemen ...

  3. Codeforces Global Round 6D(VECTOR<ARRAY<INT,3> >)

    一个人只要存在债务关系,那么他的债务可以和这整个债务关系网中任何人连边,和他当初借出或欠下的人没有关系.只需要记录他的债务值即可. #define HAVE_STRUCT_TIMESPEC #incl ...

  4. 简单聊下Unicode和UTF-8

    今晚听同事分享提到这个,简单总结下. ## Unicode字符集 Unicode的出现是因为ASCII等其他编码码不够用了,比如ASCII是英语为母语的人发明的,只要一个字节8位就能够表示26个英文字 ...

  5. 简单聊下IO复用

    没图,不分析API Java中IO API的发展:Socket -> SocketChannel -> AsynchronousSocketChannelServerSocket -> ...

  6. Ubuntu_10.04下Hadoop-0.20.2集群配置手册

    Ubuntu_10.04下Hadoop-0.20.2集群配置手册 一.软硬件环境的准备 下面的文章来自hadoopor.com,我先交待一下我自己的环境: 两台机器,每台机器上面两个虚机(vmware ...

  7. JQuery -&gt; 超级简单的下拉菜单

    使用jquery实现一个超级简单的下拉菜单. 效果图 最初的效果 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvRmVlTGFuZw==/font/5a6L ...

  8. 简单分析下用yii2的yii\helpers\Html类和yii.js实现的post请求

    yii2提供了很多帮助类,比如Html.Url.Json等,可以很方便的实现一些功能,下面简单说下这个Html.用yii2写view时时经常会用到它,今天在改写一个页面时又用到了它.它比较好用的地方就 ...

  9. Example017简单的下拉框

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. 简单说下 Winform 的分页快速开发框架必须要实现的几个功能之一

    简单说下 Winform 的分页快速开发框架必须要实现的几个功能之一 分页非为前端分页  和 后端分页,前端分页只有适用于B/S,B/S的呈现速度远远不如C/S,而C/S则没有这个问题,所以分页必然是 ...

随机推荐

  1. 2018-11-19-win10-uwp-使用-Azure-DevOps-自动构建

    title author date CreateTime categories win10 uwp 使用 Azure DevOps 自动构建 lindexi 2018-11-19 15:26:4 +0 ...

  2. C# - 能否让 SortedSet.RemoveWhere 内传入的委托异步执行

    TL;DR; 若想充分利用 RemoveWhere 带来的性能优势,建议传入判断是否删除元素的委托内采取同步操作.若一定要在该委托内使用异步操作,可以采用本文中绕行的方法,但摈弃了 RemoveWhe ...

  3. C语言程序设计-笔记2-分支结构

    C语言程序设计-笔记2-分支结构 例3-1  简单的猜数游戏.输入你所猜的整数(假定1-100),与计算机产生的被猜数比较,若相等,显示猜中:若不等,显示与被猜数的大小关系. /*简单的猜数游戏*/ ...

  4. vue使用promise.all异步实现多个请求完成之后在执行某些操作

    使用场景:多个请求方法拿到数据之后需要对这不同的数据进行比较,之后在输出并渲染 思路:使用promise.all()异步操作: Promise.all([ //上架 new Promise((reso ...

  5. MindSpore强化学习:使用PPO配合环境HalfCheetah-v2进行训练

    本文分享自华为云社区<MindSpore强化学习:使用PPO配合环境HalfCheetah-v2进行训练>,作者: irrational. 半猎豹(Half Cheetah)是一个基于Mu ...

  6. DB2查找最耗时SQL

    两种方法:db2top和snapshot for dynamic sql 1. db2top -d <dbname>

  7. arduino uno+LCD12864(ST7735S)+蓝牙模块实现贪吃蛇

    1.前言: 1.1本实验实现的贪吃蛇能穿越边界,结束游戏的唯一条件是贪吃蛇到达指定长度 1.2本实验所用LCD可能不是LCD12864,LCD12864所用库为u8glib,笔者在词库中并没有找到型号 ...

  8. ansible系列(25)--ansible的notify和handlers

    1. notify和handlers Handlers 是一个触发器,同时是一个特殊的 tasks ,它无法直接运行,它需要被tasks 通知后才会运行.比如: httpd 服务配置文件发生变更,我们 ...

  9. 大数据之Hadoop集群的HDFS压力测试

    测试HDFS写性能 原文:sw-code 1)写测试的原理 2)测试内容:向HDFS集群写10个128MB的文件(3个机器每个4核,2 * 4 = 8 < 10 < 3 * 4 =12) ...

  10. Django模型层Models的使用步骤

    1.安装pymysql(这里使用MySQL数据库) pip install pymysql 2.在Django的工程同名子目录的__init__.py文件中添加如下语句 from pymysql im ...