Submission #952590

# Submission time Handle Problem Language Result Execution time Memory
952590 2024-03-24T10:15:29 Z nguyennh Divide and conquer (IZhO14_divide) C++14
100 / 100
41 ms 8636 KB
#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

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 time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 2 ms 344 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 2 ms 348 KB Output is correct
22 Correct 3 ms 528 KB Output is correct
23 Correct 15 ms 604 KB Output is correct
24 Correct 17 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 2 ms 344 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 2 ms 348 KB Output is correct
22 Correct 3 ms 528 KB Output is correct
23 Correct 15 ms 604 KB Output is correct
24 Correct 17 ms 604 KB Output is correct
25 Correct 14 ms 604 KB Output is correct
26 Correct 4 ms 856 KB Output is correct
27 Correct 3 ms 940 KB Output is correct
28 Correct 15 ms 3420 KB Output is correct
29 Correct 17 ms 3416 KB Output is correct
30 Correct 41 ms 6304 KB Output is correct
31 Correct 29 ms 6232 KB Output is correct
32 Correct 29 ms 6232 KB Output is correct
33 Correct 28 ms 6236 KB Output is correct
34 Correct 34 ms 6224 KB Output is correct
35 Correct 34 ms 8284 KB Output is correct
36 Correct 41 ms 8636 KB Output is correct