Advertisement
Dennnhhhickk

Untitled

Nov 6th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.22 KB | None | 0 0
  1. // CC.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <cmath>
  7. #include <algorithm>
  8. #include <iomanip>
  9. #include <string>
  10. #include <fstream>
  11. #include <string>
  12.  
  13. using namespace std;
  14.  
  15. typedef long long ll;
  16. typedef unsigned long long ull;
  17. typedef string str;
  18.  
  19. class Do {
  20. public:
  21.     ll x, l, r, Add;
  22.     Do *L, *R;
  23.    
  24.     Do()
  25.     {
  26.         x = 0;
  27.         Add = 0;
  28.         l = 0;
  29.         r = 0;
  30.         L = NULL;
  31.         R = L;
  32.     }
  33.  
  34.     friend Do *push(Do *);
  35.     friend Do *build(Do *, ll, ll);
  36.     friend void rec(Do *);
  37.     friend ll query(Do *, ll, ll);
  38.     friend Do *modify(Do *, ll, ll);
  39.     friend Do *modify(Do *, ll, ll, ll);
  40.  
  41. };
  42.  
  43. ll a[100000];
  44.  
  45. Do *push(Do *);
  46. Do *build(Do *, ll, ll);
  47. void rec(Do *);
  48. ll query(Do *, ll, ll);
  49. Do *modify(Do *, ll, ll);
  50. Do *modify(Do *, ll, ll, ll);
  51.  
  52. int main()
  53. {
  54.     Do *t = new Do();
  55.     //После ввода массива a размером n
  56.     t = build(t, 0, n - 1);
  57.     system("pause");
  58.     return 0;
  59. }
  60.  
  61. Do *push(Do *t)
  62. {
  63.     if (t->Add)
  64.     {
  65.         t->x += t->Add * (t->r - t->l + 1);
  66.         if (t->l != t->r)
  67.         {
  68.             t->L->Add = t->Add;
  69.             t->R->Add = t->Add;
  70.         }
  71.         t->Add = 0;
  72.     }
  73.     return t;
  74. }
  75.  
  76. Do *build(Do *t, ll l, ll r)
  77. {
  78.     if (l == r)
  79.     {
  80.         t->x = a[l];
  81.         t->l = l;
  82.         t->r = r;
  83.         return t;
  84.     }
  85.     ll m = (r + l) / 2;
  86.     t->L = new Do();
  87.     t->R = new Do();
  88.     t->l = l;
  89.     t->r = r;
  90.     t->L = build(t->L, l, m);
  91.     t->R = build(t->R, m + 1, r);
  92.     t->x = t->L->x + t->R->x;
  93.     return t;
  94. }
  95.  
  96. void rec(Do *t)
  97. {
  98.     push(t);
  99.     if (t->l == t->r)
  100.     {
  101.         return;
  102.     }
  103.     rec(t->L);
  104.     rec(t->R);
  105. }
  106.  
  107. ll query(Do *t, ll l, ll r)
  108. {
  109.     push(t);
  110.     if (r < t->l || t->r < l)
  111.         return 0;
  112.     if (l <= t->l && t->r <= r)
  113.         return t->x;
  114.     return query(t->L, l, r) + query(t->R, l, r);
  115. }
  116.  
  117. Do *modify(Do *t, ll pos, ll val)
  118. {
  119.     push(t);
  120.     if (t->l == t->r)
  121.     {
  122.         t->x += val;
  123.         return t;
  124.     }
  125.     if (t->L->r <= pos)
  126.         t->L = modify(t->L, pos, val);
  127.     else
  128.         t->R = modify(t->R, pos, val);
  129.     t->x = t->L->x + t->R->x;
  130.     return t;
  131. }
  132.  
  133. Do *modify(Do *t, ll l, ll r, ll val)
  134. {
  135.     if (r < t->l || t->r < l)
  136.         return t;
  137.     if (l <= t->l && t->r <= r)
  138.     {
  139.         t->Add = val;
  140.         push(t);
  141.         return t;
  142.     }
  143.     modify(t->L, l, r, val);
  144.     modify(t->R, l, r, val);
  145.     t->x = t->L->x + t->R->x;
  146.     return t;
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement