推荐快读快写模版
2026-02-12 17:56:48
发布于:浙江
快读不是我的,别骂我!
这个快读不是我自己的,但是可见效果显著(提升了接近 1s)。所以打算帮忙宣传一下捏。
原帖子(原帖主开源了应该没事吧
使用方法很简单。把这一段粘贴到 int main 前面。读入的时候这样写: read(你要读入的变量1,你要读入的变量2,…)。似乎可以无限添加变量(?)。这里常用的变量类型基本能往里面放,非常的方便(不过注意,这里支持 double 但不支持 float,其他不支持的变量类型请看原贴)。快写是一样的。
别嫌代码长,放到前面粘贴就行。在卡常的时候非常好用捏。
被人胁迫了,P3378 源代码,同时可以参考一下快读的使用方法:
#include <bits/stdc++.h>
using namespace std;
#define int long long
//*************************
#ifdef __linux__
#define gc getchar_unlocked
#define pc putchar_unlocked
#else
#define gc _getchar_nolock
#define pc _putchar_nolock
#endif
inline bool blank(const char x) {return !(x^32)||!(x^10)||!(x^13)||!(x^9);}
template<typename Tp> inline void read(Tp &x) {x=0; register bool z=true; register char a=gc(); for(;!isdigit(a);a=gc()) if(a=='-') z=false; for(;isdigit(a);a=gc()) x=(x<<1)+(x<<3)+(a^48); x=(z?x:~x+1);}
inline void read(double &x) {x=0.0; register bool z=true; register double y=0.1; register char a=gc(); for(;!isdigit(a);a=gc()) if(a=='-') z=false; for(;isdigit(a);a=gc()) x=x*10+(a^48); if(a!='.') return x=z?x:-x,void(); for(a=gc();isdigit(a);a=gc(),y/=10) x+=y*(a^48); x=(z?x:-x);}
inline void read(char &x) {for(x=gc();blank(x)&&(x^-1);x=gc());}
inline void read(char *x) {register char a=gc(); for(;blank(a)&&(a^-1);a=gc()); for(;!blank(a)&&(a^-1);a=gc()) *x++=a; *x=0;}
inline void read(string &x) {x=""; register char a=gc(); for(;blank(a)&&(a^-1);a=gc()); for(;!blank(a)&&(a^-1);a=gc()) x+=a;}
template<typename T,typename ...Tp> inline void read(T &x,Tp &...y) {read(x),read(y...);}
template<typename Tp> inline void write(Tp x) {if(!x) return pc(48),void(); if(x<0) pc('-'),x=~x+1; register int len=0; register char tmp[64]; for(;x;x/=10) tmp[++len]=x%10+48; while(len) pc(tmp[len--]);}
inline void write(const double x) {register int a=6; register double b=x,c=b; if(b<0) pc('-'),b=-b,c=-c; register double y=5*powl(10,-a-1); b+=y,c+=y; register int len=0; register char tmp[64]; if(b<1) pc(48); else for(;b>=1;b/=10) tmp[++len]=floor(b)-floor(b/10)*10+48; while(len) pc(tmp[len--]); pc('.'); for(c*=10;a;a--,c*=10) pc(floor(c)-floor(c/10)*10+48);}
inline void write(const pair<int,double>x) {register int a=x.first; if(a<7) {register double b=x.second,c=b; if(b<0) pc('-'),b=-b,c=-c; register double y=5*powl(10,-a-1); b+=y,c+=y; register int len=0; register char tmp[64]; if(b<1) pc(48); else for(;b>=1;b/=10) tmp[++len]=floor(b)-floor(b/10)*10+48; while(len) pc(tmp[len--]); a&&(pc('.')); for(c*=10;a;a--,c*=10) pc(floor(c)-floor(c/10)*10+48);} else cout<<fixed<<setprecision(a)<<x.second;}
inline void write(const char x) {pc(x);}
inline void write(const bool x) {pc(x?49:48);}
inline void write(char *x) {fputs(x,stdout);}
inline void write(const char *x) {fputs(x,stdout);}
inline void write(const string &x) {fputs(x.c_str(),stdout);}
template<typename T,typename ...Tp> inline void write(T x,Tp ...y) {write(x),write(y...);}
//***********************************
class Heap{
private:
int heap[1000005],siz=0;
public:
void push(int x){
int son,pa;
heap[++siz]=x;
son=siz;
while(son>1){
pa=son/2;
if(heap[pa]<=heap[son])break;
swap(heap[pa],heap[son]);
son=pa;
}
}int top(){
return heap[1];
}
void pop(){
int pa,son;
heap[1]=heap[siz--];
pa=1;
while(pa*2<=siz){
son=2*pa;
if(son<siz and heap[son+1]<heap[son])son++;
if(heap[pa]<=heap[son])break;
swap(heap[pa],heap[son]);
pa=son;
}
}int size(){
return siz;
}bool empty(){
return !siz;
}
};
signed main(){
Heap heap;
int t;
read(t);
while(t--){
int op;
read(op);
if(op==1){
int x;
read(x);
heap.push(x);
}else if(op==2){
write(heap.top(),'\n');
}else{
heap.pop();
}
}
return 0;
}
这里用快读搭配手写堆可以获得较快的运行速度。其中分割线之间的是快读快写部分。
这个代码我又优化了一下,最终跑到了 147ms,已经很不错了。
全部评论 7
or
1周前 来自 广东
1d
1周前 来自 浙江
0原来现在版本T0成评测波动了吗……洛谷现在实际运行次数不优化好像是没法超别人的
1周前 来自 广东
0你这也没最优解啊
1周前 来自 广东
0C++里面是的吧,我再看看
1周前 来自 浙江
0居然被秋裤抄了
1周前 来自 浙江
0一个月前都有人跑到110ms左右了
1周前 来自 广东
0
1周前 来自 浙江
0顶顶顶
1周前 来自 浙江
01周前 来自 浙江
0你这代码太慢了
1周前 来自 广东
0我偷你这个试一试
1周前 来自 广东
0(
1周前 来自 浙江
0


































有帮助,赞一个