Submission #1057357

# Submission time Handle Problem Language Result Execution time Memory
1057357 2024-08-13T18:02:14 Z MilosMilutinovic Dungeon 3 (JOI21_ho_t5) C++14
31 / 100
591 ms 265380 KB
#include <bits/stdc++.h>

using namespace std;

const long long inf = (long long) (1e16);

const int N = (int) (4e7);

int root, tsz, ch[N][2];
pair<long long, long long> st[N];

void Modify(int& x, long long l, long long r, long long ll, long long rr, int a, long long b) {
  if (!x) {
    x = ++tsz;
  }
  if (ll <= l && r <= rr) {
    st[x].first += a;
    st[x].second += b;
    return;
  }
  long long mid = (l + r) >> 1;
  if (rr <= mid) {
    Modify(ch[x][0], l, mid, ll, rr, a, b);
  } else if (ll > mid) {
    Modify(ch[x][1], mid + 1, r, ll, rr, a, b);
  } else {
    Modify(ch[x][0], l, mid, ll, rr, a, b);
    Modify(ch[x][1], mid + 1, r, ll, rr, a, b);
  }
}

long long Query(int x, long long l, long long r, int i) {
  if (l == r) {
    return st[x].first * 1LL * i + st[x].second;
  }
  long long mid = (l + r) >> 1;
  if (i <= mid) {
    return st[x].first * 1LL * i + st[x].second + Query(ch[x][0], l, mid, i);
  } else {
    return st[x].first * 1LL * i + st[x].second + Query(ch[x][1], mid + 1, r, i);
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  vector<int> b(n);
  for (int i = 0; i < n; i++) {
    cin >> b[i];
  }
  vector<int> L(n), R(n);
  {
    vector<int> stk;
    for (int i = 0; i < n; i++) {
      while (!stk.empty() && b[i] < b[stk.back()]) {
        stk.pop_back();
      }
      L[i] = stk.empty() ? -1 : stk.back();
      stk.push_back(i);
    }
  }
  {
    vector<int> stk;
    for (int i = n - 1; i >= 0; i--) {
      while (!stk.empty() && b[i] <= b[stk.back()]) {
        stk.pop_back();
      }
      R[i] = stk.empty() ? n : stk.back();
      stk.push_back(i);
    }
  }
  vector<long long> pref(n + 1);
  for (int i = 0; i < n; i++) {
    pref[i + 1] = pref[i] + a[i];
  }
  vector<int> s(m), t(m), u(m);
  for (int i = 0; i < m; i++) {
    cin >> s[i] >> t[i] >> u[i];
    --s[i]; --t[i]; --t[i];
  }
  const int LOG = 20;
  vector<vector<int>> mx(n, vector<int>(LOG));
  for (int i = 0; i < n; i++) {
    mx[i][0] = a[i];
  }
  for (int j = 1; j < LOG; j++) {
    for (int i = 0; i + (1 << j) <= n; i++) {
      mx[i][j] = max(mx[i][j - 1], mx[i + (1 << (j - 1))][j - 1]);
    }
  }
  vector<int> logs(n + 1);
  for (int i = 2; i <= n; i++) {
    logs[i] = logs[i >> 1] + 1;
  }
  auto GetMax = [&](int l, int r) {
    int k = logs[r - l + 1];
    return max(mx[l][k], mx[r - (1 << k) + 1][k]);
  };
  vector<vector<array<long long, 4>>> qc(n);
  for (int i = 0; i < n; i++) {
    {
      // after L_i
      long long d = pref[R[i]] - pref[i];
      qc[i].push_back({0, d, b[i], 0});
      qc[i].push_back({d + 1, inf, 0, d * b[i]});
      if (L[i] != -1) {
        qc[L[i]].push_back({0, d, -b[i], 0});
        qc[L[i]].push_back({d + 1, inf, 0, -d * b[i]});
      }
    }
    if (L[i] != -1) {
      // before L_i
      vector<long long> v;
      v.push_back(0);
      v.push_back(inf);
      v.push_back(pref[R[i]] - pref[i]);
      v.push_back(pref[i] - pref[L[i]]);
      v.push_back(pref[R[i]] - pref[L[i]]);
      sort(v.begin(), v.end());
      v.erase(unique(v.begin(), v.end()), v.end());
      for (int j = 0; j + 1 < (int) v.size(); j++) {
        if (v[j] >= pref[R[i]] - pref[L[i]]) {
          break;
        }
        pair<long long, long long> p, q;
        if (pref[i] + v[j] < pref[R[i]]) {
          p = {1, pref[i]};
        } else {
          p = {0, pref[R[i]]};
        }
        if (pref[i] > pref[L[i]] + v[j]) {
          q = {0, pref[i]};
        } else {
          q = {1, pref[L[i]]};
        }
        pair<long long, long long> r;
        r.first = p.first - q.first;
        r.second = p.second - q.second;
        qc[L[i]].push_back({v[j], v[j + 1] - 1, r.first * b[i], r.second * b[i]});
      }
    }
  }
  vector<vector<int>> qs(n);
  for (int i = 0; i < m; i++) {
    qs[s[i]].push_back(i);
  }
  vector<long long> res(m);
  for (int i = n - 1; i >= 0; i--) {
    for (auto& p : qc[i]) {
      Modify(root, 0, inf, p[0], p[1], p[2], p[3]);
    }
    for (int id : qs[i]) {
      if (GetMax(s[id], t[id]) > u[id]) {
        res[id] = -1;
      } else {
        res[id] = Query(root, 0, inf, u[id]);
      }
    }
  }
  for (int i = 0; i < m; i++) {
    cout << res[i] << '\n';
  }
  return 0;
}
# Verdict Execution time Memory Grader output
1 Incorrect 8 ms 4956 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 122 ms 68800 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 531 ms 135388 KB Output is correct
2 Correct 581 ms 252864 KB Output is correct
3 Correct 249 ms 80580 KB Output is correct
4 Correct 489 ms 145336 KB Output is correct
5 Correct 533 ms 257724 KB Output is correct
6 Correct 591 ms 242912 KB Output is correct
7 Correct 465 ms 120148 KB Output is correct
8 Correct 522 ms 217476 KB Output is correct
9 Correct 213 ms 69584 KB Output is correct
10 Correct 463 ms 200380 KB Output is correct
11 Correct 554 ms 265380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 8 ms 4956 KB Output isn't correct
2 Halted 0 ms 0 KB -