-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDraft.h
More file actions
260 lines (229 loc) · 7.95 KB
/
Draft.h
File metadata and controls
260 lines (229 loc) · 7.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
//#include <bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <map>
#include <set>
#include <fstream>
#include <string>
#include <bitset>
#include <ctime>
#include <random>
#include <iterator>
#include <cmath>
#include <queue>
#include <deque>
#include <time.h>
#include <chrono>
#include <functional>
#define INCLUDE
using namespace std;
using ll = long long;
using ld = long double;
template <class T> using vec = vector<T>;
template <class T, size_t k> using arr = array<T, k>;
#define lmd(a, b, expr) [&](auto a, auto b) { return expr; }
#define all(a) (a).begin(), (a).end()
#define myset(T, a, b, cmp) multiset<T, function<bool(T, T)>> s([&](T a, T b) { return cmp; })
#define print(ans) cout << ans << "\n"
#define prret(ans) cout << ans << "\n"; return
#define ifret(stat, ans) if (stat) { prret(ans); }
#define ynret(stat) if (stat) { prret("YES"); } else { prret("NO"); }
#define fore(a) for (auto &e : (a))
#define bit(a, i) (((a) >> (i)) & 1LL)
#define mask(a, x) ((a) & (x))
#define dty(a) decltype(a)
#define tmp1 template <class T>
#define tmp2 template <class T, class N>
#define tmp3 template <class T, class N, class M>
#define tmpk template <class T, size_t k>
tmp1 auto sort(T &a) { sort(all(a)); }
tmp2 auto sort(T &a, N pr) { sort(all(a), pr); }
tmp1 auto rsort(T &a) { sort(all(a), lmd(b, c, b > c)); }
tmp1 auto rsort(T f, T l) { sort(f, l, lmd(b, c, b > c)); }
tmp2 auto low(T &a, const N &x) { return lower_bound(all(a), x); }
tmp2 auto low(T f, T l, const N &x) { return lower_bound(f, l, x); }
tmp3 auto low(T &a, const N &x, M pr) { return lower_bound(all(a), x, pr); }
tmp3 auto low(T f, T l, const N &x, M pr) { return lower_bound(f, l, x, pr); }
tmp2 auto upp(T &a, const N &x) { return upper_bound(all(a), x); }
tmp2 auto upp(T f, T l, const N &x) { return upper_bound(f, l, x); }
tmp3 auto upp(T &a, const N &x, M pr) { return upper_bound(all(a), x, pr); }
tmp3 auto upp(T f, T l, const N &x, M pr) { return upper_bound(f, l, x, pr); }
tmp1 auto rev(T &a) { reverse(all(a)); }
tmp1 auto rev(T f, T l) { reverse(f, l); }
tmp1 auto mne(T &a) { return min_element(all(a)); }
tmp1 auto mne(T f, T l) { return min_element(f, l); }
tmp2 auto mne(T &a, N pr) { return min_element(all(a), pr); }
tmp2 auto mne(T f, T l, N pr) { return min_element(f, l, pr); }
tmp1 auto mxe(T &a) { return max_element(all(a)); }
tmp1 auto mxe(T f, T l) { return max_element(f, l); }
tmp2 auto mxe(T &a, N pr) { return max_element(all(a), pr); }
tmp2 auto mxe(T f, T l, N pr) { return max_element(f, l, pr); }
tmp2 auto min(T a, N b) { return sizeof(a) > sizeof(b) ? min(a, (T)b) : min((N)a, b); }
tmp2 auto max(T a, N b) { return sizeof(a) > sizeof(b) ? max(a, (T)b) : max((N)a, b); }
#ifdef __GNUC__
__extension__ typedef __int128 int128;
__extension__ typedef __float128 float128;
auto &operator>>(istream &is, int128 &x) { ll e; is >> e; x = e; return is; }
auto &operator>>(istream &is, float128 &x) { long double e; is >> e; x = e; return is; }
auto &operator<<(ostream &os, int128 &x) { return os << (long long)x; }
auto &operator<<(ostream &os, float128 &x) { return os << (long double)x; }
#else
using int128 = long long;
using float128 = long double;
#endif
tmp1 auto &operator>>(istream &is, vec<T> &x) { fore(x) is >> e; return is; }
tmp1 auto &operator>>(istream &is, vec<vec<T>> &x) { fore(x) is >> e; return is; }
tmpk auto &operator>>(istream &is, arr<T, k> &x) { fore(x) is >> e; return is; }
tmpk auto &operator>>(istream &is, vec<arr<T, k>> &x) { fore(x) is >> e; return is; }
tmp1 auto &operator<<(ostream &os, vec<T> &x) { fore(x) os << e << " "; return os; }
tmp1 auto &operator<<(ostream &os, vec<vec<T>> &x) { fore(x) os << e << "\n"; return os; }
tmpk auto &operator<<(ostream &os, arr<T, k> &x) { fore(x) os << e << " "; return os; }
tmpk auto &operator<<(ostream &os, vec<arr<T, k>> &x) { fore(x) os << e << "\n"; return os; }
// get i-th bit from a: #DEFINE bit(a,i) (((a)>>(i))&1LL)
// x is a group like indicator: bit(x,i) == 1 iff i in x
// intersection group a and group x: #DEFINE mask(a,x) ((a)&(x))
auto start_time = chrono::steady_clock::now();
auto finish_time = chrono::steady_clock::now();
random_device rd;
mt19937 mt(rd());
ll divup(ll a, ll b) { return a / b + ((a ^ b) > 0 && a % b); } // dividing a by b rounded up
ll divdown(ll a, ll b) { return a / b - ((a ^ b) < 0 && a % b); } // dividing a by b rounded down
ll truemod(ll a, ll mod) { return a = a % mod, a + (a < 0) * mod; }
ll gcd(ll x, ll y) { while (y) { x %= y; swap(x, y); } return abs(x); }
ll lcm(ll x, ll y) { return abs(x * y) / gcd(x, y); }
const ll inf = (ll)1e18;
ll mod = 998244353;
const ll N = 1e5 + 2;
struct ll_segtree {
private:
struct node;
ll n;
vec<node> tree;
public:
ll_segtree(vec<ll> &a) {
n = a.size();
tree.resize(4 * n + 1);
build(0, n, 1, a);
}
// add x on close segment [ql, qr]
void add(ll ql, ll qr, ll x) {
add(0, n, ql, qr + 1, x);
}
// get sum on close segment [ql, qr]
ll query(ll ql, ll qr) {
return query(0, n, ql, qr + 1);
}
private:
struct node {
ll sum{}, err{};
node() {};
node(ll x) : sum(x) {};
node(ll x, ll err) : sum(x), err(err) {};
friend node operator+(node &a, node &b) { return node(a.sum + b.sum); };
};
ll left(ll v) { return v << 1; }
ll right(ll v) { return (v << 1) + 1; }
void build(ll l, ll r, ll v, vec<ll> &a) {
if (r - l == 1) {
tree[v] = a[l];
return;
}
ll m = (l + r) >> 1ll;
build(l, m, left(v), a);
build(m, r, right(v), a);
tree[v] = tree[left(v)] + tree[right(v)];
}
void destroy(ll v, ll err) {
tree[v].err += err;
}
void push(ll l, ll r, ll v) {
destroy(left(v), tree[v].err);
destroy(right(v), tree[v].err);
}
void lazy_repair(ll l, ll r, ll v) {
if (r - l > 1)
push(l, r, v);
tree[v].sum += (r - l) * tree[v].err;
tree[v].err = 0;
}
// add x on segment [l, r)
void add(ll l, ll r, ll ql, ll qr, ll x, ll v = 1) {
lazy_repair(l, r, v);
if (ql >= r || qr <= l)
return;
if (ql <= l && r <= qr) {
destroy(v, x);
lazy_repair(l, r, v);
return;
}
ll m = (l + r) >> 1ll;
add(l, m, ql, qr, x, left(v));
add(m, r, ql, qr, x, right(v));
tree[v] = tree[left(v)] + tree[right(v)];
}
// get sum on segment [l, r)
ll query(ll l, ll r, ll ql, ll qr, ll v = 1) {
lazy_repair(l, r, v);
if (ql >= r || qr <= l)
return 0;
if (ql <= l && r <= qr)
return tree[v].sum;
ll m = (l + r) >> 1ll;
ll x = query(l, m, ql, qr, left(v));
ll y = query(m, r, ql, qr, right(v));
tree[v] = tree[left(v)] + tree[right(v)];
return x + y;
}
};
//ifstream fin("input.txt");
//ofstream fout("output.txt");
//#define cin fin
//#define cout fout
void precalc() {
}
//#define TESTS
void solve() {
ll n, k; cin >> n >> k;
vec<ll> a(n); cin >> a;
sort(a);
ll sum = 0;
if (k == 1) {
fore(a) sum += e;
print(sum);
return;
}
ll x = 0;
vec<ll> b(n);
ll_segtree st(b);
ll a = 0, b = 0, c = 0, d = 0;
while (j < n) {
if (st.query())
st.add(i, i + k - 2, 1);
while (st.query())
}
}
// #define COUNTING_TIME
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
precalc();
signed T = 1;
#ifdef TESTS
cin >> T;
#endif
#if defined(COUNTING_TIME) & defined(TESTS)
start_time = chrono::steady_clock::now();
#endif
while (T--)
solve();
#if defined(COUNTING_TIME) & defined(TESTS)
finish_time = chrono::steady_clock::now();
auto elapsed_ms =
chrono::duration_cast<chrono::milliseconds>(finish_time - start_time);
cerr << "\nElapsed time: " << elapsed_ms.count() << " ms\n";
#endif
return 0;
}