Advertisement
Mlxa

Интересное дерево отрезков.

Oct 15th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.54 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. #define int ll
  5.  
  6. const ll MAXN = 3E5 + 5;
  7. const ll BAD  = 3E5;
  8. const ll INF  = 1E18 + 18;
  9.  
  10. ll  n, m;
  11. ll  d[MAXN],
  12.     h[MAXN];
  13. ll  sumd[MAXN],
  14.     first[MAXN],
  15.     second[MAXN];
  16.  
  17.  
  18. struct Node
  19. {
  20.     int max_a_ind,
  21.         max_b_ind,
  22.         ans_a_ind,
  23.         ans_b_ind;
  24.     Node():
  25.         max_a_ind(BAD),
  26.         max_b_ind(BAD),
  27.         ans_a_ind(BAD),
  28.         ans_b_ind(BAD)
  29.     {}
  30.     Node(int i):
  31.         max_a_ind(i),
  32.         max_b_ind(i),
  33.         ans_a_ind(BAD),
  34.         ans_b_ind(BAD)
  35.     {}
  36. };
  37.  
  38. ll func(const Node & x)
  39. {
  40.     return first[x.ans_a_ind] + second[x.ans_b_ind];
  41. }
  42.  
  43. Node t[4 * MAXN];
  44.  
  45. Node merge(const Node & l, const Node & r)
  46. {
  47.     Node result;
  48.     result.max_a_ind = first[l.max_a_ind] > first[r.max_a_ind] ? l.max_a_ind : r.max_a_ind;
  49.     result.max_b_ind = second[l.max_b_ind] > second[r.max_b_ind] ? l.max_b_ind : r.max_b_ind;
  50.     ll l_res = func(l);
  51.     ll m_res = first[l.max_a_ind] + second[r.max_b_ind];
  52.     ll r_res = func(r);
  53.     ll mx = max(l_res, max(m_res, r_res));
  54.  
  55.     if (l_res == mx)
  56.     {
  57.         result.ans_a_ind = l.ans_a_ind;
  58.         result.ans_b_ind = l.ans_b_ind;
  59.     }
  60.     else if (m_res == mx)
  61.     {
  62.         result.ans_a_ind = l.max_a_ind;
  63.         result.ans_b_ind = r.max_b_ind;
  64.     }
  65.     else
  66.     {
  67.         result.ans_a_ind = r.ans_a_ind;
  68.         result.ans_b_ind = r.ans_b_ind;
  69.     }
  70.     return result;
  71. }
  72.  
  73. #define ls (2 * v + 1)
  74. #define rs (2 * v + 2)
  75.  
  76. void build(int v, int vl, int vr)
  77. {
  78.     if (vr - vl < 1)
  79.     {
  80.         return;
  81.     }
  82.     if (vr - vl == 1)
  83.     {
  84.         t[v] = Node(vl);
  85.     }
  86.     else
  87.     {
  88.         int vm = (vl + vr) / 2;
  89.         build(ls, vl, vm);
  90.         build(rs, vm, vr);
  91.         t[v] = merge(t[ls], t[rs]);
  92.     }
  93. }
  94.  
  95. Node q(int v, int vl, int vr, int ql, int qr) // [l, r)
  96. {
  97.     if (qr <= vl || vr <= ql)
  98.     {
  99.         return Node();
  100.     }
  101.     if (ql <= vl && vr <= qr)
  102.     {
  103.         return t[v];
  104.     }
  105.     int vm = (vl + vr) / 2;
  106.     return merge(q(ls, vl, vm, ql, qr), q(rs, vm, vr, ql, qr));
  107. }
  108.  
  109. Node query(int l, int r) // [l, r]
  110. {
  111.     return q(0, 0, MAXN, l, r + 1);
  112. }
  113.  
  114. void solve(int a, int b)
  115. {
  116.     if (a > b)
  117.     {
  118.         b += n;
  119.     }
  120.  
  121.     auto xx = query(b + 1, a + n - 1);
  122.     cout << func(xx) << '\n';
  123.  
  124. }
  125.  
  126. int32_t main()
  127. {
  128.     ios::sync_with_stdio(0);
  129.     cin.tie(0);
  130.     cout.tie(0);
  131.  
  132.     first[BAD] = -INF;
  133.     second[BAD] = -INF;
  134.  
  135.     cin >> n >> m;
  136.     for (int i = 0; i < n; ++i)
  137.     {
  138.         cin >> d[i];
  139.     }
  140.     for (int i = 0; i < n; ++i)
  141.     {
  142.         cin >> h[i];
  143.     }
  144.  
  145.     sumd[0] = 0;
  146.     for (int i = 0; i < n; ++i)
  147.     {
  148.         d[n + i] = d[i];
  149.         h[i + n] = h[i];
  150.     }
  151.     for (int i = 0; i < 2 * n; ++i)
  152.     {
  153.         sumd[i + 1] = sumd[i] + d[i];
  154.     }
  155.  
  156.     for (int i = 0; i < 2 * n; ++i)
  157.     {
  158.         first[i] = 2 * h[i] - sumd[i];
  159.         second[i] = 2 * h[i] + sumd[i];
  160.     }
  161.  
  162.     build(0, 0, MAXN);
  163.  
  164.     while (m--)
  165.     {
  166.         int a, b;
  167.         cin >> a >> b;
  168.         solve(a - 1, b - 1);
  169.     }
  170.  
  171.     /*while (m--)
  172.     {
  173.         int l, r;
  174.         cin >> l >> r;
  175.         --l; --r;
  176.         Node ans = query(l, r);
  177.         cout << "Max first on [l; r]:  " << ans.max_a_ind + 1 << ' ' << first[ans.max_a_ind] << endl;
  178.         cout << "Max second on [l; r]: " << ans.max_b_ind + 1 << ' ' << second[ans.max_b_ind] << endl;
  179.         cout << "Max answer: " << ans.ans_a_ind + 1 << ' ' << ans.ans_b_ind + 1 << ' ' << func(ans) << endl;
  180.     }*/
  181.  
  182.     return 0;
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement