ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Code Style

2021-08-29 09:01:47  阅读:126  来源: 互联网

标签:Style Code int sum cnt si low define


RT。

时间:21/8/29

  1. 万能头(除了某神仙OJ

  2. 快读板子封装(不怎么写)

  3. 代码块和代码块,空行区别。

  4. 大括号不换行。

  5. for里register (如果卡register 就不写

  6. while 和 if 如果后面能只用一行就不打括号。

  7. 线段树Define来写。

  8. 写图论喜欢写 u,v

  9. ++i 和 i++ 随缘

  10. 一道题如果要多个数据结构,会用namespace封装(或者注释一行等号来分割)。

  11. 如果不是撞关键字基本不大写。

  12. 讨厌空格(除了压行的if后面)。

  13. 能连等直接连着写(除了UB)。

  14. 同类型(功能)操作一般压在一行用逗号隔开一下。

  15. 尖括号能不空格就不空。

  16. maxn不写,写si(size缩写)

  17. 一个模块的变量定义到一起。

  18. if里多条件要打空格

  19. cnt 这种有多个的话会在后面用下划线标识功能。

  20. 注释:code... // description (两个空格)

  21. cmp死也不写,operator 好。

  22. push_back -> pb ,first -> fr

  23. bool b[] 少用,一般写 bitset<si>b

  24. 英文注释较多

  25. 一般用前向星而且写 Next

  26. 不怎么压行,但一旦压行会舍弃上面的某些条件

  27. 不喜欢手写 min max swap 之类的,除非要卡常。

几份代码:

tarjan:



#include<bits/stdc++.h>
using namespace std;

const int si_n=1e5+10;
const int si_m=1e6+10;
#define pb push_back

struct Edge{
	int ver,Next,head;
}e[si_m];
int cnt_e=0;
void add(int u,int v){
	e[++cnt_e].ver=v,e[cnt_e].Next=e[u].head;
	e[u].head=cnt_e;
}

stack<int>s;
bool ins[si_n]; // in the stack or not
int c[si_n]; //c[x] = the num of SCC which is x iside
vector<int>scc[si_n]; // scc[i] -> all node in i-th scc (information of i-th scc)
int dfn[si_n],low[si_n];
int n,m,cnt_t=0,tot=0; // tot = how many scc in the graph. 

void tarjan(int u){
	dfn[u]=low[u]=++cnt_t;
	s.push(u),ins[u]=true;
	for(register int i=e[u].head;i;i=e[i].Next){
		int v=e[i].ver;
		if(!dfn[v]){
			tarjan(v);
			low[u]=min(low[u],low[v]);
		}
		else if(ins[v]) low[u]=min(low[u],dfn[v]);
	}
	if(dfn[u]==low[u]){
		++tot;
		int x;
		do{
			x=s.top(),s.pop(),ins[x]=false;
			c[x]=tot,scc[tot].pb(x);
		}while(u!=x);
	}
}

Edge edag[si_m];
int cnt_d=0;
void add_n(int u,int v){
	edag[++cnt_d].ver=v,edag[cnt_d].Next=edag[u].head;
	edag[u].head=cnt_d;
}
void contract(){
	for(register int u=1;u<=n;++u){
		for(register int i=e[u].head;i;i=e[i].Next){
			int v=e[i].ver;
			if(c[u]==c[v]) continue;
			add_n(c[u],c[v]);
		}
	}
}

int main(){
	cin>>n>>m;
	for(register int i=1,u,v;i<=m;++i){
		cin>>u>>v;
		add(u,v);
	}
	for(register int i=1;i<=n;++i){
		if(!dfn[i]) tarjan(i);
	}
	//......
}

segment tree:


#include<bits/stdc++.h>
using namespace std;

#define int long long
#define l(o) t[o].l
#define r(o) t[o].r
#define sum(o) t[o].value
#define tag(o) t[o].lazy
#define lson(o) o<<1
#define rson(o) o<<1|1

const int si=4e5+10;
int n,m,a[si];
struct segment_tree{
    int l,r; // Data interval
    int value,lazy; // Information & Lazytag
}t[si*4];

inline void pushdown(int p){
    if(tag(p)){
        sum(lson(p))+=tag(p)*(r(lson(p))-l(lson(p))+1);
        sum(rson(p))+=tag(p)*(r(rson(p))-l(rson(p))+1); // Update information
        tag(lson(p))+=tag(p),tag(rson(p))+=tag(p); // Spread tag 
        tag(p)=0; // Delete
    }return;
}
inline void build(int p,int l,int r){
    l(p)=l,r(p)=r,tag(p)=0;
    if(l==r){
        sum(p)=a[l];return;
    } // Or a[r](value of leaf)
    int mid=(l+r)>>1;
    build(lson(p),l,mid);
    build(rson(p),mid+1,r);
    sum(p)=sum(lson(p))+sum(rson(p)); // Pushup(or minv,maxv,sin,cos...)
}
inline void modify(int p,int x,int val){
    int l=l(p),r=r(p);
    if(l==r){ //Find leaf
        sum(p)=val;return; //Here is CHANGE x to val
    }
    int mid=(l+r)>>1;
    if(x<=mid) modify(lson(p),x,val); // In leftson
    else modify(rson(p),x,val); // In rightson
    sum(p)=sum(lson(p))+sum(rson(p)); // Pushup
}
inline int query(int p,int l,int r){
	int res=0,nl=l(p),nr=r(p); 
    if(l<=nl && nr<=r) return sum(p);
    pushdown(p);
    int mid=(nl+nr)>>1;
    if(l<=mid) res+=query(lson(p),l,r);
    if(r>mid) res+=query(rson(p),l,r);
    return res;
}
inline void update(int p,int l,int r,int v){
    int nl=l(p),nr=r(p);
    if(l<=nl && nr<=r){
        sum(p)+=v*(nr-nl+1);tag(p)+=v;
        return;
    }// Here is to ADD v for all elements in range[l,r]
    pushdown(p); 
    int mid=(nl+nr)>>1;
    if(l<=mid) update(lson(p),l,r,v);
    if(r>mid) update(rson(p),l,r,v);
    sum(p)=sum(lson(p))+sum(rson(p));
}

signed main(){
    scanf("%lld%lld",&n,&m);
    for(register int i=1;i<=n;++i){
       scanf("%lld",&a[i]);
    }
    build(1,1,n);
    while(m--){
        int op,l,r,k;
        scanf("%lld%lld%lld",&op,&l,&r);
        if(op==1){
            scanf("%lld",&k);
            update(1,l,r,k);
        }
        else printf("%lld\n",query(1,l,r));
    }
    return 0;
}

如果以前的代码和当前马蜂不一样以现在为准。

标签:Style,Code,int,sum,cnt,si,low,define
来源: https://www.cnblogs.com/BTeqwq/p/code-st.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有