Submission #66462


ソースコード

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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
#ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
using namespace std;
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#endif
#pragma region Macros
// rep macro
#define foa(v, a) for(auto &v : a)
#define REPname(a, b, c, d, e, ...) e
#define REP(...) REPname(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)
#define REP0(x) for(int i = 0; i < (x); ++i)
#define REP1(i, x) for(int i = 0; i < (x); ++i)
#define REP2(i, l, r) for(int i = (l); i < (r); ++i)
#define REP3(i, l, r, c) for(int i = (l); i < (r); i += (c))
#define REPSname(a, b, c, ...) c
#define REPS(...) REPSname(__VA_ARGS__, REPS1, REPS0)(__VA_ARGS__)
#define REPS0(x) for(int i = 1; i <= (x); ++i)
#define REPS1(i, x) for(int i = 1; i <= (x); ++i)
#define RREPname(a, b, c, d, e, ...) e
#define RREP(...) RREPname(__VA_ARGS__, RREP3, RREP2, RREP1, RREP0)(__VA_ARGS__)
#define RREP0(x) for(int i = (x)-1; i >= 0; --i)
#define RREP1(i, x) for(int i = (x)-1; i >= 0; --i)
#define RREP2(i, l, r) for(int i = (r)-1; i >= (l); --i)
#define RREP3(i, l, r, c) for(int i = (r)-1; i >= (l); i -= (c))
#define RREPSname(a, b, c, ...) c
#define RREPS(...) RREPSname(__VA_ARGS__, RREPS1, RREPS0)(__VA_ARGS__)
#define RREPS0(x) for(int i = (x); i >= 1; --i)
#define RREPS1(i, x) for(int i = (x); i >= 1; --i)
// name macro
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define SZ(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define popcnt(x) __builtin_popcountll(x)
template <class T = int>
using V = std::vector<T>;
template <class T = int>
using VV = std::vector<std::vector<T>>;
template <class T = int>
using VVV = std::vector<std::vector<std::vector<T>>>;
template <class T>
using pqup = std::priority_queue<T, std::vector<T>, std::greater<T>>;
using ll = long long;
using ld = long double;
using int128 = __int128_t;
using pii = std::pair<int, int>;
using pll = std::pair<long long, long long>;
// input macro
template <class T, class U>
std::istream &operator>>(std::istream &is, std::pair<T, U> &p) {
is >> p.first >> p.second;
return is;
}
template <class T>
std::istream &operator>>(std::istream &is, std::vector<T> &v) {
for(T &i : v) is >> i;
return is;
}
std::istream &operator>>(std::istream &is, __int128_t &a) {
std::string s;
is >> s;
__int128_t ret = 0;
for(int i = 0; i < s.length(); i++)
if('0' <= s[i] and s[i] <= '9')
ret = 10 * ret + s[i] - '0';
a = ret * (s[0] == '-' ? -1 : 1);
return is;
}
namespace scanner {
void scan(int &a) { std::cin >> a; }
void scan(long long &a) { std::cin >> a; }
void scan(std::string &a) { std::cin >> a; }
void scan(char &a) { std::cin >> a; }
void scan(char a[]) { std::scanf("%s", a); }
void scan(double &a) { std::cin >> a; }
void scan(long double &a) { std::cin >> a; }
template <class T, class U>
void scan(std::pair<T, U> &p) { std::cin >> p; }
template <class T>
void scan(std::vector<T> &a) { std::cin >> a; }
void INPUT() {}
template <class Head, class... Tail>
void INPUT(Head &head, Tail &... tail) {
scan(head);
INPUT(tail...);
}
} // namespace scanner
#define VEC(type, name, size) \
std::vector<type> name(size); \
scanner::INPUT(name)
#define VVEC(type, name, h, w) \
std::vector<std::vector<type>> name(h, std::vector<type>(w)); \
scanner::INPUT(name)
#define INT(...) \
int __VA_ARGS__; \
scanner::INPUT(__VA_ARGS__)
#define LL(...) \
long long __VA_ARGS__; \
scanner::INPUT(__VA_ARGS__)
#define STR(...) \
std::string __VA_ARGS__; \
scanner::INPUT(__VA_ARGS__)
#define CHAR(...) \
char __VA_ARGS__; \
scanner::INPUT(__VA_ARGS__)
#define DOUBLE(...) \
double __VA_ARGS__; \
scanner::INPUT(__VA_ARGS__)
#define LD(...) \
long double __VA_ARGS__; \
scanner::INPUT(__VA_ARGS__)
// output-macro
template <class T, class U>
std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {
os << p.first << " " << p.second;
return os;
}
template <class T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &a) {
for(int i = 0; i < int(a.size()); ++i) {
if(i) os << " ";
os << a[i];
}
return os;
}
std::ostream &operator<<(std::ostream &dest, __int128_t &value) {
std::ostream::sentry s(dest);
if(s) {
__uint128_t tmp = value < 0 ? -value : value;
char buffer[128];
char *d = std::end(buffer);
do {
--d;
*d = "0123456789"[tmp % 10];
tmp /= 10;
} while(tmp != 0);
if(value < 0) {
--d;
*d = '-';
}
int len = std::end(buffer) - d;
if(dest.rdbuf()->sputn(d, len) != len) {
dest.setstate(std::ios_base::badbit);
}
}
return dest;
}
template <class T>
void print(const T a) { std::cout << a << '\n'; }
template <class Head, class... Tail>
void print(Head H, Tail... T) {
std::cout << H << ' ';
print(T...);
}
template <class T>
void printel(const T a) { std::cout << a << '\n'; }
template <class T>
void printel(const std::vector<T> &a) {
for(const auto &v : a)
std::cout << v << '\n';
}
template <class Head, class... Tail>
void printel(Head H, Tail... T) {
std::cout << H << '\n';
printel(T...);
}
void Yes(const bool b = true) { std::cout << (b ? "Yes\n" : "No\n"); }
void No() { std::cout << "No\n"; }
void YES(const bool b = true) { std::cout << (b ? "YES\n" : "NO\n"); }
void NO() { std::cout << "No\n"; }
void err(const bool b = true) {
if(b) {
std::cout << "-1\n", exit(0);
}
}
//debug macro
namespace debugger {
template <class T>
void view(const std::vector<T> &a) {
std::cerr << "{ ";
for(const auto &v : a) {
std::cerr << v << ", ";
}
std::cerr << "\b\b }";
}
template <class T>
void view(const std::vector<std::vector<T>> &a) {
std::cerr << "{\n";
for(const auto &v : a) {
std::cerr << "\t";
view(v);
std::cerr << "\n";
}
std::cerr << "}";
}
template <class T, class U>
void view(const std::vector<std::pair<T, U>> &a) {
std::cerr << "{\n";
for(const auto &p : a) std::cerr << "\t(" << p.first << ", " << p.second << ")\n";
std::cerr << "}";
}
template <class T, class U>
void view(const std::map<T, U> &m) {
std::cerr << "{\n";
for(const auto &p : m) std::cerr << "\t[" << p.first << "] : " << p.second << "\n";
std::cerr << "}";
}
template <class T, class U>
void view(const std::pair<T, U> &p) { std::cerr << "(" << p.first << ", " << p.second << ")"; }
template <class T>
void view(const std::set<T> &s) {
std::cerr << "{ ";
for(auto &v : s) {
view(v);
std::cerr << ", ";
}
std::cerr << "\b\b }";
}
template <class T>
void view(const T &e) { std::cerr << e; }
} // namespace debugger
#ifdef LOCAL
void debug_out() {}
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
debugger::view(H);
std::cerr << ", ";
debug_out(T...);
}
#define debug(...) \
do { \
std::cerr << __LINE__ << " [" << #__VA_ARGS__ << "] : ["; \
debug_out(__VA_ARGS__); \
std::cerr << "\b\b]\n"; \
} while(false)
#else
#define debug(...) (void(0))
#endif
// vector macro
template <class T>
int lb(const std::vector<T> &a, const T x) { return std::distance((a).begin(), std::lower_bound((a).begin(), (a).end(), (x))); }
template <class T>
int ub(const std::vector<T> &a, const T x) { return std::distance((a).begin(), std::upper_bound((a).begin(), (a).end(), (x))); }
template <class T>
void UNIQUE(std::vector<T> &a) {
std::sort(a.begin(), a.end());
a.erase(std::unique(a.begin(), a.end()), a.end());
}
template <class T>
std::vector<T> press(std::vector<T> &a) {
auto res = a;
UNIQUE(res);
for(auto &v : a)
v = lb(res, v);
return res;
}
#define SORTname(a, b, c, ...) c
#define SORT(...) SORTname(__VA_ARGS__, SORT1, SORT0, ...)(__VA_ARGS__)
#define SORT0(a) std::sort((a).begin(), (a).end())
#define SORT1(a, c) std::sort((a).begin(), (a).end(), [](const auto x, const auto y) { return x c y; })
template <class T>
void ADD(std::vector<T> &a, const T x) {
for(auto &v : a) v += x;
}
template <class T>
void SUB(std::vector<T> &a, const T x = 1) {
for(auto &v : a) v -= x;
}
template <class T>
void MUL(std::vector<T> &a, const T x) {
for(auto &v : a) v *= x;
}
template <class T>
void DIV(std::vector<T> &a, const T x) {
for(auto &v : a) v /= x;
}
// math macro
template <class T, class U>
inline bool chmin(T &a, const U &b) { return a > b ? a = b, true : false; }
template <class T, class U>
inline bool chmax(T &a, const U &b) { return a < b ? a = b, true : false; }
template <class T>
T divup(T x, T y) { return (x + y - 1) / y; }
template <class T>
T POW(T a, long long n) {
T ret = 1;
while(n) {
if(n & 1) ret *= a;
a *= a;
n >>= 1;
}
return ret;
}
template<typename T>
T ADD(T a, T b){
T res;
return __builtin_add_overflow(a, b, &res) ? numeric_limits<T>::max() : res;
}
template<typename T>
T MUL(T a, T b){
T res;
return __builtin_mul_overflow(a, b, &res) ? numeric_limits<T>::max() : res;
}
// modpow
long long POW(long long a, long long n, const int mod) {
long long ret = 1;
while(n) {
if(n & 1) (ret *= a) %= mod;
(a *= a) %= mod;
n >>= 1;
}
return ret;
}
// others
struct fast_io {
fast_io() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed << setprecision(15);
}
} fast_io_;
#pragma endregion
using R = long double;
constexpr R EPS=1E-11;
// r の(誤差付きの)符号に従って, -1, 0, 1 を返す.
int sgn(const R& r){ return (r > EPS) - (r < -EPS); }
// a, b の(誤差付きの)大小比較の結果に従って, -1, 0, 1 を返す.
int sgn(const R& a, const R &b){ return sgn(a-b); }
// a > 0 は sgn(a) > 0
// a < b は sgn(a, b) < 0
// a >= b は sgn(a, b) >= 0
// のように書く.
// return s * 10^n
long long x10(const string& s, size_t n) {
if (s.front() == '-') return -x10(s.substr(1), n);
auto pos = s.find('.');
if (pos == string::npos) return stoll(s + string(n, '0'));
return stoll(s.substr(0, pos) + s.substr(pos + 1) + string(n + pos + 1 - s.size(), '0'));
}
long long ceildiv(long long a, long long b) {
if (b < 0) a = -a, b = -b;
if (a >= 0) return (a + b - 1) / b;
else return a / b;
}
long long floordiv(long long a, long long b) {
if (b < 0) a = -a, b = -b;
if (a >= 0) return a / b;
else return (a - b + 1) / b;
}
long long floorsqrt(long long x) {
assert(x >= 0);
long long ok = 0;
long long ng = 1;
while (ng * ng <= x) ng <<= 1;
while (ng - ok > 1) {
long long mid = (ng + ok) >> 1;
if (mid * mid <= x) ok = mid;
else ng = mid;
}
return ok;
}
inline int topbit(unsigned long long x){
return x?63-__builtin_clzll(x):-1;
}
inline int popcount(unsigned long long x){
return __builtin_popcountll(x);
}
inline int parity(unsigned long long x){//popcount%2
return __builtin_parity(x);
}
const int inf = 1e9;
const ll INF = 1e18;
void main_() {
LL(N,M,K);
LL(P);
V<pair<pll,ll>> s,t;
ll st = 0;
ll mx = 0;
REP(i,P){
LL(L,R,C);
mx = max(mx,R);
L--;
if(L != st){
pair<pll,ll> x;
x.fi.fi = st;
x.fi.se = L;
x.se = M;
s.push_back(x);
}
st = R;
pair<pll,ll> y;
y.fi.fi = L;
y.fi.se = R;
y.se = C;
s.push_back(y);
if(i == P-1 && mx != N){
pair<pll,ll> x;
x.fi.fi = R;
x.fi.se = N;
x.se = M;
s.push_back(x);
}
}
sort(all(s));
ll sum = 0;
ll l = 0;
ll r = 0;
ll ans = INF;
ll sz = s.size();
ll num = 0;
for(ll i = 0;i < sz;i++){
pair<pll,ll> x = s[i];
l = x.fi.fi;
if(l >= s[num].fi.fi){
r = l;
num = i;
sum = 0;
}
ll add = 0;
for(ll j = num; j < sz;j++){
pair<pll,ll> y = s[j];
r = y.fi.fi;
if(sum + (y.fi.se - y.fi.fi) * y.se < K){
sum += (y.fi.se - y.fi.fi) * y.se;
add = (y.fi.se - y.fi.fi) * y.se;
num = j;
continue;
}
else{
ll d = (K - sum - 1)/y.se + 1;
if(sum == K)d = 0;
r += d;
add = d * y.se;
sum += add;
num = j;
break;
}
}
if(sum >= K)ans = min(ans,r-l);
sum -= add;
r = s[num].fi.fi;
sum -= (x.fi.se - x.fi.fi) * x.se;
}
for(auto x:s){
pair<pll,ll> y;
y.fi.se = N - x.fi.fi;
y.fi.fi = N - x.fi.se;
y.se = x.se;
t.push_back(y);
}
sort(all(t));
s = t;
l = 0,r = 0,sum = 0,num = 0;
for(ll i = 0;i < sz;i++){
pair<pll,ll> x = s[i];
l = x.fi.fi;
if(l >= s[num].fi.fi){
r = l;
num = i;
sum = 0;
}
ll add = 0;
for(ll j = num; j < sz;j++){
pair<pll,ll> y = s[j];
r = y.fi.fi;
if(sum + (y.fi.se - y.fi.fi) * y.se < K){
sum += (y.fi.se - y.fi.fi) * y.se;
add = (y.fi.se - y.fi.fi) * y.se;
num = j;
continue;
}
else{
ll d = (K - sum - 1)/y.se + 1;
if(sum == K)d = 0;
r += d;
add = d * y.se;
sum += add;
num = j;
break;
}
}
if(sum >= K)ans = min(ans,r-l);
sum -= add;
r = s[num].fi.fi;
sum -= (x.fi.se - x.fi.fi) * x.se;
}
cout << ans << endl;
}
int main() {
int t = 1;
//cin >> t;
while(t--) main_();
return 0;
}

ステータス

項目 データ
問題 1493 - Sand of Star
ユーザー名 unos
投稿日時 2021-05-05 00:00:29
言語 C++17
状態 Accepted
得点 500
ソースコード長 15985 Byte
最大実行時間 64 ms
最大メモリ使用量 16936 KB

セット

セット 得点 Cases
1 ALL 500 / 500 *

テストケース

ファイル名 状態 実行時間 メモリ使用量 #
hand_made01.txt AC 24 ms 476 KB
1
hand_made02.txt AC 24 ms 436 KB
1
hand_made03.txt AC 53 ms 15604 KB
1
hand_made04.txt AC 22 ms 496 KB
1
hand_made05.txt AC 25 ms 580 KB
1
in01.txt AC 43 ms 9228 KB
1
in02.txt AC 64 ms 16304 KB
1
in03.txt AC 35 ms 7556 KB
1
in04.txt AC 50 ms 15504 KB
1
in05.txt AC 36 ms 7496 KB
1
in06.txt AC 37 ms 8388 KB
1
in07.txt AC 44 ms 8196 KB
1
in08.txt AC 54 ms 16540 KB
1
in09.txt AC 39 ms 4096 KB
1
in10.txt AC 47 ms 14256 KB
1
in11.txt AC 27 ms 1024 KB
1
in12.txt AC 22 ms 2448 KB
1
in13.txt AC 29 ms 4436 KB
1
in14.txt AC 49 ms 14528 KB
1
in15.txt AC 46 ms 16936 KB
1
in16.txt AC 28 ms 4156 KB
1
in17.txt AC 37 ms 8080 KB
1
in18.txt AC 45 ms 16752 KB
1
in19.txt AC 23 ms 2172 KB
1
in20.txt AC 46 ms 8696 KB
1
in21.txt AC 38 ms 4316 KB
1
in22.txt AC 60 ms 15052 KB
1
in23.txt AC 28 ms 2180 KB
1
in24.txt AC 37 ms 7408 KB
1
in25.txt AC 35 ms 7228 KB
1
in26.txt AC 48 ms 16512 KB
1
in27.txt AC 53 ms 16848 KB
1
in28.txt AC 30 ms 4080 KB
1
in29.txt AC 49 ms 14216 KB
1
in30.txt AC 54 ms 15088 KB
1
sample01.txt AC 23 ms 748 KB
1
sample02.txt AC 19 ms 572 KB
1