记录编号 415954 评测结果 AAAAAAAAAAAAAAA
题目名称 数列操作B 最终得分 100
用户昵称 Gravatarxzcxzc11 是否通过 通过
代码语言 C++ 运行时间 4.415 s
提交时间 2017-06-18 22:59:04 内存使用 1.07 MiB
显示代码纯文本
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <string>
  5. #include <iomanip>
  6. //#define DEBUG
  7.  
  8. /*
  9. seg_tree :: calculate the sum number
  10. */
  11.  
  12. using namespace std;
  13.  
  14. const int INF = 0;
  15.  
  16. class Seg_tree_node;
  17. typedef Seg_tree_node *seg_pointer;
  18.  
  19. class Seg_tree_node
  20. {
  21. public:
  22. long long val;
  23. int left, right;
  24. bool is_leaf;
  25. seg_pointer lchild, rchild;
  26. long long lazy;
  27.  
  28. Seg_tree_node() { lazy = 0; } //default constractor
  29. void create(long long arr[], int left, int right);
  30. long long query(int l, int r);
  31. void push_down();
  32. void wide_modify(int l, int r, long long v);
  33. };
  34.  
  35. inline long long mselect(const long long a, const long long b)
  36. {
  37. return a + b;
  38. }
  39.  
  40. void Seg_tree_node::create(long long arr[], int l, int r)
  41. {
  42. if (l == r) //leaf node
  43. {
  44. val = arr[l];
  45. left = right = l;
  46. is_leaf = true;
  47. lchild = rchild = NULL;
  48. return;
  49. }
  50. else
  51. {
  52. left = l;
  53. right = r;
  54. int m = (left + right) >> 1;
  55. is_leaf = false;
  56. lchild = new Seg_tree_node;
  57. rchild = new Seg_tree_node;
  58. lchild->create(arr, left, m);
  59. rchild->create(arr, m + 1, right);
  60. val = mselect(lchild->val, rchild->val);
  61. }
  62. }
  63.  
  64. long long Seg_tree_node::query(int ql, int qr)
  65. {
  66. if (qr < left || ql > right)
  67. {
  68. return INF;
  69. }
  70.  
  71. if (lazy != 0)
  72. {
  73. push_down();
  74. }
  75. if (ql <= left && right <= qr)
  76. {
  77. return val;
  78. }
  79. int m = (ql + qr) >> 1;
  80. return mselect(lchild->query(ql, qr), rchild->query(ql, qr));
  81. }
  82.  
  83. void Seg_tree_node::push_down()
  84. {
  85. #ifdef DEBUG
  86. if (lazy == 0)
  87. {
  88. cout << "err push down:lazy_tag is 0" << endl;
  89. return;
  90. }
  91. #endif // DEBUG
  92.  
  93. if (!is_leaf)
  94. {
  95. lchild->val += lazy * (lchild->right - lchild->left + 1); //if the seg_tree type isn't the same,then change this part
  96. rchild->val += lazy * (rchild->right - rchild->left + 1); //if the seg_tree type isn't the same,then change this part
  97. lchild->lazy += lazy;
  98. rchild->lazy += lazy;
  99. }
  100. lazy = 0;
  101. }
  102.  
  103. void Seg_tree_node::wide_modify(int l, int r, long long v)
  104. {
  105. if (right < l || left > r)
  106. {
  107. return;
  108. }
  109. if (l <= left && right <= r)
  110. {
  111. val += v * (right - left + 1); //if the seg_tree type isn't the same,then change this part
  112. lazy += v;
  113. return;
  114. }
  115. if (!is_leaf)
  116. {
  117. if (lazy != 0)
  118. {
  119. push_down();
  120. }
  121. lchild->wide_modify(l, r, v);
  122. rchild->wide_modify(l, r, v);
  123. val = mselect(lchild->val, rchild->val);
  124. }
  125. }
  126.  
  127. const int maxn = 100001;
  128. long long A[maxn];
  129.  
  130. int main()
  131. {
  132. #ifndef DEBUG
  133. freopen("shulieb.in", "r", stdin);
  134. freopen("shulieb.out", "w", stdout);
  135. ios::sync_with_stdio(0);
  136. #endif
  137. int n;
  138. cin >> n;
  139. for (int i = 1; i <= n; ++i)
  140. {
  141. cin >> A[i];
  142. }
  143. Seg_tree_node root;
  144. root.create(A, 1, n);
  145. int m;
  146. cin >> m;
  147. char cmd[20];
  148. int a, b, c;
  149. for (int i = 1; i <= m; ++i)
  150. {
  151. cin >> cmd;
  152. if (cmd[0] == 'A')
  153. {
  154. cin >> a >> b >> c;
  155. root.wide_modify(a, b, c);
  156. }
  157. else
  158. {
  159. cin >> a;
  160. cout << root.query(a, a) << endl;
  161. }
  162. }
  163. return 0;
  164. }