Advertisement
Guest User

Untitled

a guest
Dec 10th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.72 KB | None | 0 0
  1. #include <vector>
  2. #include <utility>
  3. #include <algorithm>
  4. #include <set>
  5. #include <iostream>
  6. #include <string>
  7. #include <map>
  8. #include <queue>
  9. #include <unordered_map>
  10. #include <math.h>
  11. #include <stdio.h>
  12.  
  13. using namespace std;
  14.  
  15. typedef long long ll;
  16. typedef vector<int> vi;
  17. typedef vector<ll> vl;
  18. typedef vector<vi> vvi;
  19. typedef pair<int, int> pii;
  20. typedef pair<double, double> pdd;
  21.  
  22. const int inf = (int)1e9;
  23. const int mod = (int)1e9 + 7;
  24.  
  25. #define eps 1e-9
  26. #define mp make_pair
  27. #define PI 3.14159265359
  28. #define all(x) (x).begin(), (x).end()
  29. #define name ""
  30.  
  31. vector<pii> t;
  32. vi a;
  33.  
  34. void build(int v, int tl, int tr)
  35. {
  36.     if (tl == tr) {
  37.         t[v].first = t[v].second = a[tl];
  38.         return;
  39.     }
  40.  
  41.     int tm = (tl + tr) / 2;
  42.     build(2 * v, tl, tm);
  43.     build(2 * v + 1, tm + 1, tr);
  44.  
  45.     t[v].first = max({ t[2 * v + 1].first, t[2 * v].first, t[2 * v + 1].second, t[2 * v].second });
  46.     t[v].second = min({ t[2 * v + 1].first, t[2 * v].first, t[2 * v + 1].second, t[2 * v].second });
  47. }
  48.  
  49. pii query(int v, int tl, int tr, int l, int r)
  50. {
  51.     if (l > r)
  52.         return mp(-inf, inf);
  53.     if (l == tl && r == tr)
  54.         return t[v];
  55.  
  56.     int tm = (tl + tr) / 2;
  57.     pii p1 = query(2 * v, tl, tm, l, min(r, tm));
  58.     pii p2 = query(2 * v + 1, tm + 1, tr, max(l, tm + 1), r);
  59.  
  60.     return mp(max(p1.first, p2.first), min(p1.second, p2.second));
  61. }
  62.  
  63. void upd(int v, int tl, int tr, int i, int x)
  64. {
  65.     if (tl == tr)
  66.         t[v].first = t[v].second = x;
  67.     else {
  68.         int tm = (tl + tr) / 2;
  69.         if (i <= tm)
  70.             upd(2 * v, tl, tm, i, x);
  71.         else
  72.             upd(2 * v + 1, tm + 1, tr, i, x);
  73.  
  74.         t[v].first = max({ t[2 * v + 1].first, t[2 * v].first, t[2 * v + 1].second, t[2 * v].second });
  75.         t[v].second = min({ t[2 * v + 1].first, t[2 * v].first, t[2 * v + 1].second, t[2 * v].second });
  76.     }
  77.  
  78. }
  79.  
  80. int main()
  81. {
  82. #ifdef _DEBUG
  83.     freopen("input.txt", "rt", stdin);
  84.     freopen("output.txt", "wt", stdout);
  85. #else
  86.     freopen(name".in", "rt", stdin);
  87.     freopen(name".out", "wt", stdout);
  88. #endif
  89.  
  90.     a.resize(100000);
  91.     for (ll i = 1; i <= 100000; i++) {
  92.         a[i - 1] = (int)((i * i) % 12345) + (int)((i * i * i) % 23456);
  93.     }
  94.  
  95.     t.assign(a.size() * 4, pii(-inf, inf));
  96.     build(1, 0, a.size() - 1);
  97.  
  98.     int k;
  99.     scanf("%d", &k);
  100.     for (int i = 0; i < k; i++) {
  101.         int x, y;
  102.         scanf("%d %d", &x, &y);
  103.  
  104.         if (x > 0) {
  105.             x--;
  106.             y--;
  107.             pii p = query(1, 0, a.size() - 1, x, y);
  108.  
  109.             printf("%d\n", p.first - p.second);
  110.         }
  111.         else {
  112.             x *= -1;
  113.             x--;
  114.  
  115.             upd(1, 0, a.size() - 1, x, y);
  116.         }
  117.     }
  118.  
  119.     return 0;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement