結果
問題 | No.2546 Many Arithmetic Sequences |
ユーザー | 👑 Nachia |
提出日時 | 2023-11-16 01:02:47 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
RE
|
実行時間 | - |
コード長 | 3,687 bytes |
コンパイル時間 | 1,426 ms |
コンパイル使用メモリ | 98,784 KB |
実行使用メモリ | 31,760 KB |
最終ジャッジ日時 | 2024-09-26 09:54:49 |
合計ジャッジ時間 | 6,408 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,812 KB |
testcase_01 | AC | 2 ms
6,940 KB |
testcase_02 | AC | 3 ms
6,940 KB |
testcase_03 | AC | 72 ms
25,404 KB |
testcase_04 | AC | 115 ms
18,020 KB |
testcase_05 | AC | 97 ms
17,716 KB |
testcase_06 | AC | 23 ms
9,144 KB |
testcase_07 | AC | 46 ms
10,468 KB |
testcase_08 | AC | 16 ms
8,960 KB |
testcase_09 | AC | 41 ms
8,852 KB |
testcase_10 | AC | 72 ms
13,288 KB |
testcase_11 | AC | 81 ms
16,824 KB |
testcase_12 | AC | 37 ms
10,152 KB |
testcase_13 | AC | 80 ms
17,624 KB |
testcase_14 | AC | 78 ms
15,332 KB |
testcase_15 | AC | 24 ms
6,940 KB |
testcase_16 | AC | 73 ms
14,260 KB |
testcase_17 | AC | 104 ms
29,776 KB |
testcase_18 | AC | 59 ms
11,900 KB |
testcase_19 | AC | 68 ms
17,160 KB |
testcase_20 | AC | 42 ms
8,656 KB |
testcase_21 | AC | 50 ms
16,300 KB |
testcase_22 | AC | 86 ms
17,840 KB |
testcase_23 | AC | 172 ms
30,568 KB |
testcase_24 | AC | 167 ms
30,384 KB |
testcase_25 | AC | 167 ms
30,540 KB |
testcase_26 | AC | 159 ms
30,876 KB |
testcase_27 | AC | 163 ms
30,044 KB |
testcase_28 | AC | 113 ms
23,912 KB |
testcase_29 | AC | 114 ms
23,024 KB |
testcase_30 | AC | 115 ms
23,824 KB |
testcase_31 | AC | 116 ms
23,000 KB |
testcase_32 | AC | 112 ms
23,336 KB |
testcase_33 | AC | 2 ms
6,940 KB |
testcase_34 | AC | 130 ms
31,760 KB |
testcase_35 | AC | 3 ms
6,940 KB |
testcase_36 | AC | 118 ms
31,340 KB |
testcase_37 | RE | - |
testcase_38 | AC | 2 ms
6,940 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; { 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 }); } } 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; }