SHARE
    TWEET
    Vince14

    /<> 1275 (sqrt decomposition)

    Sep 9th, 2023
    145
    0
    Never
    Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
    C++ 1.77 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 = 1000005;
    23. int N, Q;
    24. long long arr[MAXN];
    25. int sq;
    26. long long sq_arr[MAXN];
    27. void build_sqrt(){
    28. for(int i = 0; i < N; i++){
    29. sq_arr[i / sq] += arr[i];
    30. }
    31. }
    32. long long query(int x, int y){
    33. int xdec = x / sq;
    34. int ydec = y / sq;
    35. long long ret = 0;
    36. if(xdec == ydec){
    37. for(int i = x; i <= y; i++){
    38. ret += arr[i];
    39. }
    40. return ret;
    41. }
    42. for(int i = x; i < (xdec + 1) * sq; i++){
    43. ret += arr[i];
    44. }
    45. for(int i = xdec + 1; i < ydec; i++){
    46. ret += sq_arr[i];
    47. }
    48. for(int i = ydec * sq; i <= y; i++){
    49. ret += arr[i];
    50. }
    51. return ret;
    52. }
    53. void update(int a, long long b){
    54. sq_arr[a / sq] -= arr[a];
    55. sq_arr[a / sq] += b;
    56. arr[a] = b;
    57. }
    58. int main() {
    59. FAST;
    60. cin >> N >> Q;
    61. for(int i = 0; i < N; i++){
    62. cin >> arr[i];
    63. }
    64. sq = (int) sqrt(N);
    65. build_sqrt();
    66. for(int i = 0; i < Q; i++){
    67. int x, y, a;
    68. long long b;
    69. cin >> x >> y >> a >> b;
    70. x--; y--; a--;
    71. if(x > y){
    72. swap(x, y);
    73. }
    74. cout << query(x, y) << "\n";
    75. update(a, b);
    76. }
    77. }
    78. /*
    79. 5 2
    80. 1 2 3 4 5
    81. 2 3 3 1
    82. 3 5 4 1
    83. */
    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 によって変換されたページ (->オリジナル) /