QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#252244 | #4590. Happy Travelling | lam | TL | 221ms | 34784kb | C++14 | 5.3kb | 2023-11-15 17:08:28 | 2023-11-15 17:08:28 |
Judging History
answer
/*
Author : DeMen100ns (a.k.a Vo Khac Trieu)
School: Ho Chi Minh City University of Science
*/
#include <bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
template<typename T> int SIZE(T (&t)){ return t.size(); } template<typename T, size_t N> int SIZE(T (&t)[N]){ return N; } string to_string(char t){ return "'" + string({t}) + "'"; } string to_string(bool t){ return t ? "true" : "false"; } string to_string(const string &t, int x1=0, int x2=1e9){ string ret = ""; for(int i = min(x1,SIZE(t)), _i = min(x2,SIZE(t)-1); i <= _i; ++i){ ret += t[i]; } return '"' + ret + '"'; } string to_string(const char* t){ string ret(t); return to_string(ret); } template<size_t N> string to_string(const bitset<N> &t, int x1=0, int x2=1e9){ string ret = ""; for(int i = min(x1,SIZE(t)); i <= min(x2,SIZE(t)-1); ++i){ ret += t[i] + '0'; } return to_string(ret); } template<typename T, typename... Coords> string to_string(const T (&t), int x1=0, int x2=1e9, Coords... C); template<typename T, typename S> string to_string(const pair<T, S> &t){ return "(" + to_string(t.first) + ", " + to_string(t.second) + ")"; } template<typename T, typename... Coords> string to_string(const T (&t), int x1, int x2, Coords... C){ string ret = "["; x1 = min(x1, SIZE(t)); auto e = begin(t); advance(e,x1); for(int i = x1, _i = min(x2,SIZE(t)-1); i <= _i; ++i){ ret += to_string(*e, C...) + (i != _i ? ", " : ""); e = next(e); } return ret + "]"; } template<int Index, typename... Ts> struct print_tuple{ string operator() (const tuple<Ts...>& t) { string ret = print_tuple<Index - 1, Ts...>{}(t); ret += (Index ? ", " : ""); return ret + to_string(get<Index>(t)); } }; template<typename... Ts> struct print_tuple<0, Ts...> { string operator() (const tuple<Ts...>& t) { return to_string(get<0>(t)); } }; template<typename... Ts> string to_string(const tuple<Ts...>& t) { const auto Size = tuple_size<tuple<Ts...>>::value; return print_tuple<Size - 1, Ts...>{}(t); } void dbgr(){;} template<typename Heads, typename... Tails> void dbgr(Heads H, Tails... T){ cout << to_string(H) << " | "; dbgr(T...); } void dbgs(){;} template<typename Heads, typename... Tails> void dbgs(Heads H, Tails... T){ cout << H << " "; dbgs(T...); }
#define dbgv(...) cout << to_string(__VA_ARGS__) << endl;
#define dbg(...) cout << "[" << #__VA_ARGS__ << "]: "; dbgv(__VA_ARGS__);
#define dbgr(...) dbgr(__VA_ARGS__); cout << endl;
#define dbgm(...) cout << "[" << #__VA_ARGS__ << "]: "; dbgr(__VA_ARGS__);
const int N = 1e5 + 5;
const long long INF = numeric_limits<long long>::max() / 2;
const int MAXA = 1e9;
const int B = sqrt(N) + 5;
int n, k, d;
int a[N], dp[N], h[N], t[N];
vector <int> sum[N];
multiset <int> cur[N];
vector <int> er[2 * N];
int seg[4 * N];
void upd(int id, int l, int r, int u, int v, int val){
if (l > v || r < u) return;
if (l >= u && r <= v){
seg[id] = max(seg[id], val);
// lazy[id] = max(lazy[id], val);
return;
}
//down(id);
int mid = (l + r) >> 1;
upd(id << 1, l, mid, u, v, val);
upd(id << 1 | 1, mid + 1, r, u, v, val);
}
int get(int id, int l, int r, int pos){
if (l == r) return seg[id];
int mid = (l + r) >> 1;
if (pos <= mid) return max(seg[id], get(id << 1, l, mid, pos));
else return max(seg[id], get(id << 1 | 1, mid + 1, r, pos));
}
void case1(){
for(int i = 0; i < n; ++i){
int l = i + 1, r = i + t[i + 1];
// if (i / k > 0){
// sum[i % k][i / k] += sum[i % k][i / k - 1];
// }
for(int val : er[i]){
cur[i % k].erase(cur[i % k].find(val));
}
int val;
// dbgm(i, (i / k) * -d, cur[i % k], h[i + 1])
if (i > 0) val = (i / k) * -d + (*prev(cur[i % k].end())) + h[i + 1];
else val = h[1];
// dbgm(i, val)
if (i == n - 1){
cout << val;
return;
}
for(int j = 0; j < k; ++j){
int lk = (l - 1) / k, rk = r / k;
if ((l - 1) % k > j) ++lk;
if (r % k < j) --rk;
//dbgm(j, l, r, lk, rk)
if (lk > rk) continue;
//dbgm(val + lk * d)
cur[j].insert(val + lk * d);
er[(rk + 1) * k + j].push_back(val + lk * d);
// dbg((rk + 1) * k + j)
// pol[j][lk] -= d;
// if (rk + 1 < pol[j].size()) pol[j][rk + 1] += d;
// sum[j][lk] += lk * d;
// if (rk + 1 < sum[j].size()) sum[j][rk + 1] -= lk * d;
}
}
}
void case2(){
fill_n(seg, 4 * n + 1, -INF);
upd(1, 1, n, 1, 1, 0);
for(int i = 1; i < n; ++i){
dp[i] = get(1, 1, n, i) + h[i];
for(int l = i, r = min(i + k - 1, i + t[i]), v = 0; l <= i + t[i]; l += k, r = min(r + k, i + t[i]), v += d){
//cout << i << " : " << l << " " << r << " -> " << dp[i] << " " << v << endl;
upd(1, 1, n, max(i + 1, l), r, dp[i] - v);
}
}
dp[n] = get(1, 1, n, n) + h[n];
cout << dp[n];
}
void solve(){
cin >> n >> k >> d;
for(int i = 1; i <= n; ++i) cin >> h[i];
for(int i = 1; i < n; ++i) cin >> t[i];
if (k < B / 3) case1();
else case2();
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0);
int t = 1; // cin >> t;
for(int test = 1; test <= t; ++test){
solve();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 18092kb
input:
6 2 1 8 -7 -8 9 0 2 5 3 3 2 1
output:
18
result:
ok single line: '18'
Test #2:
score: 0
Accepted
time: 0ms
memory: 17868kb
input:
8 8 8 10 -5 -5 -5 -5 -5 -5 10 5 2 5 3 2 1 1
output:
15
result:
ok single line: '15'
Test #3:
score: 0
Accepted
time: 0ms
memory: 18156kb
input:
13 2 2 -5 -4 -4 -1 7 -6 -5 -4 -3 -2 -1 5 -7 3 10 9 8 7 6 5 4 3 2 1 1
output:
-9
result:
ok single line: '-9'
Test #4:
score: 0
Accepted
time: 0ms
memory: 18184kb
input:
2 1 0 -10000 10000 1
output:
0
result:
ok single line: '0'
Test #5:
score: 0
Accepted
time: 221ms
memory: 34784kb
input:
98987 4 3 -8225 -8961 -5537 -5621 -8143 -5214 -5538 -6912 -6601 -8839 -7872 -7867 -9553 -9793 -7333 -7360 -5820 -7459 -8824 -9716 -9757 -5846 -5300 -5912 -7953 -8360 -7609 -5937 -5525 -9748 -7326 -8311 -9979 -9292 -8542 -7589 -7939 -5914 -7985 -9999 -9212 -8274 -8084 -6620 -5991 -7826 -6327 -5228 -6...
output:
-84108
result:
ok single line: '-84108'
Test #6:
score: 0
Accepted
time: 91ms
memory: 26388kb
input:
98467 2 3 -5677 -9080 -6665 -5838 -5755 -8938 -6286 -5160 -7147 -8370 -8214 -6088 -9763 -5183 -7123 -7264 -5298 -8855 -6381 -6592 -9216 -8429 -9598 -7443 -7393 -8712 -5545 -6778 -6010 -5717 -9102 -7968 -6140 -9592 -7917 -5217 -5015 -7798 -9339 -5678 -7073 -7607 -7961 -6185 -9941 -6421 -8779 -5388 -8...
output:
-150169
result:
ok single line: '-150169'
Test #7:
score: 0
Accepted
time: 74ms
memory: 26168kb
input:
96173 2 1 -6463 -9099 -6269 -6169 -8273 -9839 -9929 -5447 -5908 -6884 -6908 -8359 -6477 -9414 -9207 -8180 -6264 -9293 -8981 -6557 -9260 -9700 -6785 -7121 -8382 -9712 -5178 -5821 -9107 -9004 -7472 -9306 -9311 -7160 -7965 -5394 -8048 -7415 -5233 -7746 -7390 -5298 -7721 -7915 -9646 -5371 -5712 -6234 -5...
output:
-45780
result:
ok single line: '-45780'
Test #8:
score: 0
Accepted
time: 25ms
memory: 22808kb
input:
96905 1 1 -7933 -5685 -6201 -5415 -7294 -9904 -8968 -8433 -6287 -6727 -5933 -5418 -8953 -6743 -7057 -7087 -7244 -5302 -5130 -8934 -5053 -9811 -8695 -5989 -7785 -5991 -8351 -9463 -7037 -8867 -8956 -8194 -5272 -6270 -7737 -7793 -8851 -5839 -5178 -7009 -8018 -7375 -6091 -9226 -7117 -9522 -9248 -6036 -8...
output:
-72336
result:
ok single line: '-72336'
Test #9:
score: 0
Accepted
time: 45ms
memory: 30212kb
input:
97070 2 1 -6843 -9736 -7145 -9801 -6733 -7807 -8835 -5776 -6971 -7101 -8428 -7429 -9250 -9405 -6152 -7904 -5761 -8347 -6597 -8875 -8741 -7759 -8905 -9136 -8820 -9272 -9124 -6384 -7302 -5351 -8660 -5499 -9551 -9598 -9332 -7226 -5354 -9753 -7657 -5512 -7806 -5845 -5320 -9052 -8654 -9068 -5744 -7548 -7...
output:
-52581
result:
ok single line: '-52581'
Test #10:
score: 0
Accepted
time: 19ms
memory: 20780kb
input:
95643 95643 0 -5396 -7667 -7724 -8395 -5929 -5814 -6879 -7612 -6487 -5657 -6266 -7551 -8950 -6584 -5148 -7025 -7570 -6296 -6031 -7598 -5852 -5859 -7943 -6304 -6896 -5652 -9303 -5869 -9073 -6843 -7438 -6428 -8896 -5239 -7793 -6730 -7331 -7202 -8118 -9344 -8450 -8315 -8402 -8705 -5246 -9043 -7347 -872...
output:
-13774
result:
ok single line: '-13774'
Test #11:
score: 0
Accepted
time: 20ms
memory: 21432kb
input:
100000 99999 476 -109 -1014 -1404 -1837 -712 -1500 -1760 -751 184 -333 -860 -418 -1676 -1499 -1506 -456 -1692 -1473 -1872 -1523 -599 461 -583 -1685 -94 121 53 -385 192 -562 -1169 220 366 -746 -584 -1395 325 -502 98 -1773 -293 -72 -1894 -449 416 -1001 -1350 -1842 -1279 -528 323 -1196 32 -83 -1112 410...
output:
4735350
result:
ok single line: '4735350'
Test #12:
score: -100
Time Limit Exceeded
input:
93009 101 191 -278 -103 -706 -686 -521 -375 -330 -183 -381 -91 -686 -683 -89 -448 -724 -71 -374 -470 -214 -475 -505 -599 -108 -157 -199 -541 -509 -477 -62 -96 -415 -49 -524 -93 -230 -745 -561 -47 -451 -165 -763 -461 -357 -555 -757 -52 -246 -297 -145 -599 -36 -366 -157 -200 -38 -344 -105 -509 -54 -41...