Submission #952579

# Submission time Handle Problem Language Result Execution time Memory
952579 2024-03-24T09:59:22 Z nguyennh Divide and conquer (IZhO14_divide) C++14
48 / 100
41 ms 9224 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 , int 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 (st[id] > x) return -1;
      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);
    it.update(1 , 0 , n , 0 , 0);
    for ( int i = 1 ; i <= n ; i++ ) it.update(1 , 0 , n , i , f[i - 1] - x[i]);
    int64_t ans = 0;
    for ( int i = 1 ; i <= n ; i++ ){
      int pos = it.walk(1 , 0 , n , f[i] - x[i]);
      assert(pos != -1);
      if (pos != i) ans = max(ans , pref[i] - pref[pos]);
      else 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, int)':
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:50:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   50 |       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 0 ms 348 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 428 KB Output is correct
12 Correct 0 ms 348 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 0 ms 348 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 428 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 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 604 KB Output is correct
18 Correct 2 ms 484 KB Output is correct
19 Correct 2 ms 496 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 3 ms 480 KB Output is correct
22 Correct 3 ms 348 KB Output is correct
23 Correct 16 ms 604 KB Output is correct
24 Correct 14 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 0 ms 348 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 428 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 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 604 KB Output is correct
18 Correct 2 ms 484 KB Output is correct
19 Correct 2 ms 496 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 3 ms 480 KB Output is correct
22 Correct 3 ms 348 KB Output is correct
23 Correct 16 ms 604 KB Output is correct
24 Correct 14 ms 604 KB Output is correct
25 Correct 16 ms 604 KB Output is correct
26 Correct 3 ms 952 KB Output is correct
27 Correct 4 ms 1116 KB Output is correct
28 Correct 18 ms 4188 KB Output is correct
29 Correct 18 ms 4504 KB Output is correct
30 Correct 41 ms 9224 KB Output is correct
31 Correct 32 ms 8024 KB Output is correct
32 Correct 34 ms 8124 KB Output is correct
33 Correct 32 ms 7764 KB Output is correct
34 Incorrect 32 ms 7780 KB Output isn't correct
35 Halted 0 ms 0 KB -