QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#376191#7417. Honorable MentionXiaohubaWA 1608ms34356kbC++236.2kb2024-04-03 23:32:272024-04-03 23:32:28

Judging History

你现在查看的是最新测评结果

  • [2024-04-03 23:32:28]
  • 评测
  • 测评结果:WA
  • 用时:1608ms
  • 内存:34356kb
  • [2024-04-03 23:32:27]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

// #define LOCK_GETCHAR
// #define USE_INT_128

#if __cplusplus < 201400
#warning "Please use c++14 or higher."
#define CONSTEXPR_FUNC
#define ENABLE_IF_INT
#else
#define CONSTEXPR_FUNC constexpr
#define ENABLE_IF_INT , enable_if_t<_is_integer<T>, int> = 0
template <class T> constexpr bool _is_integer = numeric_limits<T>::is_integer;
template <> constexpr bool _is_integer<bool> = false;
template <> constexpr bool _is_integer<char> = false;
#ifdef USE_INT_128
template <> constexpr bool _is_integer<__int128> = true;
template <> constexpr bool _is_integer<__uint128_t> = true;
#endif
template <class T ENABLE_IF_INT>
constexpr T INF = numeric_limits<T>::max() >> 1;
#endif

#if !defined(_WIN32) && !defined(LOCK_GETCHAR)
#define getchar getchar_unlocked
#endif

#define il inline
#define mkp make_pair
#define fi first
#define se second
#define For(i, j, k) for (decltype(j - k) i = (j); i <= (k); ++i)     // NOLINT
#define ForDown(i, j, k) for (decltype(j - k) i = (j); i >= (k); --i) // NOLINT
#define pb push_back
#define eb emplace_back
#ifndef ONLINE_JUDGE
#define FileIO(filename)                                                       \
  freopen(filename ".in", "r", stdin);                                         \
  freopen(filename ".out", "w", stdout)
#else
#define FileIO(filename) void(0)
#endif

using ll = long long;
using uint = unsigned int;
using ull = unsigned long long;
using db = double;
using ldb = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#ifdef USE_INT_128
using lll = __int128_t;
using ulll = __uint128_t;
#endif

// clang-format off
template<typename T> constexpr il T sq(const T & x){ return x * x; }
template<typename T> CONSTEXPR_FUNC il void cmin(T & x, const T &y){ x = min(x, y); }
template<typename T> CONSTEXPR_FUNC il void cmax(T & x, const T &y){ x = max(x, y);}
template<typename T> CONSTEXPR_FUNC il T qpow(T x, ull y, T mod){T ans = 1; x %= mod; while (y) { if(y & 1)(ans *= x) %= mod;(x *= x) %= mod; y >>= 1;} return ans;}
template<typename T> CONSTEXPR_FUNC il T qpow(T x, ull y){T ans = 1; while (y) {if(y & 1) ans *= x;x *= x;y >>= 1;} return ans;}
template<typename T ENABLE_IF_INT> il void read(T &x){ x = 0; int f = 1; int c = getchar(); while(!isdigit(c)) {if (c == '-') f = -1;c = getchar();} while(isdigit(c)) {x = x * 10 + c - '0';c = getchar();} x *= f;}
template<typename T, typename ... Args> il void read(T &x, Args &... y){ read(x); read(y...); }
// clang-format on

// File head end

namespace {
constexpr ll MAXN = 35005, INF = 1e14;
int n, q, a[MAXN];
struct Node {
  int l, r;
  array<vector<ll>, 4> val;
} T[MAXN << 2];
#define mid(p) ((T[p].l + T[p].r) >> 1)
#define sz(p) (T[p].r - T[p].l + 1)
#define lc(p) (mid(p) << 1)
#define rc(p) (mid(p) << 1 | 1)
il vector<ll> merge(const vector<ll> &x, const vector<ll> &y) {
  assert(!x.empty() && !y.empty());
  vector<ll> res = {x[0] + y[0]};
  for (ll p = 1, q = 1, cur = res[0]; p < x.size() || q < y.size();) {
    if (q == y.size() || (p < x.size() && x[p] - x[p - 1] >= y[q] - y[q - 1]))
      res.eb(cur += x[p] - x[p - 1]), p++;
    else
      res.eb(cur += y[q] - y[q - 1]), q++;
  }

  // cerr << "===>\n";
  // for (ll i : x)
  //   cerr << i << ' ';
  // cerr << '\n';
  // for (ll i : y)
  //   cerr << i << ' ';
  // cerr << '\n';
  // cerr << "<===\n";
  // for (ll x : res)
  //   cerr << x << ' ';
  // cerr << "\n\n";

  // ll prev = res[1] - res[0];
  // For(i, 2, signed(res.size()) - 1) assert(res[i] - res[i - 1] <= prev),
  //     prev = res[i] - res[i - 1];

  return res;
}
il void pu(int p) {
  T[p].val[0] = T[p].val[1] = T[p].val[2] = T[p].val[3] =
      vector<ll>(sz(p) + 1, -INF);
  // cerr << "pu " << T[p].l << ' ' << T[p].r << '\n';
  For(i, 0, 3) For(j, 0, 3) {
    int k = (i >> 1) << 1 | (j & 1);
    auto vec = merge(T[lc(p)].val[i], T[rc(p)].val[j]);
    assert(vec.size() == sz(p) + 1);
    For(o, 0, sz(p)) cmax(T[p].val[k][o], vec[o]);
    if (i & (j >> 1) & 1)
      For(o, 1, sz(p)) cmax(T[p].val[k][o - 1], vec[o]);
  }
  // For(i, 0, 3) {
  //   for (ll x : T[p].val[i])
  //     cerr << x << ' ';
  //   cerr << '\n';
  // }
}
void build(int p, int l, int r) {
  T[p].l = l, T[p].r = r;
  if (l == r) {
    T[p].val[3] = {-INF, a[l]};
    T[p].val[0] = {0, -INF};
    T[p].val[1] = T[p].val[2] = {-INF, -INF};
    return;
  }
  int mid = (l + r) >> 1;
  build(lc(p), l, mid), build(rc(p), mid + 1, r);
  pu(p);
}
array<ll, 4> qry(int p, int ql, int qr, ll V) {
  if (ql <= T[p].l && qr >= T[p].r) {
    auto search = [&](int id) {
      int l = 0, r = sz(p);
      ll ans = -INF;
      while (l <= r) {
        int mid = (l + r) >> 1;
        if (mid < sz(p) &&
            T[p].val[id][mid + 1] - V * (mid + 1) > T[p].val[id][mid] - V * mid)
          l = mid + 1;
        else
          ans = T[p].val[id][mid] - V * mid, r = mid - 1;
      }
      return ans;
    };
    return {search(0), search(1), search(2), search(3)};
  }
  int mid = mid(p);
  if (qr <= mid)
    return qry(lc(p), ql, qr, V);
  else if (ql > mid)
    return qry(rc(p), ql, qr, V);
  else {
    array<ll, 4> ans = {-INF, -INF, -INF, -INF}, L = qry(lc(p), ql, qr, V),
                 R = qry(rc(p), ql, qr, V);
    For(i, 0, 3) For(j, 0, 3) {
      int k = (i >> 1) << 1 | (j & 1);
      cmax(ans[k], L[i] + R[j]);
      if (i & (j >> 1) & 1)
        cmax(ans[k], L[i] + R[j] + V);
    }
    return ans;
  }
}
il ll solve(int L, int R, int K) {
  ll l = -3e9, r = 3e9;
  auto q2 = qry(1, L, R, R + 1);
  ll ans = *max_element(q2.begin(), q2.end()) + K * (R + 1);
  while (l <= r) {
    ll mid = (l + r) / 2;
    auto q1 = qry(1, L, R, mid), q2 = qry(1, L, R, mid + 1);
    ll v1 = *max_element(q1.begin(), q1.end()) + 1ll * K * mid,
       v2 = *max_element(q2.begin(), q2.end()) + 1ll * K * (mid + 1);
    if (v1 > v2)
      l = mid + 1;
    else
      ans = v1, r = mid - 1;
  }
  return ans;
}
il void Main() {
  read(n, q);
  For(i, 1, n) read(a[i]);
  build(1, 1, n);
  // cerr << "> " << T[1].val[0][1] << ' ' << T[1].val[3][1] << '\n';
  while (q--) {
    int l, r, k;
    read(l, r, k);
    cout << solve(l, r, k) << '\n';
  }
}
} // namespace

signed main() { return Main(), 0; }

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 17760kb

input:

5 5
-1 2 -3 4 -5
1 5 1
1 5 2
1 5 3
1 5 4
1 5 5

output:

4
6
5
2
-3

result:

ok 5 number(s): "4 6 5 2 -3"

Test #2:

score: 0
Accepted
time: 2ms
memory: 17836kb

input:

5 1
7 7 7 7 7
1 5 1

output:

35

result:

ok 1 number(s): "35"

Test #3:

score: -100
Wrong Answer
time: 1608ms
memory: 34356kb

input:

25000 25000
-11889 -16468 18402 -13238 14125 12120 -3295 -12106 20152 7478 8575 9968 -13946 -13408 23715 -14670 -21837 12589 7029 20609 2868 12728 -6942 982 5884 -7561 -2058 -7892 7806 16344 14645 13904 7355 9860 15446 -539 24072 21969 19028 -24939 -21911 -11836 21045 -4727 -3882 2138 18513 -4796 -1...

output:

10341941
44514177
6687268
77971944
99605102
107722534
15473041
17383093
62015893
10703102
41214044
22949449
9407209
9022260
39351814
72311292
5178065
42027491
52700848
38135774
37045964
4761513
16326256
16812496
107985169
28306484
46710957
864270
102812861
27960495
50366764
16379719
2791377
21112728...

result:

wrong answer 44th numbers differ - expected: '121436115', found: '121434418'