Advertisement
Guest User

IZhO 2012 F Solution

a guest
Jul 5th, 2015
1,822
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.56 KB | None | 0 0
  1. #include <map>
  2. #include <set>
  3. #include <new>
  4. #include <stack>
  5. #include <deque>
  6. #include <queue>
  7. #include <cmath>
  8. #include <ctime>
  9. #include <locale>
  10. #include <memory>
  11. #include <vector>
  12. #include <cstdio>
  13. #include <limits>
  14. #include <cassert>
  15. #include <ctype.h>
  16. #include <climits>
  17. #include <cstdarg>
  18. #include <utility>
  19. #include <cstring>
  20. #include <iomanip>
  21. #include <numeric>
  22. #include <cassert>
  23. #include <sstream>
  24. #include <fstream>
  25. #include <complex>
  26. #include <iterator>
  27. #include <iostream>
  28. #include <typeinfo>
  29. #include <valarray>
  30. #include <algorithm>
  31. #include <functional>
  32.  
  33. #define name "f"
  34. #define fs first
  35. #define sc second
  36. #define mp make_pair
  37. #define pb push_back
  38. #define sz(s) ((int) s.size ())
  39. #define all(s) s.begin (), s.end ()
  40.  
  41. using namespace std;
  42.  
  43. typedef long long ll;
  44. typedef unsigned long long ull;
  45. typedef pair<int, int> ii;
  46. typedef vector<int> vi;
  47. typedef vector<ii> vii;
  48.  
  49. const int N = 123456;
  50. const double eps = 1e-11;
  51. const int inf = (int) 1e9;
  52. const int mod = (int) 1e9 + 7;
  53.  
  54. struct tree {
  55.     int sum, add, tl, tr, l, r;
  56.     tree () : sum (0), add (0), l (-1), r (-1) {}
  57. };
  58.  
  59. int cnt = 2;
  60. tree t[64 * N];
  61.  
  62. void push (int v) {
  63.     if (t[v].add) {
  64.         t[v].sum = t[v].tr - t[v].tl + 1;
  65.         int tm = (t[v].tl + t[v].tr) >> 1;
  66.         if (t[v].l == -1) {
  67.             t[v].l = cnt++;
  68.             t[t[v].l].tl = t[v].tl;
  69.             t[t[v].l].tr = tm;
  70.         }
  71.         if (t[v].r == -1) {
  72.             t[v].r = cnt++;
  73.             t[t[v].r].tl = tm + 1;
  74.             t[t[v].r].tr = t[v].tr;
  75.         }
  76.         t[t[v].l].add = t[t[v].r].add = 1;
  77.         t[v].add = 0;
  78.     }
  79. }
  80.  
  81. void update (int v, int l, int r) {
  82.     push (v);
  83.     if (l == t[v].tl && r == t[v].tr) {
  84.         t[v].add = 1;
  85.         push (v);
  86.     } else {
  87.         int tm = (t[v].tl + t[v].tr) >> 1;
  88.         if (t[v].l == -1) {
  89.             t[v].l = cnt++;
  90.             t[t[v].l].tl = t[v].tl;
  91.             t[t[v].l].tr = tm;
  92.         }
  93.         if (t[v].r == -1) {
  94.             t[v].r = cnt++;
  95.             t[t[v].r].tl = tm + 1;
  96.             t[t[v].r].tr = t[v].tr;
  97.         }
  98.         if (l > tm) {
  99.             update (t[v].r, l, r);
  100.         } else if (r <= tm) {
  101.             update (t[v].l, l, r);
  102.         } else {
  103.             update (t[v].l, l, tm);
  104.             update (t[v].r, tm + 1, r);
  105.         }
  106.         push (t[v].l);
  107.         push (t[v].r);
  108.         t[v].sum = t[t[v].l].sum + t[t[v].r].sum;
  109.     }
  110. }
  111.  
  112. int get (int v, int l, int r) {
  113.     push (v);
  114.     if (l == t[v].tl && r == t[v].tr) {
  115.         return t[v].sum;
  116.     } else {
  117.         int tm = (t[v].tl + t[v].tr) >> 1;
  118.         if (t[v].l == -1) {
  119.             t[v].l = cnt++;
  120.             t[t[v].l].tl = t[v].tl;
  121.             t[t[v].l].tr = tm;
  122.         }
  123.         if (t[v].r == -1) {
  124.             t[v].r = cnt++;
  125.             t[t[v].r].tl = tm + 1;
  126.             t[t[v].r].tr = t[v].tr;
  127.         }
  128.         if (l > tm) {
  129.             return get (t[v].r, l, r);
  130.         } else if (r <= tm) {
  131.             return get (t[v].l, l, r);
  132.         } else {
  133.             return get (t[v].l, l, tm) + get (t[v].r, tm + 1, r);
  134.         }
  135.     }
  136. }
  137.  
  138. int main()
  139. {
  140.     freopen (name".in", "r", stdin);
  141.     freopen (name".out", "w", stdout);
  142.     int q;
  143.     scanf ("%d", &q);
  144.     t[1].sum = 0; t[1].add = 0;
  145.     t[1].tl = 1; t[1].tr = inf;
  146.     int c = 0;
  147.     while (q--) {
  148.         int d, x, y;
  149.         scanf ("%d %d %d", &d, &x, &y);
  150.         if (d == 1) {
  151.             printf ("%d\n", c = get (1, x + c, y + c));
  152.         } else {
  153.             update (1, x + c, y + c);
  154.         }
  155.     }
  156.     return 0;
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement