Dennnhhhickk

Дерево отрезков на указателях

Nov 6th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.20 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.    
  56.     system("pause");
  57.     return 0;
  58. }
  59.  
  60. Do *push(Do *t)
  61. {
  62.     if (t->Add)
  63.     {
  64.         t->x += t->Add * (t->r - t->l + 1);
  65.         if (t->l != t->r)
  66.         {
  67.             t->L->Add += t->Add;
  68.             t->R->Add += t->Add;
  69.         }
  70.         t->Add = 0;
  71.     }
  72.     return t;
  73. }
  74.  
  75. Do *build(Do *t, ll l, ll r)
  76. {
  77.     if (l == r)
  78.     {
  79.         t->x = a[l];
  80.         t->l = l;
  81.         t->r = r;
  82.         return t;
  83.     }
  84.     ll m = (r + l) / 2;
  85.     t->L = new Do();
  86.     t->R = new Do();
  87.     t->l = l;
  88.     t->r = r;
  89.     t->L = build(t->L, l, m);
  90.     t->R = build(t->R, m + 1, r);
  91.     t->x = t->L->x + t->R->x;
  92.     return t;
  93. }
  94.  
  95. void rec(Do *t)
  96. {
  97.     t = push(t);
  98.     cout << t->x << ' ';
  99.     if (t->l == t->r)
  100.         return;
  101.     rec(t->L);
  102.     rec(t->R);
  103. }
  104.  
  105. ll query(Do *t, ll l, ll r)
  106. {
  107.     t = push(t);
  108.     if (r < t->l || t->r < l)
  109.         return 0;
  110.     if (l <= t->l && t->r <= r)
  111.         return t->x;
  112.     return query(t->L, l, r) + query(t->R, l, r);
  113. }
  114.  
  115. Do *modify(Do *t, ll pos, ll val)
  116. {
  117.     t = push(t);
  118.     if (t->l == t->r)
  119.     {
  120.         t->x += val;
  121.         return t;
  122.     }
  123.     if (t->L->r <= pos)
  124.         t->L = modify(t->L, pos, val);
  125.     else
  126.         t->R = modify(t->R, pos, val);
  127.     t->x = t->L->x + t->R->x;
  128.     return t;
  129. }
  130.  
  131. Do *modify(Do *t, ll l, ll r, ll val)
  132. {
  133.     t = push(t);
  134.     if (r < t->l || t->r < l)
  135.         return t;
  136.     if (l <= t->l && t->r <= r)
  137.     {
  138.         t->Add += val;
  139.         t = push(t);
  140.         return t;
  141.     }
  142.     t->L = modify(t->L, l, r, val);
  143.     t->R = modify(t->R, l, r, val);
  144.     t->x = t->L->x + t->R->x;
  145.     return t;
  146. }
Add Comment
Please, Sign In to add comment