约定
对于一些表示必要性的关键词,从 must 到 mustn’t 排序如下:
必须 > 尽量 > 应当 > 建议 > 可以 > 不建议 > 不应当 > 尽量不 > 不得。
为方便阅读,本文中所有上述关键词都用粗体字表示。
另外,若没有「至少」、「至多」等词的限定,所有的数字默认为严格的。 例如「中间有一个空行」中的「有一个」默认为「有且仅有一个」。
基本码风
syc 的码风一般情况下是紧凑的,尤其是打比赛,syc 码风全部是:
#include<bits/stdc++.h>
using namespace std;
int w[500005*4],a[500005],lzj[500005*4],lzc[500005*4],p;
int qpow(int a,int b){
if(b==1)return a%p;
if(b<=0)return 1;
int cnt=qpow(a,b/2);
if(b%2==1)return 1LL*cnt*cnt%p*a%p;
return 1LL*cnt*cnt%p;
}
void pushup(int u){
w[u]=(w[u*2]+w[u*2+1])%p;
}
void build(int u,int l,int r){
lzc[u]=1;
lzj[u]=0;
if(l==r){
w[u]=a[l]%p;
return;
}
int m=(l+r)/2;
build(u*2,l,m);
build(u*2+1,m+1,r);
pushup(u);
}
bool in(int l,int r,int L,int R){
return (L<=l)&&(r<=R);
}
bool of(int l,int r,int L,int R){
return (r<L)||(R<l);
}
void pushdown(int u,int l,int r){
int m=(l+r)/2;
w[u*2]=(1LL*w[u*2]*lzc[u]%p+1LL*lzj[u]*(m-l+1)%p)%p;
lzc[u*2]=1LL*lzc[u*2]*lzc[u]%p;
lzj[u*2]=(1LL*lzj[u*2]*lzc[u]%p+lzj[u])%p;
w[u*2+1]=(1LL*w[u*2+1]*lzc[u]%p+1LL*lzj[u]*(r-m)%p)%p;
lzc[u*2+1]=1LL*lzc[u*2+1]*lzc[u]%p;
lzj[u*2+1]=(1LL*lzj[u*2+1]*lzc[u]%p+lzj[u])%p;
lzc[u]=1;
lzj[u]=0;
}
void uqd1(int u,int l,int r,int x,int p){
if(l==r){
w[u]=x%p;
return;
}
pushdown(u,l,r);
int m=(l+r)/2;
if(p<=m)uqd1(u*2,l,m,x,p);
else uqd1(u*2+1,m+1,r,x,p);
pushup(u);
}
int q1(int u,int l,int r,int p){
if(l==r)return w[u];
pushdown(u,l,r);
int m=(l+r)/2;
if(p<=m)return q1(u*2,l,m,p);
else return q1(u*2+1,m+1,r,p);
}
void uqd2(int u,int l,int r,int L,int R,int x){
if(in(l,r,L,R)){
w[u]=(w[u]+1LL*(r-l+1)*x%p)%p;
lzj[u]=(lzj[u]+x)%p;
return;
}
if(of(l,r,L,R))return;
pushdown(u,l,r);
int m=(l+r)/2;
uqd2(u*2,l,m,L,R,x);
uqd2(u*2+1,m+1,r,L,R,x);
pushup(u);
}
void uqd3(int u,int l,int r,int L,int R,int x){
if(in(l,r,L,R)){
w[u]=1LL*w[u]*x%p;
lzc[u]=1LL*lzc[u]*x%p;
lzj[u]=1LL*lzj[u]*x%p;
return;
}
if(of(l,r,L,R))return;
pushdown(u,l,r);
int m=(l+r)/2;
uqd3(u*2,l,m,L,R,x);
uqd3(u*2+1,m+1,r,L,R,x);
pushup(u);
}
int q2(int u,int l,int r,int L,int R){
if(in(l,r,L,R))return w[u];
if(of(l,r,L,R))return 0;
pushdown(u,l,r);
int m=(l+r)/2;
return (q2(u*2,l,m,L,R)+q2(u*2+1,m+1,r,L,R))%p;
}
int main(){
int n,q,m;
cin>>n>>q>>m;
p=m;
for(int i=1;i<=n;++i){
cin>>a[i];
}
build(1,1,n);
while(q--){
int op,x,y,k;
cin>>op;
if(op==1){
cin>>x>>y>>k;
uqd3(1,1,n,x,y,k);
}else if(op==2){
cin>>x>>y>>k;
uqd2(1,1,n,x,y,k);
}else if(op==3){
cin>>x>>y;
cout<<q2(1,1,n,x,y)<<endl;
}
}
}可以看到,非常不喜欢打空格(尽量),一般会有一些 define 来简化程序,特别爱写 #define int long long,写主席树之类的会写 define lc ...,define rc ...(应当),个人认为我的代码比较清晰可读。并且会用 using namespace std(必须),并且非常不喜欢封装之类的(尽量,是因为有的快读函数)。会压行,调试会比较快(建议),我的大括号几乎全是紧凑风格的(必须)。
关于 syc 码风的故事
但是呢,跟你们讲,一次 syc 闲的尝试写了一下多空格的码风,就是这个题,几乎是把自己不喜欢的码风全写了一遍,那个难受啊,调试也贼慢,所以以后再也不写这样的码风了,可以给你们看看我当时写的代码以及题解,超级抽风。
如何让自己的代码变得可读?
这个是写给大家的,有人可能喜欢这种码风:
#include<bits/stdc++.h>
using namespace std;
struct edge {
int to, nxt;
} e[205];
int hd[205], tot, n, m;
int f[105][105];
void add(int u, int v) {
e[++tot].to = v;
e[tot].nxt = hd[u];
hd[u] = tot;
}
void dfs(int fa, int u) {
f[u][0] = 1;
for (int i = hd[u]; i != 0; i = e[i].nxt) {
f[u][1]++;
}
for (int i = hd[u]; i != 0; i = e[i].nxt) {
int v = e[i].to;
if (v == fa)continue;
dfs(v, u);
for (int j = 1; j <= m; ++j) {
f[u][j] += f[v][j - 1];
}
}
}
int main() {
cin >> n >> m;
for (int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
add(u, v);
add(v, u);
}
dfs(1, 0);
cout << f[1][m];
}当然可以,只要不是因为空格引发的编译错误即可(听说写这种代码的一般都是有工业写代码经历的,Oier 觉得自己这么写比较爽也可以)。
当然我觉得紧凑派是比较多的,调试快,所谓可读,就是让你的代码很好调试,一眼就能看懂那一段是什么意思,我的可读标准是:
- 不要写过于长的变量名,这很丑陋,我觉得在看代码的时候有一种碍眼的感觉,除非你有把握可以和小草神写的一样漂亮。调试还要检查变量是否有拼写错误,不太好。
- 但是也不要写无意义的变量名,比如
a,b,ld,kd,sa,这样当你写了 代码左右就会开始疯,我的个人习惯是写cnt,ans,f,lsti,j,k,n,m这种约定俗成的变量名,毕竟这些变量民都是刻在 Oier 骨子里的。 - 不要乱空格/空行,比如:
for (...){
int x;
cin>>x;
}// 调试这种我会疯,也容易括号不匹配
综上是 syc 对于码风的一些见解,拜拜~