QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#309679#6564. Frequent FlierMisukiAC ✓52ms19572kbC++2010.0kb2024-01-20 19:51:392024-01-20 19:51:39

Judging History

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

  • [2024-01-20 19:51:39]
  • 评测
  • 测评结果:AC
  • 用时:52ms
  • 内存:19572kb
  • [2024-01-20 19:51:39]
  • 提交

answer

#pragma GCC optimize("O2")
#include <algorithm>
#include <array>
#include <bit>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfenv>
#include <cfloat>
#include <chrono>
#include <cinttypes>
#include <climits>
#include <cmath>
#include <compare>
#include <complex>
#include <concepts>
#include <cstdarg>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <initializer_list>
#include <iomanip>
#include <ios>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <new>
#include <numbers>
#include <numeric>
#include <ostream>
#include <queue>
#include <random>
#include <ranges>
#include <set>
#include <span>
#include <sstream>
#include <stack>
#include <streambuf>
#include <string>
#include <tuple>
#include <type_traits>
#include <variant>

#define int ll
#define INT128_MAX (__int128)(((unsigned __int128) 1 << ((sizeof(__int128) * __CHAR_BIT__) - 1)) - 1)
#define INT128_MIN (-INT128_MAX - 1)

#define clock chrono::steady_clock::now().time_since_epoch().count()

#ifdef DEBUG
#define dbg(x) cout << (#x) << " = " << x << '\n'
#else
#define dbg(x)
#endif

namespace R = std::ranges;
namespace V = std::views;

using namespace std;

using ll = long long;
using ull = unsigned long long;
using ldb = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
//#define double ldb

template<class T>
ostream& operator<<(ostream& os, const pair<T, T> pr) {
  return os << pr.first << ' ' << pr.second;
}
template<class T, size_t N>
ostream& operator<<(ostream& os, const array<T, N> &arr) {
  for(const T &X : arr)
    os << X << ' ';
  return os;
}
template<class T>
ostream& operator<<(ostream& os, const vector<T> &vec) {
  for(const T &X : vec)
    os << X << ' ';
  return os;
}
template<class T>
ostream& operator<<(ostream& os, const set<T> &s) {
  for(const T &x : s)
    os << x << ' ';
  return os;
}

/**
 * template name: lazySegmentTree
 * author: Misuki
 * last update: 2024/01/01
 * verify: Library Checker - Range Affine Point Get
 *         Library Checker - Range Affine Range Sum
 *         Codeforces Edu Segment Tree part 2 step 3 pC - Addition and First element at least X
 */

template<class M, class T, M(*Munit)(), T(*Tunit)(), M(*Mope)(const M&, const M&), T(*Tope)(const T&, const T&), M(*comp)(const M&, const T&)>
struct lazySegmentTree {
  vector<M> data;
  vector<T> tag;
  int size, h;

  lazySegmentTree(int _size) : data(2 * _size, Munit()), tag(_size, Tunit()), size(_size), h(bit_width((unsigned)_size)) {}

  lazySegmentTree(vector<M> init) : data(2 * ssize(init), Munit()), tag(ssize(init), Tunit()), size(ssize(init)), h(bit_width(init.size())) {
    copy(init.begin(), init.end(), data.begin() + size);
    for(int i = size - 1; i > 0; i--)
      data[i] = Mope(data[i << 1], data[i << 1 | 1]);
  }

  void apply(int i, T x) {
    data[i] = comp(data[i], x);
    if (i < size)
      tag[i] = Tope(tag[i], x);
  }

  void push(int i) {
    if (i < size or i >= 2 * size) return;
    for(int s = h - 1; s > 0; s--) {
      if (tag[i >> s] != Tunit()) {
        apply(i >> (s - 1), tag[i >> s]);
        apply(i >> (s - 1) ^ 1, tag[i >> s]);
        tag[i >> s] = Tunit();
      }
    }
  }

  void pull(int i) {
    if (i < size or i >= 2 * size) return;
    i >>= 1;
    while(i) {
      data[i] = Mope(data[i << 1], data[i << 1 | 1]);
      if (tag[i] != Tunit())
        data[i] = comp(data[i], tag[i]);
      i >>= 1;
    }
  }

  void set(int i, M x) {
    push(i + size);
    data[i + size] = x;
    pull(i + size);
  }

  M get(int i) {
    push(i + size);
    return data[i + size];
  }

  void modify(int l, int r, T x) {
    if (x == Tunit()) return;
    int l0 = l + size, r0 = r + size - 1;
    push(l0), push(r0);
    for(l += size, r += size; l < r; l >>= 1, r >>= 1) {
      if (l & 1) apply(l++, x);
      if (r & 1) apply(--r, x);
    }
    pull(l0), pull(r0);
  }

  M query(int l, int r) {
    M L = Munit(), R = Munit();
    push(l + size), push(r + size - 1);
    for(l += size, r += size; l < r; l >>= 1, r >>= 1) {
      if (l & 1) L = Mope(L, data[l++]);
      if (r & 1) R = Mope(data[--r], R);
    }
    return Mope(L, R);
  }

  int firstTrue(int l, int r, function<bool(const M&)> f) {
    vector<int> idL, idR;
    int r0 = r;
    push(l + size), push(r + size - 1);
    for(l += size, r += size; l < r; l >>= 1, r >>= 1) {
      if (l & 1) idL.emplace_back(l++);
      if (r & 1) idR.emplace_back(--r);
    }
    while(!idR.empty()) {
      idL.emplace_back(idR.back());
      idR.pop_back();
    }
    M pre = Munit();
    int v = -1;
    for(int i : idL) {
      if (f(Mope(pre, data[i]))) {
        v = i;
        break;
      } else {
        pre = Mope(pre, data[i]);
      }
    }
    if (v == -1)
      return r0;
    while(v < size) {
      if (tag[v] != Tunit()) {
        apply(v << 1, tag[v]);
        apply(v << 1 | 1, tag[v]);
        tag[v] = Tunit();
      }
      if (f(Mope(pre, data[v << 1])))
        v = v << 1;
      else
        pre = Mope(pre, data[v << 1]), v = v << 1 | 1;
    }
    return v - size;
  }
};

/**
 * template name: ultraLazySegmentTree
 * author: Misuki
 * last update: 2024/01/06
 * include: lazySegmentTree
 * verify: Codeforces Edu Segment Tree part 2 step 2 pA - Addition and Minimum
 *         Codeforces Edu Segment Tree part 2 step 2 pD - Addition and Sum 
 *         Codeforces Edu Segment Tree part 2 step 2 pE - Assignment and Minimum 
 *         Codeforces Edu Segment Tree part 2 step 2 pF - Assignment and Sum 
 *         Library Checker - Area of Union of Rectangles
 */

namespace ultraLazySegmentTree {
  template<class T> T mn(const T &a, const T &b) { return min(a, b); }
  template<class T> T mx(const T &a, const T &b) { return max(a, b); }
  template<class T> T add(const T &a, const T &b) { return a + b; }
  template<class T> T upd(const T&, const T &b) { return b; }
  template<class T> T zero() { return T(0); }
  template<class T, T x> T val() { return x; }

  template<class M, M inf>
  struct addMinLazySegmentTree : lazySegmentTree<M, M, val<M, inf>, zero, mn<M>, add<M>, add<M>> {
    addMinLazySegmentTree(vector<M> init) : lazySegmentTree<M, M, val<M, inf>, zero, mn<M>, add<M>, add<M>>(init) {}
  };
  template<class M, M ninf>
  struct addMaxLazySegmentTree : lazySegmentTree<M, M, val<M, ninf>, zero, mx<M>, add<M>, add<M>> {
    addMaxLazySegmentTree(vector<M> init) : lazySegmentTree<M, M, val<M, ninf>, zero, mx<M>, add<M>, add<M>>(init) {}
  };
  template<class M, M inf>
  struct setMinLazySegmentTree : lazySegmentTree<M, M, val<M, inf>, val<M, inf>, mn<M>, upd<M>, upd<M>> {
    setMinLazySegmentTree(vector<M> init) : lazySegmentTree<M, M, val<M, inf>, val<M, inf>, mn<M>, upd<M>, upd<M>>(init) {}
  };
  template<class M, M ninf>
  struct setMaxLazySegmentTree : lazySegmentTree<M, M, val<M, ninf>, val<M, ninf>, mx<M>, upd<M>, upd<M>> {
    setMaxLazySegmentTree(vector<M> init) : lazySegmentTree<M, M, val<M, ninf>, val<M, ninf>, mx<M>, upd<M>, upd<M>>(init) {}
  };

  template<class T> pair<T, T> prAdd(const pair<T, T> &a, const pair<T, T> &b) { return make_pair(a.first + b.first, a.second + b.second); }
  template<class T> pair<T, T> prAddFirst(const pair<T, T> &a, const T &b) { return make_pair(a.first + b, a.second); }
  template<class T> pair<T, T> prMinSum(const pair<T, T> &a, const pair<T, T> &b) { return a.first != b.first ? mn(a, b) : make_pair(a.first, a.second + b.second); }
  template<class T> pair<T, T> addSumComp(const pair<T, T> &a, const T &b) { return make_pair(a.first + a.second * b, a.second); }
  template<class T> pair<T, T> setSumComp(const pair<T, T> &a, const T &b) { return make_pair(a.second * b, a.second); }
  template<class T> pair<T, T> prZero() { return make_pair(T(0), T(0)); }
  template<class T, T x, T y> pair<T, T> prVal() { return make_pair(x, y); }
  template<class T> vector<pair<T, T>> extend(vector<T> a) {
    vector<pair<T, T>> b(ssize(a));
    for(int i = 0; i < ssize(a); i++)
      b[i] = make_pair(a[i], T(1));
    return b;
  }

  template<class M>
  struct addSumLazySegmentTree : lazySegmentTree<pair<M, M>, M, prZero<M>, zero<M>, prAdd<M>, add<M>, addSumComp<M>> {
    addSumLazySegmentTree(vector<M> init) : lazySegmentTree<pair<M, M>, M, prZero<M>, zero<M>, prAdd<M>, add<M>, addSumComp<M>>(extend<M>(init)) {}
  };
  template<class M, M unuse>
  struct setSumLazySegmentTree : lazySegmentTree<pair<M, M>, M, prZero<M>, val<M, unuse>, prAdd<M>, upd<M>, setSumComp<M>> {
    setSumLazySegmentTree(vector<M> init) : lazySegmentTree<pair<M, M>, M, prZero<M>, val<M, unuse>, prAdd<M>, upd<M>, setSumComp<M>>(extend<M>(init)) {}
  };
  template<class M, M inf>
  struct addMinCntLazySegmentTree : lazySegmentTree<pair<M, M>, M, prVal<M, inf, M(0)>, zero<M>, prMinSum<M>, add<M>, prAddFirst<M>> {
    using base = lazySegmentTree<pair<M, M>, M, prVal<M, inf, M(0)>, zero<M>, prMinSum<M>, add<M>, prAddFirst<M>>;
    addMinCntLazySegmentTree(vector<M> init) : base(extend<M>(init)) {}
    addMinCntLazySegmentTree(vector<pair<M, M>> init) : base(init) {}
  };
}

using namespace ultraLazySegmentTree;

signed main() {
  ios::sync_with_stdio(false), cin.tie(NULL);

  int n, m, k; cin >> n >> m >> k;
  vector<int> f(n + 1);
  for(int &x : f | V::drop(1))
    cin >> x;

  partial_sum(f.begin(), f.end(), f.begin());
  auto sum = [&](int l, int r) -> int { return f[min(r, n)] - f[max(l, 1ll) - 1]; };

  vector<int> init(n + m);
  for(int i = 1; i < n + m; i++)
    init[i] = sum(i - m + 1, i);

  dbg(init);

  addMinLazySegmentTree<int, LLONG_MAX> st(init);
  int ans = 0;
  for(int i = 1; i <= n; i++) {
    if (int x = st.query(i, i + m); x > k) {
      dbg(i);
      dbg(x - k);
      ans += x - k;
      st.modify(i, i + m, k - x);
    }
  }

  cout << f.back() - ans << '\n';

  return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 3528kb

input:

8 3 2
3
1
4
1
5
9
2
6

output:

8

result:

ok single line: '8'

Test #2:

score: 0
Accepted
time: 49ms
memory: 14048kb

input:

200000 2467 999931035
182548858
69876218
33328350
919486767
739850600
948199964
392292320
39077742
366752074
917496841
246661698
37219034
56677740
188278971
965701828
28059790
13200243
825730775
542597589
320715170
939054378
470563899
914247467
990100491
290827128
903662394
611104879
18631185
412134...

output:

82994275905

result:

ok single line: '82994275905'

Test #3:

score: 0
Accepted
time: 3ms
memory: 12456kb

input:

1 200000 999959273
1255319

output:

1255319

result:

ok single line: '1255319'

Test #4:

score: 0
Accepted
time: 52ms
memory: 19572kb

input:

200000 118880 996878327
993340390
999483057
808153702
337349872
863222189
7495963
995920883
12950768
958082368
993215196
967152406
1388062
949959944
836952150
964071667
5291
139115263
958470154
51295691
175385
925242139
995685554
10895812
12563
55482
479983443
42805
996241239
6228013
302633329
10331...

output:

2990634981

result:

ok single line: '2990634981'

Test #5:

score: 0
Accepted
time: 33ms
memory: 14236kb

input:

200000 15 44049967
999927791
29351
999894087
687373343
518282902
19725626
452930
6701
999697321
27032796
5498
259987
999999980
485673990
805879
315651507
166801295
189865616
999910671
6203805
978801777
41916529
942904087
997589912
17146790
922137016
444882952
999884603
999926345
938979650
18597283
9...

output:

598769574112

result:

ok single line: '598769574112'