代码风格规范(线段树2)
教授要求规范代码风格,具体要求如下:
- 引用库命令和库名称之间加空格
例如
#include <cstdio>
- 运算符号两侧要加空格
例如
int a = 9 + 6;
- 大括号换不换行均可(但我倾向换行)
例如
for(int i = 0 ; i < N ; i ++)
{
// do something...
}
- 函数名采用大驼峰式命名法
例如
inline int GetSum(int a[], const int len)
{
int sum = 0;
for(int i = 0 ; i < len ; i ++)
sum += a[i];
return sum;
}
- 变量名采用小驼峰式命名法
例如
int leftSon;
- 逗号前不加空格,逗号后加空格
例如
int leftSon, rightSon;
- 代码内多加注释,变量名不要太随意
下面以 线段树2为例,展示整体的代码规范
```cpp
#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#define int long long
namespace Zhang_Tao
{
inline int Read_Int(int &x)
{
x = 0;
int fh = 1;
char ch = getchar();
while(!isdigit(ch))
{
if(ch == '-')
fh = -1;
ch = getchar();
}
while(isdigit(ch))
{
x = x * 10 + ch -'0';
ch = getchar();
}
return x *= fh;
}
//Read an int fastly
void Write_Int(int x)
{
if(x < 0)
putchar('-'), x *= -1;
if(x > 9)
Write_Int(x / 10);
putchar(x % 10 + 48);
}
//Write an int Fastly
inline int Get_GCD(const int x, const int y)
{
return y ? Get_GCD(y, x % y) : x;
}
//Get GCD of x and y
inline void Swap(int &x, int &y)
{
x ^= y ^= x ^= y;
}
//Change two numbers
}//namespace Zhang_Tao
using namespace Zhang_Tao;
using namespace std;
#ifndef Maxn
#define Maxn 100005
#endif
int N, M, Mod;
int Array[Maxn];
class SegmentTree
{
public:
static SegmentTree *BuildTree(const int L, const int R)
{
SegmentTree *node = new SegmentTree;
#ifndef Node
#define Node node ->
#endif
Node l = L, Node r = R;
Node addTag = 0, Node plusTag = 1;
if(L == R)
{
Node sum = Array[L];
Node lson = Node rson = nullptr;
}
else
{
int Mid = (L + R) >> 1;
Node lson = BuildTree(L, Mid);
Node rson = BuildTree(Mid + 1, R);
Node Update();
}
#undef Node
return node;
}
inline bool InRange(const int L, const int R)
{
return (L <= l) && (r <= R);
}
inline bool OutofRange(const int L, const int R)
{
return (r < L) || (R < l);
}
inline void Update()
{
sum = (lson -> sum + rson -> sum) % Mod;
}
inline void MaketagAdd(const int V)
{
int Add = (r - l + 1) * V % Mod;
sum = (Add + sum) % Mod;
addTag = (addTag + V) % Mod;
}
inline void MaketagPlus(const int V)
{
sum = sum * V % Mod;
addTag = addTag * V % Mod;
plusTag = plusTag * V % Mod;
}
inline void PushDown()
{
if(plusTag != 1)
{
lson -> MaketagPlus(plusTag);
rson -> MaketagPlus(plusTag);
plusTag = 1;
}
if(addTag)
{
lson -> MaketagAdd(addTag);
rson -> MaketagAdd(addTag);
addTag = 0;
}
}
//Modify function, type = 1, plus mode ; type = 2, add mode.
void Modify(const int L, const int R, const int Type, const int V)
{
if(InRange(L, R))
{
if(Type == 1)
MaketagPlus(V);
else
MaketagAdd(V);
}
else if(! OutofRange(L, R))
{
PushDown();
lson -> Modify(L, R, Type, V);
rson -> Modify(L, R, Type, V);
Update();
}
}
//Query function, to get sum of every elements in [L, R].
int Query(const int L,const int R)
{
if(InRange(L, R))
return sum;
if(OutofRange(L, R))
return 0;
PushDown();
int Lsum = lson -> Query(L, R);
int Rsum = rson -> Query(L, R);
return (Lsum + Rsum) % Mod;
}
private:
int l, r;
int addTag, plusTag;
int sum;
SegmentTree *lson, *rson;
}*Root;
signed main()
{
Read_Int(N), Read_Int(M), Read_Int(Mod);
for(int i = 1 ; i <= N ; i ++)
Read_Int(Array[i]);
Root = SegmentTree::BuildTree(1, N);
for(int i = 0 ; i < M ; i ++)
{
int L, R, Type, V;
Read_Int(Type), Read_Int(L), Read_Int(R);
if(Type != 3)
Root -> Modify(L, R, Type, Read_Int(V));
else
Write_Int(Root -> Query(L, R)), putchar('\n');
}
return 0;
}
繁华尽处,
寻一静谧山谷,
筑一木制小屋,
砌一青石小路,
与你晨钟暮鼓,
安之若素。