[关闭]
@Yeasion-Nein 2018-08-06T21:50:58.000000Z 字数 3656 阅读 692

的码风修改历程

总之,今天是一个值得纪念的伟大日子,我将自己的码风进行了彻底的修改,大概是参考了Pks和的码风,分为以下几点。

1.变量名。在所有的计算符号之前和之后加空格。如:"&","|","!","","","",""这些运算符。两个相邻的计算符之间不加空格。

原版:

  1. while(c<'0'||c>'9') c=getchar();

后版:

  1. while(c < '0' || c > '9') c = getchar();

2.逗号。每一个逗号之后都跟一个空格,比如:

  1. LL A, B, C, Mark, N, M, Value[MAXN];

3.变量名。尽可能多的使用大写字符和下划线。

例:原版线段树1

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<algorithm>
  5. #define MAXN 1000010
  6. using namespace std;
  7. long long value[MAXN],tag[MAXN];
  8. struct point
  9. {
  10. long long left;
  11. long long right;
  12. long long sum;
  13. }edge[MAXN*4];
  14. #define leftson now*2
  15. #define rightson now*2+1
  16. void update(long long now)
  17. {
  18. edge[now].sum=edge[leftson].sum+edge[rightson].sum;
  19. }
  20. void build(long long left,long long right,long long now)
  21. {
  22. if(left==right)
  23. {
  24. edge[now].sum=value[left];
  25. return ;
  26. }
  27. long long mid=(left+right)/2;
  28. build(left,mid,now*2);
  29. build(mid+1,right,now*2+1);
  30. update(now);
  31. }
  32. void put(long long now,long long mid,long long left,long long right)
  33. {
  34. if(tag[now])
  35. {
  36. tag[leftson]+=tag[now];
  37. tag[rightson]+=tag[now];
  38. edge[leftson].sum+=(mid-left+1)*tag[now];
  39. edge[rightson].sum+=(right-mid)*tag[now];
  40. tag[now]=0;
  41. }
  42. }
  43. void change(long long left,long long right,long long now,long long v,long long now_left,long long now_right)
  44. {
  45. if(now_left<=left)
  46. if(now_right>=right)
  47. {
  48. tag[now]+=v;
  49. edge[now].sum+=(right-left+1)*v;
  50. return ;
  51. }
  52. long long mid=(left+right)/2;
  53. put(now,mid,left,right);
  54. if(now_left<=mid)
  55. change(left,mid,now*2,v,now_left,now_right);
  56. if(mid<now_right)
  57. change(mid+1,right,now*2+1,v,now_left,now_right);
  58. update(now);
  59. }
  60. long long ask(long long left,long long right,long long now,long long now_left,long long now_right)
  61. {
  62. long long ans=0;
  63. if(now_left<=left)
  64. if(now_right>=right)
  65. return edge[now].sum;
  66. long long mid=(left+right)/2;
  67. put(now,mid,left,right);
  68. if(mid>=now_left)
  69. ans+=ask(left,mid,now*2,now_left,now_right);
  70. if(mid<now_right)
  71. ans+=ask(mid+1,right,now*2+1,now_left,now_right);
  72. return ans;
  73. }
  74. int main()
  75. {
  76. long long n,m;
  77. scanf("%lld%lld",&n,&m);
  78. for(long long i=1;i<=n;i++)
  79. scanf("%lld",&value[i]);
  80. build(1,n,1);
  81. for(long long i=1;i<=m;i++)
  82. {
  83. long long p; scanf("%lld",&p);
  84. if(p==1)
  85. {
  86. long long x,y,z;
  87. scanf("%lld%lld%lld",&x,&y,&z);
  88. change(1,n,1,z,x,y);
  89. }
  90. else if(p==2)
  91. {
  92. long long x,y;
  93. scanf("%lld%lld",&x,&y);
  94. printf("%lld\n",ask(1,n,1,x,y));
  95. }
  96. }
  97. return 0;
  98. }

后版

  1. #include <iostream>
  2. #include <cstdio>
  3. #define MAXN 1000100
  4. #define LL long long
  5. #define Re register
  6. #define LS (now << 1)
  7. #define RS (now << 1 | 1)
  8. #define Mid ((L + R) >>1)
  9. #define E_Mid ((Edge[now].L + Edge[now].R) >> 1)
  10. using namespace std;
  11. inline void Read(LL & x){
  12. char c = getchar(); x = 0;
  13. while(c < '0' || c > '9') c = getchar();
  14. while(c <= '9' && c >= '0')
  15. x = x * 10 + c - 48, c = getchar();
  16. }
  17. inline void Print(LL x){
  18. int num = 0; char c[15];
  19. while(x) c[++num]=(x % 10) + 48, x /= 10;
  20. while(num) putchar(c[num --]);
  21. putchar('\n');
  22. }
  23. struct Node{
  24. LL L, R, Sum, Tag;
  25. }Edge[MAXN << 1];
  26. LL A, B, C, Mark, N, M, Value[MAXN];
  27. inline void Push_Up(LL now){
  28. Edge[now].Sum = Edge[LS].Sum + Edge[RS].Sum;
  29. }
  30. inline void Push_Down(LL now){
  31. if(Edge[now].Tag){
  32. Edge[LS].Tag += Edge[now].Tag, Edge[RS].Tag += Edge[now].Tag;
  33. Edge[LS].Sum += Edge[now].Tag * (Edge[LS].R - Edge[LS].L + 1);
  34. Edge[RS].Sum += Edge[now].Tag * (Edge[RS].R - Edge[RS].L + 1);
  35. Edge[now].Tag = 0;
  36. }
  37. }
  38. void Build_Tree(LL now, LL L, LL R){
  39. if(L == R){
  40. Edge[now].L = L, Edge[now].R = R;
  41. Edge[now].Sum = Value[L];
  42. return ;
  43. }
  44. Edge[now].L = L; Edge[now].R = R;
  45. Build_Tree(RS, Mid + 1, R); Build_Tree(LS, L, Mid);
  46. Push_Up(now);
  47. }
  48. void Add(LL now, LL L, LL R, LL K){
  49. if(Edge[now].L >= L && Edge[now].R <= R){
  50. Edge[now].Tag += K;
  51. Edge[now].Sum += K * (Edge[now].R - Edge[now].L + 1);
  52. return ;
  53. } Push_Down(now);
  54. if(E_Mid >= L) Add(LS, L, R, K);
  55. if(E_Mid < R) Add(RS, L, R, K);
  56. Push_Up(now);
  57. }
  58. LL Query(LL now, LL L, LL R){
  59. if(Edge[now].L >= L && Edge[now].R <= R)
  60. return Edge[now].Sum;
  61. Push_Down(now); LL Ans = 0;
  62. if(E_Mid >= L) Ans += Query(LS, L, R);
  63. if(E_Mid < R) Ans += Query(RS, L, R);
  64. return Ans;
  65. }
  66. int main(){
  67. Read(N); Read(M);
  68. for(int i = 1; i <= N; i ++)
  69. Read(Value[i]);
  70. Build_Tree(1, 1, N);
  71. for(int i = 1; i <= M; i ++){
  72. Read(Mark);
  73. if(Mark == 1){
  74. Read(A); Read(B); Read(C);
  75. Add(1, A, B, C);
  76. } else{
  77. Read(A); Read(B);
  78. Print(Query(1, A, B));
  79. }
  80. } return 0;
  81. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注