SHARE
    TWEET
    Vince14

    /<> 16975 (lazy propagation seg)

    Sep 23rd, 2023
    164
    0
    Never
    Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
    C++ 2.24 KB | Source Code | 0 0
    1. #include <iostream>
    2. #include <string>
    3. #include <cstring>
    4. #include <algorithm>
    5. #include <cmath>
    6. #include <vector>
    7. #include <set>
    8. #include <map>
    9. #include <stack>
    10. #include <queue>
    11. #include <deque>
    12. #include <unordered_map>
    13. #include <numeric>
    14. #include <iomanip>
    15. using namespace std;
    16. #define pii pair<long long, long long>
    17. #define ll long long
    18. #define FAST ios_base::sync_with_stdio(false); cin.tie(NULL)
    19. const long long dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
    20. const long long dl[2] = {1, -1};
    21. const long long MOD = 1000000007;
    22. const long long MAXN = 100005;
    23. int N, M;
    24. int arr[MAXN];
    25. long long seg[MAXN * 4], lazy[MAXN * 4];
    26. void prop(int x, int s, int e){
    27. if(lazy[x] == 0) return;
    28. seg[x] += lazy[x] * (e + 1 - s);
    29. if(s == e){
    30. lazy[x] = 0;
    31. }
    32. else{
    33. lazy[x * 2] += lazy[x];
    34. lazy[x * 2 + 1] += lazy[x];
    35. lazy[x] = 0;
    36. }
    37. }
    38. void build(int x, int s, int e){
    39. if(s == e){
    40. seg[x] = arr[s];
    41. return;
    42. }
    43. int mid = (s + e)/2;
    44. build(x * 2, s, mid);
    45. build(x * 2 + 1, mid + 1, e);
    46. seg[x] = seg[x * 2] + seg[x * 2 + 1];
    47. }
    48. void update(int x, int s, int e, int a, int b, int add){
    49. prop(x, s, e);
    50. if(e < a or b < s) return;
    51. if(a <= s and e <= b){
    52. lazy[x] += add;
    53. prop(x, s, e);
    54. return;
    55. }
    56. int mid = (s + e)/2;
    57. update(x * 2, s, mid, a, b, add);
    58. update(x * 2 + 1, mid + 1, e, a, b, add);
    59. seg[x] = seg[x * 2] + seg[x * 2] + 1;
    60. }
    61. long long query(int x, int s, int e, int idx){
    62. prop(x, s, e);
    63. if(idx < s or idx > e) return 0;
    64. if(s == e){
    65. return seg[x];
    66. }
    67. int mid = (s + e)/2;
    68. return query(x * 2, s, mid, idx) + query(x * 2 + 1, mid + 1, e, idx);
    69. }
    70. int main() {
    71. FAST;
    72. cin >> N;
    73. for(int i = 1; i <= N; i++){
    74. cin >> arr[i];
    75. }
    76. build(1, 1, N);
    77. cin >> M;
    78. for(int a, i = 1; i <= M; i++){
    79. cin >> a;
    80. if(a == 1){
    81. int b, c, d;
    82. cin >> b >> c >> d;
    83. update(1, 1, N, b, c, d);
    84. }
    85. else{
    86. int b;
    87. cin >> b;
    88. cout << query(1, 1, N, b) << "\n";
    89. }
    90. }
    91. }
    92. /*
    93. 5
    94. 1 2 3 4 5
    95. 4
    96. 1 3 4 6
    97. 2 3
    98. 1 1 3 -2
    99. 2 3
    100. */
    Advertisement
    Add Comment
    Please, Sign In to add comment
    Public Pastes
    We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
    Not a member of Pastebin yet?
    Sign Up, it unlocks many cool features!

    AltStyle によって変換されたページ (->オリジナル) /