Submission #952590

#TimeUsernameProblemLanguageResultExecution timeMemory
952590nguyennhDivide and conquer (IZhO14_divide)C++14
100 / 100
41 ms8636 KiB
#include<bits/stdc++.h> #define el '\n' using namespace std ; const int MN = 1e5 + 6; const int64_t inf = 1e18; int x[MN] , g[MN] , d[MN] , n; namespace sub_trau{ void solve(){ int64_t ans = 0; vector<int64_t> pref_gold(n + 5) , pref_en(n + 5); for ( int i = 1 ; i <= n ; i++ ){ pref_gold[i] = pref_gold[i - 1] + g[i]; pref_en[i] = pref_en[i - 1] + d[i]; } for ( int i = 1 ; i <= n ; i++ ){ for ( int j = i ; j <= n ; j++ ){ if (pref_en[j] - pref_en[i - 1] >= x[j] - x[i]) ans = max(ans , pref_gold[j] - pref_gold[i - 1]); } } cout << ans; } } namespace sub_final{ struct Segtri{ vector<int64_t> st; Segtri(int n) : st(4 * n + 5) { for ( int i = 1 ; i <= n ; i++ ) st[i] = inf; } void update(int id , int l , int r , int i , int64_t val){ if (i < l || i > r) return; if (l == r){ st[id] = val; return; } int mid = l + r >> 1; update(id << 1 , l , mid , i , val); update(id << 1 | 1 , mid + 1 , r , i , val); st[id] = min(st[id << 1] , st[id << 1 | 1]); } int walk(int id , int l , int r , int64_t x){ if (l == r) return l; int mid = l + r >> 1; if (st[id << 1] <= x) return walk(id << 1 , l , mid , x); return walk(id << 1 | 1 , mid + 1 , r , x); } }; void solve(){ vector<int64_t> pref(n + 5) , f(n + 5); for ( int i = 1 ; i <= n ; i++ ){ pref[i] = pref[i - 1] + g[i]; f[i] = f[i - 1] + d[i]; } Segtri it(n); for ( int i = 1 ; i <= n ; i++ ) it.update(1 , 1 , n , i , f[i] - x[i] - d[i]); int64_t ans = 0; for ( int i = 1 ; i <= n ; i++ ){ int pos = it.walk(1 , 1 , n , f[i] - x[i]); ans = max(ans , pref[i] - pref[pos - 1]); } cout << ans; } } int32_t main (){ // freopen("divide.inp" , "r" , stdin); // freopen("divide.out" , "w" , stdout); ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for ( int i = 1 ; i <= n ; i++ ) cin >> x[i] >> g[i] >> d[i]; if (n <= 5000){ sub_trau::solve(); return 0; } sub_final::solve(); }

Compilation message (stderr)

divide.cpp: In member function 'void sub_final::Segtri::update(int, int, int, int, int64_t)':
divide.cpp:41:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   41 |       int mid = l + r >> 1;
      |                 ~~^~~
divide.cpp: In member function 'int sub_final::Segtri::walk(int, int, int, int64_t)':
divide.cpp:49:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   49 |       int mid = l + r >> 1;
      |                 ~~^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...