제출 #803033

#제출 시각아이디문제언어결과실행 시간메모리
803033farhan132Bigger segments (IZhO19_segments)C++17
0 / 100
7 ms12104 KiB
/***Farhan132***/
// #pragma GCC optimize("Ofast,fast-math")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
// #pragma GCC optimization ("unroll-loops")

#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
typedef double dd;
typedef pair<ll , ll> ii;
typedef tuple < ll,  ll, ll > tp;
 
#define ff first
#define ss second
#define pb push_back
#define in insert
#define bug printf("**!\n")
#define mem(a , b) memset(a, b ,sizeof(a))
#define front_zero(n) __builtin_clz(n)
#define back_zero(n) __builtin_ctzll(n)
#define total_one(n) __builtin_popcount(n)
#define clean fflush(stdout)
 
const ll mod =  (ll) 998244353;
// const ll mod =  (ll) 1e9 + 7;
//const ll INF = numeric_limits<ll>::max()-1;
const ll INF = (ll)2e18;
 
// ll dx[]={0,1,0,-1};
// ll dy[]={1,0,-1,0};
// ll dxx[]={0,1,0,-1,1,1,-1,-1};
// ll dyy[]={1,0,-1,0,1,-1,1,-1};
 
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
 
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
 
template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

const ll N = 5e5 + 5;

ll dp[N], ans[2][N], pref[N], a[N];

vector < ll > v[N];

ll find(ll c, ll i, int id){
    if(c < 0) return -1;
    ll l = v[c][0]; ll r = v[c].back();
    ll res = -1;
    while(l <= r){
        ll m = (l + r)/2;
        if(ans[id][m] <= pref[i] - pref[m]){
            res = m;
            l = m + 1;
        }else{
            r = m - 1;
        }
    }
    return res;
}

void solve(){

    pref[0] = 0; dp[0] = 0;

    ll n; cin >> n;

    for(ll i = 1; i <= n; i++){
        cin >> a[i];
        pref[i] = pref[i - 1] + a[i]; 
    }

    v[0].pb(0);
    ans[0][0] = 0, ans[0][1] = INF;

    ll cur = 0;

    for(ll i = 1; i <= n; i++){
        ll j = find(cur, i, 0);
        if(j != -1){
            cur++;
            ans[0][i] = pref[i] - pref[j];
            j = find(cur-1, i, 1);
            if(j != -1) ans[1][i] = pref[i] - pref[j];
            else ans[1][i] = INF;
        }else{
            j = find(cur, i, 0);
            ans[0][i] = pref[i] - pref[j];
            j = max(find(cur-1, i, 1), find(cur-2, i, 1));
            if(j != -1) ans[1][i] = pref[i] - pref[j];
            else ans[1][i] = INF;
        }
        v[cur].pb(i);
    }
    cout << cur << '\n';
    
   return;
}

int main() {

    #ifdef LOCAL
        freopen("in.txt", "r", stdin);
        freopen("out.txt", "w", stdout);
        auto start_time = clock();
    #else
         // freopen("subsequence.in", "r", stdin);
         // freopen("subsequence.out", "w", stdout); 
         ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
    #endif

    //precalc();
 
    ll T = 1, CT = 0;// cin >> T; 
 
    while(T--){
        // cout << "Case #" << ++CT << ": ";
        solve();
    }
    
    #ifdef LOCAL
        auto end_time = clock();
        cerr<< "Execution time: "<<(end_time - start_time)*(int)1e3/CLOCKS_PER_SEC<<" ms\n";
    #endif
 
    return 0;
} 

컴파일 시 표준 에러 (stderr) 메시지

segments.cpp: In function 'int main()':
segments.cpp:118:15: warning: unused variable 'CT' [-Wunused-variable]
  118 |     ll T = 1, CT = 0;// cin >> T;
      |               ^~
segments.cpp: In function 'void solve()':
segments.cpp:92:41: warning: array subscript -1 is below array bounds of 'll [500005]' {aka 'long long int [500005]'} [-Warray-bounds]
   92 |             ans[0][i] = pref[i] - pref[j];
      |                                   ~~~~~~^
segments.cpp:46:22: note: while referencing 'pref'
   46 | ll dp[N], ans[2][N], pref[N], a[N];
      |                      ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...