Submission #197615

# Submission time Handle Problem Language Result Execution time Memory
197615 2020-01-22T02:59:55 Z sinhriv Triple Jump (JOI19_jumps) C++17
100 / 100
1232 ms 91200 KB
/***
Todo:

If we fix A, then B should be the next element to the right which is > A
Same thing happens if we fix B.

So stack to find the closest greater left and right.

After that do something with C?
***/


#include<bits/stdc++.h>
#define left owo
#define right uwu

using namespace std;

const int N = 4002000;
const int MAXN = 500050;

struct SegmentTree{
  int ans[N];
  int best[N];
  int lazy[N];

  #define mid ((l + r) >> 1)

  SegmentTree() {
    for(int i = 0; i < N; ++i) ans[i] = best[i] = (int)-1e9;
  }

  void push(int x) {
    ans[x] = max(ans[x], best[x] + lazy[x]);
    lazy[x << 1] = max(lazy[x << 1], lazy[x]);
    lazy[x << 1 | 1] = max(lazy[x << 1 | 1], lazy[x]);
    lazy[x] = 0;
    return;
  }

  void modify(int x, int l, int r, int pos, int val) {
    push(x);
    if(l == r) {
      best[x] = max(best[x], val);
      return;
    }
    if(pos <= mid) modify(x << 1, l, mid, pos, val);
    else modify(x << 1 | 1, mid + 1, r, pos, val);
    best[x] = max({best[x], best[x << 1], best[x << 1 | 1]});
    ans[x] = max({ans[x], ans[x << 1], ans[x << 1 | 1]});
    return;
  }

  int query(int x, int l, int r, int pos) {
    push(x);
    if(r < pos) return -1;
    if(l >= pos){
      //cout << l << " " << r << " " << ans[x] << endl;
      return ans[x];
    }
    int ret = max(query(x << 1, l, mid, pos), query(x << 1 | 1, mid + 1, r, pos));
    ans[x] = max({ans[x], ans[x << 1], ans[x << 1 | 1]});
    return ret;
  }
  #undef mid
}seg;

int n, q;
int a[MAXN];
int left[MAXN];
int right[MAXN];
int answer[MAXN];
vector<pair<int, int>> queries[MAXN];

int main() {
  ios_base::sync_with_stdio(false); cin.tie(NULL);

  cin >> n;
  for(int i = 0; i < n; ++i) cin >> a[i];

  vector<int> st;
  for(int i = 0; i < n; ++i) {
    while(!st.empty() && a[i] >= a[st.back()]) {
      right[st.back()] = i;
      st.pop_back();
    }
    if(!st.empty()) left[i] = st.back();
    else left[i] = -1;
    st.push_back(i);
  }

  while(!st.empty()) {
    right[st.back()] = -1;
    st.pop_back();
  }

  cin >> q;
  for(int i = 0; i < q; ++i) {
    int l, r;
    cin >> l >> r;
    queries[--r].emplace_back(--l, i);
  }

  set<pair<int, int>> que;
  //b - a <= c - b -> 2b - a <= c
  for(int i = 0; i < n; ++i) {
    if(right[i] != -1) {
      que.emplace(2 * right[i] - i, i);
    }
    if(left[i] != -1) {
      que.emplace(2 * i - left[i], i + MAXN);
    }

    while(!que.empty() && que.begin() -> first <= i) {
      auto p = *que.begin(); que.erase(que.begin());
      int x, y;
      if(p.second >= MAXN) {
        p.second -= MAXN;
        x = left[p.second];
        y = p.second;
      }
      else {
        x = p.second;
        y = right[p.second];
      }
      seg.modify(1, 0, n - 1, x, a[x] + a[y]);
    }

    /// update on IT as C = a[i]
    seg.lazy[1] = max(seg.lazy[1], a[i]);
    /// answer for queries
    for(auto q : queries[i]) {
      answer[q.second] = seg.query(1, 0, n - 1, q.first);
    }
  }

  for(int i = 0; i < q; ++i) {
    cout << answer[i] << "\n";
  }

  // chiu cha biet sai gi


  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 41 ms 43512 KB Output is correct
2 Correct 41 ms 43512 KB Output is correct
3 Correct 48 ms 43444 KB Output is correct
4 Correct 41 ms 43416 KB Output is correct
5 Correct 42 ms 43512 KB Output is correct
6 Correct 41 ms 43384 KB Output is correct
7 Correct 40 ms 43512 KB Output is correct
8 Correct 41 ms 43484 KB Output is correct
9 Correct 41 ms 43512 KB Output is correct
10 Correct 41 ms 43384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 41 ms 43512 KB Output is correct
2 Correct 41 ms 43512 KB Output is correct
3 Correct 48 ms 43444 KB Output is correct
4 Correct 41 ms 43416 KB Output is correct
5 Correct 42 ms 43512 KB Output is correct
6 Correct 41 ms 43384 KB Output is correct
7 Correct 40 ms 43512 KB Output is correct
8 Correct 41 ms 43484 KB Output is correct
9 Correct 41 ms 43512 KB Output is correct
10 Correct 41 ms 43384 KB Output is correct
11 Correct 345 ms 61684 KB Output is correct
12 Correct 348 ms 61688 KB Output is correct
13 Correct 355 ms 61708 KB Output is correct
14 Correct 352 ms 61688 KB Output is correct
15 Correct 352 ms 61848 KB Output is correct
16 Correct 360 ms 61036 KB Output is correct
17 Correct 341 ms 61048 KB Output is correct
18 Correct 355 ms 61132 KB Output is correct
19 Correct 345 ms 61688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 201 ms 51736 KB Output is correct
2 Correct 121 ms 51576 KB Output is correct
3 Correct 124 ms 52628 KB Output is correct
4 Correct 207 ms 51676 KB Output is correct
5 Correct 210 ms 51676 KB Output is correct
6 Correct 204 ms 51068 KB Output is correct
7 Correct 206 ms 50868 KB Output is correct
8 Correct 204 ms 50932 KB Output is correct
9 Correct 214 ms 51320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 41 ms 43512 KB Output is correct
2 Correct 41 ms 43512 KB Output is correct
3 Correct 48 ms 43444 KB Output is correct
4 Correct 41 ms 43416 KB Output is correct
5 Correct 42 ms 43512 KB Output is correct
6 Correct 41 ms 43384 KB Output is correct
7 Correct 40 ms 43512 KB Output is correct
8 Correct 41 ms 43484 KB Output is correct
9 Correct 41 ms 43512 KB Output is correct
10 Correct 41 ms 43384 KB Output is correct
11 Correct 345 ms 61684 KB Output is correct
12 Correct 348 ms 61688 KB Output is correct
13 Correct 355 ms 61708 KB Output is correct
14 Correct 352 ms 61688 KB Output is correct
15 Correct 352 ms 61848 KB Output is correct
16 Correct 360 ms 61036 KB Output is correct
17 Correct 341 ms 61048 KB Output is correct
18 Correct 355 ms 61132 KB Output is correct
19 Correct 345 ms 61688 KB Output is correct
20 Correct 201 ms 51736 KB Output is correct
21 Correct 121 ms 51576 KB Output is correct
22 Correct 124 ms 52628 KB Output is correct
23 Correct 207 ms 51676 KB Output is correct
24 Correct 210 ms 51676 KB Output is correct
25 Correct 204 ms 51068 KB Output is correct
26 Correct 206 ms 50868 KB Output is correct
27 Correct 204 ms 50932 KB Output is correct
28 Correct 214 ms 51320 KB Output is correct
29 Correct 1129 ms 85316 KB Output is correct
30 Correct 904 ms 85316 KB Output is correct
31 Correct 971 ms 87380 KB Output is correct
32 Correct 1119 ms 85584 KB Output is correct
33 Correct 1232 ms 85428 KB Output is correct
34 Correct 1126 ms 83156 KB Output is correct
35 Correct 1157 ms 82788 KB Output is correct
36 Correct 1106 ms 83064 KB Output is correct
37 Correct 1131 ms 84312 KB Output is correct
38 Correct 815 ms 91200 KB Output is correct
39 Correct 816 ms 91084 KB Output is correct
40 Correct 795 ms 87984 KB Output is correct
41 Correct 797 ms 87288 KB Output is correct
42 Correct 794 ms 87288 KB Output is correct
43 Correct 808 ms 89100 KB Output is correct
44 Correct 851 ms 90444 KB Output is correct
45 Correct 862 ms 90532 KB Output is correct
46 Correct 838 ms 87444 KB Output is correct
47 Correct 834 ms 87032 KB Output is correct
48 Correct 842 ms 86940 KB Output is correct
49 Correct 844 ms 89204 KB Output is correct
50 Correct 923 ms 88464 KB Output is correct
51 Correct 921 ms 88592 KB Output is correct
52 Correct 908 ms 86128 KB Output is correct
53 Correct 906 ms 85880 KB Output is correct
54 Correct 915 ms 86032 KB Output is correct
55 Correct 912 ms 87492 KB Output is correct