Advertisement
Guest User

Untitled

a guest
Feb 18th, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.94 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6. using ll = long long;
  7.  
  8. vector<ll> a, t;
  9. int n, q;
  10.  
  11. int mt(int i, int j)
  12. {
  13.     if (a[i] > a[j])
  14.         return i;
  15.     return j;
  16. }
  17.  
  18. void build(int v = 1, int tl = 0, int tr = n - 1)
  19. {
  20.     if (tl == tr)
  21.         t[v] = tl;
  22.     else
  23.     {
  24.         int tm = tl + tr >> 1;
  25.         build(v*2, tl, tm);
  26.         build(v*2 + 1, tm + 1, tr);
  27.         t[v] = max(a[t[v*2]], a[t[v*2+1]]);
  28.     }
  29. }
  30.  
  31. ll mc(int l, int r, int v = 1, int tl = 0, int tr = n - 1)
  32. {
  33.     if (l > r)
  34.         return a.size() - 1;
  35.     if (tl == l && tr == r)
  36.         return t[v];
  37.     int tm = tl + tr >> 1;
  38.     return mt(mc(l, min(tm, r), v*2, tl, tm), mc(max(tm + 1, l), r, v*2 + 1, tm + 1, tr));
  39. }
  40.  
  41. int main()
  42. {
  43.     cin >> n >> q;
  44.     a.resize(n);
  45.     t.resize(4*n);
  46.     for (int i = 0; i < n; ++i)
  47.         cin >> a[i];
  48.     a.push_back((ll)-1);
  49.     build();
  50.     while (q--)
  51.     {
  52.         int l, r;
  53.         cin >> l >> r;
  54.         bool ws = 0;
  55.         for (int i = l - 1; i < r; ++i)
  56.         {
  57.             ll x, y;
  58.             x = mc(i + 1, r-1);
  59.             y = mt(mc(i + 1, x - 1), mc(x + 1, r-1));
  60.             if (a[x] >= a[y] && a[x] >= a[i])
  61.                 if (a[x] < a[y] + a[i])
  62.                     ws = 1;
  63.             else
  64.                 if (a[y] >= a[i] && a[y] >= a[x])
  65.                     if (a[y] < a[x] + a[i])
  66.                         ws = 1;
  67.             else
  68.                         if (a[i] < a[x] + a[y])
  69.                             ws = 1;
  70.             if (ws)
  71.             {
  72.                 vector<int> trr(3);
  73.                 trr[0] = i;
  74.                 trr[1] = x;
  75.                 trr[2] = y;
  76.                 sort(trr.begin(), trr.end());
  77.                 cout << trr[0] + 1 << ' ' << trr[1] + 1 << ' ' << trr[2] + 1 << endl;
  78.                 break;
  79.             }
  80.         }
  81.         if (!ws)
  82.             cout << -1 << endl;
  83.     }
  84.  
  85.     return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement