rsx's code style

Posted zhltao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了rsx's code style相关的知识,希望对你有一定的参考价值。

My Code style

  • 大括号换行(不得不说这个其实很重要
  • 一律使用 using namespace std;
  • 头文件一般只用 #include <bits/stdc++.h> 确实简洁很多
  • 使用快速读入快速输出,但不使用 fread, fwrite,这个后文会说
  • 尽量使用预编译命令来定义一些简短的无递归的函数。
  • 本地一律使用文件操作
  • ++a,a-- 之类的运算符没有空格
  • 行末尽量没有多余空格
  • 一个函数之内尽量不存在空行,而各个函数之间必须有空行隔开
  • 简短的函数在不用#define 的情况下一律用 inline 内联
  • 例如 +-*/ << >> & ^ ~ % | < > 一律两边空格,而特别的 ! 的右边没有空格
  • () 左括号的左边有空格,右括号的右边有空格
  • 三目运算符两边都有空格
  • 变量名要尽量让人明白意思,禁止出现aa, bb, ii, jj, ll, rr, ldak 之类的名称
  • 单行循环没有大括号,示长度决定是否写在一行
  • 模板类的名称均用为 template <typename KYN>
  • 对于long long之类的长变量名 均使用 typedef long long ll; 而用 #define
  • 针对 ch <= ‘9‘ && ch >= ‘0‘isdigit(ch) 代替
  • 能用位运算尽量位运算 /kk
  • 高级的树形结构用指针实现,并且写类
  • 不使用STL的 stacklist
  • 函数末尾应有return 但是 void 不用
  • 自己的命名空间 namespace RSX_love_KYN
  • 尽量多用 #define 来简化代码,例如 : #defien sit std::string::iterator 保证自己理解即可

示例程序

#include <bits/stdc++.h>

using namespace std;

#define gc() getchar()
#define pc(i) putchar(i)

template <typename KYN>
inline KYN read()
{
	KYN x = 0;
	char ch = gc();
	bool f = 0;
	while(!isdigit(ch))
	{
		f = (ch == ‘-‘);
		ch = gc();
	}
	while(isdigit(ch))
	{
		x = x * 10 + (ch - ‘0‘);
		ch = gc();
	}
	return f ? -x : x;
}

template <typename KYN>
void put(KYN x)
{
	if(x < 0)
	{
		x = -x;
		pc(‘-‘);
	}
	if(x < 10) {
		pc(x + 48);
		return;
	}
	put(x / 10);
	pc(x % 10 + 48);
	return ;
}

#define vit std::vector <int>:: iterator 
#define vi std::vector <int>
#define lbd(i, j, k) lower_bound(i, j, k)
#define pii std::pair <int, int>
#define mkp(i, j) std::make_pair(i, j)
#define lowbit(i) (i & -i)
#define ispow(i) (i == lowbit(i))
#define rdi() read <int> ()
#define rdl() read <long long> ()
#define pti(i) put <int> (i), putchar(‘ ‘)
#define ptl(i) put <long long> (i), putchar(‘ ‘)

typedef long long ll;
typedef double db;
typedef long double ldb;
typedef unsigned long long ull;
typedef unsigned int ui;

const int Maxn = 1e6  + 1e5 + 111;

namespace RSX_love_KYN
{

const int MAXN = 1e6  + 1e5 + 111;

template <typename KYN>
class avlTree
{
	private:
	
	struct avlNode;
	typedef avlNode *avl;
	struct avlNode
	{
		avl ls, rs;
		int size, height, num;
		KYN data;
		void update() 
		{
			this->size = this->ls->size + this->rs->size + this->num;
			this->height = max(this->ls->height , this->rs->height) + 1;
		}
	};
	
	protected:
	avl rot, null, tot, deleted[MAXN];
	avlNode memory[MAXN];
	int deltop;
	inline avl init(KYN x)
	{
		avl tmp = deltop ? deleted[deltop--] : tot++;
		tmp->ls = tmp->rs = null;
		tmp->size = tmp->num = tmp->height = 1;
		tmp->data = x;
		return tmp;
	}

	inline avl Single_left(avl T)
	{
		avl a = T->ls;
		T->ls = a->rs;
		a->rs = T;
		T->update();
		a->update();
		return a;
	}
	
	inline avl Single_right(avl T)
	{
		avl a = T->rs;
		T->rs = a->ls;
		a->ls = T;
		T->update();
		a->update();
		return a;
	}
	
	inline avl double_left(avl T)
	{
		T->ls = Single_right(T->ls);
		return Single_left(T);
	}
	
	inline avl double_right(avl T)
	{
		T->rs = Single_left(T->rs);
		return Single_right(T);
	}
	
	avl insert(avl T, KYN x)
	{
		if(T == null) return init(x);
		if(x == T->data)
		{
			++(T->num);
			T->update();
			return T;
		}
		if(x < T->data)
		{
			T->ls = insert(T->ls, x);
			T->update();
			if(T->ls->height - T->rs->height == 2)
			{
				if(x < T->ls->data) T = Single_left(T);
				else T = double_left(T);
			}
		}
		else
		{
			T->rs = insert(T->rs, x);
			T->update();
			if(T->rs->height - T->ls->height == 2)
			{
				if(T->rs->data < x) T = Single_right(T);
				else T = double_right(T);
			}
		}
		return T;
	}
	
	avl erase(avl T, KYN x)
	{
		if(T == null) return null;
		if(x < T->data)
		{
			T->ls = erase(T->ls, x);
			T->update();
			if(T->rs->height - T->ls->height == 2)
			{
				if(T->rs->rs->height >= T->rs->ls->height) T = Single_right(T);
				else T = double_right(T);
			}
		}
		else if(T->data < x)
		{
			T->rs = erase(T->rs, x);
			T->update();
			if(T->ls->height - T->rs->height == 2)
			{
				if(T->ls->ls->height >= T->ls->rs->height) T = Single_left(T);
				else T = double_left(T);
			}
		}
		else
		{
			if(T->num > 1)
			{
				--(T->num);
				T->update();
				return T;
			}
			if(T->ls != null && T->rs != null)
			{
				avl p = T->rs;
				while(p->ls != null) p = p->ls;
				T->num = p->num;
				T->data = p->data, p->num = 1;
				T->rs = erase(T->rs, T->data);
				T->update();
				if(T->ls->height - T->rs->height == 2)
				{
					if(T->ls->ls->height >= T->ls->rs->height) T = Single_left(T);
					else T = double_left(T);
				}
			}
			else
			{
				avl p = T;
				if(T->ls != null) T = T->ls;
				else if(T->rs != null) T = T->rs;
				else T = null;
				deleted[++deltop] = p;
			}
		}
		return T;
	}
	
	KYN get_rank(avl T, KYN x) 
	{
		int ans = 0;
		while(T != null)
		{
			if(T->data == x) { return ans + T->ls->size + 1; }
			else if(x < T->data) { T = T->ls; }
			else { ans += T->ls->size + T->num; T = T->rs; }
		}
		return ans + 1;
	}
	
	KYN get_data(avl T, int rank)
	{
		while(T != null)
		{
			if(T->ls->size >= rank) T = T->ls;
			else if(T->ls->size + T->num >= rank) { return T->data; }
			else { rank -= T->num + T->ls->size; T = T->rs; }
		}
	}
	
	avl makeempty(avl x)
	{
		if(x == null) return null;
		x->ls = makeempty(x->ls);
		x->rs = makeempty(x->rs);
		deleted[++deltop] = x;
		return null;
	}
	
	void output(avl x)
	{
		if(x == null) return;
		output(x->ls);
		put <KYN> (x->data);
		putchar(‘ ‘);
		output(x->rs);
	}
	
	avl find(avl T, KYN x)
	{
		while(T != null) {
			if(T->data == x) return T;
			else if(T->data > x) { T = T->ls; }
			else { T = T->rs; }
		}
		return null;
	}
	
	public:	
	KYN prv(KYN x)
	{
		KYN ans = KYN(-1 << 30);
		avl tmp = rot;
		while(tmp != null)
		{
			if(tmp->data == x)
			{
				if(tmp->ls != null)
				{
					tmp = tmp->ls;
					while(tmp->rs != null) tmp = tmp->rs;
					ans = tmp -> data;
				}
				break;
			}
			if(tmp->data < x && ans < tmp->data) ans = tmp->data;
			tmp = tmp->data < x ? tmp->rs : tmp->ls;
		}
		return ans;
	}
	
	KYN next(KYN x) {
		KYN ans = KYN(1 << 30);
		avl tmp = rot;
		while(tmp != null)
		{
			if(tmp->data == x)
			{
				if(tmp->rs != null)
				{
					tmp = tmp->rs;
					while(tmp->ls != null) tmp = tmp->ls;
					ans = tmp->data;
				}
				break;
			}
			if(x < tmp->data && tmp->data < ans) ans = tmp->data;
			tmp = tmp->data < x ? tmp->rs : tmp->ls;
		}
		return ans;
	}
	
	avlTree()
	{
		deltop = 0;
		null = new avlNode;
		null->ls = null->rs = null;
		null->size = null->height = null->num = 0;
		rot = null;
		tot = memory;
	}
	
	inline void insert(KYN x) { rot = insert(rot, x); return ; }
	
	inline void erase(KYN x) { rot = erase(rot, x); }
	
	inline int get_rank(KYN x) { return get_rank(rot, x); }
	
	inline KYN get_data(int x) { return get_data(rot, x); }
	
	void clear() { rot = makeempty(rot); }
	
	bool find(KYN x) { return find(rot, x) != null; }
	
	void output() { output(rot); }
	
	KYN operator[] (int k) { return get_data(k); }
	
	int size() { return rot->size; }
};

}

using namespace RSX_love_KYN;

int n, opt, x, m, last = 0, tmp, ans;

avlTree <int> tree;

int main() {
#ifdef _DEBUG
	freopen("P6136_2.in", "r", stdin);
#endif
	n = rdi(); m = rdi();
	while(n--) tree.insert(rdi());
	while(m--) {
		opt = rdi(); x = rdi();
		x ^= last;
		switch (opt)
		{
			case 1: tree.insert(x); break;
			case 2: tree.erase(x); break;
			case 3: last = tree.get_rank(x); ans ^= last; break;
			case 4: last = tree[x]; ans ^= last; break;
			case 5: last = tree.prv(x); ans ^= last; break;
			case 6: last = tree.next(x); ans ^= last; break;
		}
	}
	pti(ans);
	return 0;
}

以上是关于rsx's code style的主要内容,如果未能解决你的问题,请参考以下文章

代码规范 for node.js with 'npm-coding-style'

What's the difference between @Component, @Repository & @Service annotations in Spring?(代码片段

VSCode自定义代码片段——声明函数

VSCode自定义代码片段8——声明函数

VSCode自定义代码片段——CSS动画

VSCode自定义代码片段3——url大全