rsx's code style

My Code style

  • Line breaks in braces (had to say that this is actually very important
  • Always use using namespace std;
  • Header file normally just #include <bits/stdc++.h>a lot of really simple
  • Use fast read-in and fast output, but do not use it fread, fwrite, which will be described later
  • Try to use precompiled commands to define some short non-recursive functions.
  • Use file operations locally
  • ++a, And a--the like have no spaces
  • Try not to have extra spaces at the end of the line
  • There should be no blank lines within a function, and there must be blank lines between each function
  • In short do not function #defineunder all circumstances with inlineinline
  • For example, +-*/ << >> & ^ ~ % | < > all the spaces on both sides, but especially the !right side with no spaces
  • () There is a space on the left of the left bracket and a space on the right of the right bracket
  • The trinocular operator has spaces on both sides
  • Variable names should be as clear as possible, aa, bb, ii, jj, ll, rr, ldakand names that are not allowed to appear
  • Single-line loops do not have curly brackets, indicating the length determines whether to write on one line
  • Template class names are used as template <typename KYN>
  • For long longlong variable names and the like are using typedef long long ll;and use#define
  • For ch <= '9' && ch >= '0'use isdigit(ch)in place of
  • Able to use bit operation as much as possible bit operation / kk
  • The advanced tree structure is implemented with pointers, and write classes
  • Do not use the STL stackandlist
  • End of the function should have returnbut voiddo not
  • Own namespace namespace RSX_love_KYN
  • Try to use more #defineto simplify the code, for example: #defien sit std::string::iteratorensure that they can understand

Sample program

#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;
}

Guess you like

Origin www.cnblogs.com/zhltao/p/12728443.html