結果
問題 | No.2546 Many Arithmetic Sequences |
ユーザー | 👑 Nachia |
提出日時 | 2023-11-25 01:37:01 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 172 ms / 2,000 ms |
コード長 | 3,764 bytes |
コンパイル時間 | 1,307 ms |
コンパイル使用メモリ | 99,228 KB |
実行使用メモリ | 31,716 KB |
最終ジャッジ日時 | 2024-09-26 11:18:08 |
合計ジャッジ時間 | 6,220 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 75 ms
24,860 KB |
testcase_04 | AC | 118 ms
18,000 KB |
testcase_05 | AC | 99 ms
17,732 KB |
testcase_06 | AC | 24 ms
9,036 KB |
testcase_07 | AC | 46 ms
10,520 KB |
testcase_08 | AC | 17 ms
9,088 KB |
testcase_09 | AC | 40 ms
8,860 KB |
testcase_10 | AC | 70 ms
13,416 KB |
testcase_11 | AC | 81 ms
14,496 KB |
testcase_12 | AC | 37 ms
10,032 KB |
testcase_13 | AC | 81 ms
17,708 KB |
testcase_14 | AC | 78 ms
15,296 KB |
testcase_15 | AC | 22 ms
6,436 KB |
testcase_16 | AC | 74 ms
14,140 KB |
testcase_17 | AC | 108 ms
29,260 KB |
testcase_18 | AC | 61 ms
11,940 KB |
testcase_19 | AC | 71 ms
17,148 KB |
testcase_20 | AC | 42 ms
8,668 KB |
testcase_21 | AC | 50 ms
16,284 KB |
testcase_22 | AC | 84 ms
17,876 KB |
testcase_23 | AC | 169 ms
30,044 KB |
testcase_24 | AC | 172 ms
30,048 KB |
testcase_25 | AC | 172 ms
30,044 KB |
testcase_26 | AC | 171 ms
29,920 KB |
testcase_27 | AC | 168 ms
30,048 KB |
testcase_28 | AC | 120 ms
21,352 KB |
testcase_29 | AC | 120 ms
21,224 KB |
testcase_30 | AC | 120 ms
21,348 KB |
testcase_31 | AC | 119 ms
21,224 KB |
testcase_32 | AC | 120 ms
21,348 KB |
testcase_33 | AC | 2 ms
5,376 KB |
testcase_34 | AC | 135 ms
31,716 KB |
testcase_35 | AC | 2 ms
5,376 KB |
testcase_36 | AC | 121 ms
29,712 KB |
testcase_37 | AC | 2 ms
5,376 KB |
testcase_38 | AC | 2 ms
5,376 KB |
ソースコード
#include <iostream> #include <string> #include <vector> #include <algorithm> #include <utility> #include <queue> #include <atcoder/modint> using namespace std; using i64 = long long; using u64 = unsigned long long; #define rep(i,n) for(i64 i=0; i<(i64)(n); i++) #define repr(i,n) for(i64 i=(i64)(n)-1; i>=0; i--) const i64 INF = 4001001001001001001; using Modint = atcoder::static_modint<998244353>; struct LiChaoTree{ using Val = i64; struct Func { i64 a, b; Val operator()(i64 x) { return a * x + b; } }; static Func infFunc() { return { 0,INF }; } int N; vector<Func> V; vector<bool> visited; LiChaoTree(i64 n){ N = 1; while(N < n) N *= 2; V.assign(N*2, infFunc()); visited.assign(N*2, false); } void addSegment(i64 l, i64 r, Func f){ if(l >= r) return; auto dfs = [&,this](int i,Func f,i64 a,i64 b,auto dfs) -> void { visited[i] = true; if(i >= V.size()) return; if(r <= a || b <= l) return; i64 m = (a+b)/2; if(!(l <= a && b <= r)){ dfs(i*2,f,a,m,dfs); dfs(i*2+1,f,m,b,dfs); return; } bool greatf_l = !(V[i](a) < f(a)); bool greatf_r = !(V[i](b-1) < f(b-1)); if(!greatf_l && !greatf_r) return; if(greatf_l && greatf_r){ V[i] = f; return; } if(a + 1 == b) return; if(!(V[i](m) < f(m))){ swap(V[i],f); swap(greatf_l,greatf_r); } if(greatf_l) dfs(i*2,f,a,m,dfs); else dfs(i*2+1,f,m,b,dfs); }; dfs(1,f,0,N,dfs); } void addLine(Func f){ addSegment(0,N,f); } Val minVal(i64 p){ int i = 1; Val res = infFunc()(p); int l = 0, r = N; while(i < V.size()){ if(!visited[i]) break; res = min(res,V[i](p)); i64 m = (l+r)/2; if(p < m){ i = i*2; r = m; } else{ i = i*2+1; l = m; } } return res; } Func minFunc(i64 p){ int i = 1; Func res = infFunc(); int l = 0, r = N; while(i < V.size()){ if(!visited[i]) break; if(V[i](p) < res(p)) res = V[i]; i64 m = (l+r)/2; if(p < m){ i = i*2; r = m; } else{ i = i*2+1; l = m; } } return res; } }; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); i64 N, M; cin >> N >> M; vector<pair<i64, i64>> A, B; rep(i,N){ i64 a,d; cin >> a >> d; if(d <= 0) B.push_back({ d, a }); else A.push_back({ d, a }); } auto nthTerm = [](i64 d, i64 a, i64 n) -> i64 { return a + d * n; }; auto prefixSum = [](i64 d, i64 a, i64 n) -> i64 { i64 ans = a * n; ans += d * ((n*n-n)/2); return ans; }; vector<i64> valdown; if(!B.empty()){ int n = B.size(); vector<int> ptr(n); priority_queue<pair<i64, i64>> que; rep(v,n){ auto [d,a] = B[v]; que.push({ nthTerm(d, a, ptr[v]), v }); } rep(i,M){ auto [p,v] = que.top(); que.pop(); valdown.push_back(p); ptr[v]++; auto [d,a] = B[v]; que.push({ nthTerm(d, a, ptr[v]), v }); } } else { rep(v,M) valdown.push_back(-500000'0000000); } vector<i64> valup(M+1); if(!A.empty()) { LiChaoTree ds(M+1); for(auto [d,a] : A){ LiChaoTree::Func f; f.a = -d; f.b = -2*a+d; ds.addLine(f); } for(int i=0; i<=M; i++){ i64 q = -ds.minVal(i); valup[i] = q * i / 2; } } else { rep(i,M) valup[i+1] = -INF; } vector<i64> valdownSum(M+1); rep(i,M) valdownSum[i+1] = valdownSum[i] + valdown[i]; i64 ans = -INF; rep(i,M+1) ans = max(ans, valdownSum[i] + valup[M-i]); cout << ans << endl; return 0; }