Submission #685435

#TimeUsernameProblemLanguageResultExecution timeMemory
685435OrazBBigger segments (IZhO19_segments)C++14
13 / 100
1 ms344 KiB
#include <bits/stdc++.h> #define N 100005 #define wr cout << "Continue debugging\n"; #define all(x) (x).begin(), (x).end() #define ll long long int #define pii pair <int, int> #define pb push_back #define ff first #define ss second using namespace std; int a[N], mx, n, ind[N], t[4*N]; vector<int> vec; ll pref[N]; ll dp[N][5]; int calc(int x){ int l = 0, r = vec.size()-1, pos = 0; while(l <= r){ int md = (l+r)/2; if (vec[md] > x) r = md - 1; else{ pos = vec[md]; l = md + 1; } } return pos; } int binary(int i, int x){ int l = i+1, r = n, idx=i; while(l <= r){ int md = (l+r)/2; if (pref[md]-pref[i] >= x) r = md - 1; else{ idx = md; l = md + 1; } } return idx+1; } // void upd(int x, int u, int v, int l, int r, int idx){ // if (lazy[idx]){ // t[idx] = lazy[idx]; // // if (l == 3 and r == 3 and x == 2) wr; // if (l != r){ // lazy[idx*2] = lazy[idx]; // lazy[idx*2+1] = lazy[idx]; // } // lazy[idx] = 0; // } // if (l > r or l > v or r < u) return; // // if (l == 2 and r == 2) wr // if (u <= l and r <= v){ // // cout << l << " " << r; // t[idx] = x; // if (l != r){ // lazy[idx*2] = x; // lazy[idx*2+1] = x; // } // return; // } // int md = (l+r)/2; // upd(x, u, v, l, md, idx*2); // upd(x, u, v, md+1, r, idx*2+1); // // if (l != r) // t[idx] = max(t[idx*2], t[idx*2+1]); // } // int range(int pos, int l, int r, int idx){ // if (lazy[idx]){ // t[idx] = lazy[idx]; // if (l != r){ // lazy[idx*2] = lazy[idx]; // lazy[idx*2+1] = lazy[idx]; // } // lazy[idx]=0; // } // int md = (l+r)/2; // if (l == r) return t[idx]; // if (md >= pos) range(pos, l, md, idx*2); // else range(pos, md+1, r, idx*2+1); // // t[idx] = max(t[idx*2], t[idx*2+1]); // } int main () { ios::sync_with_stdio(false); cin.tie(0); cin >> n; for (int i = 1; i <= n; i++){ cin >> a[i]; pref[i] = pref[i-1]+a[i]; } dp[1][1] = 1; dp[1][2] = a[1]; int pos = binary(1, a[1]); if (pos <= n){ ind[pos] = 1; vec.pb(pos); } // cout << vec[0]; for (int i = 2; i <= n; i++){ int pos = ind[calc(i)]; // cout << pos; if (!pos){ dp[i][1] = 1; dp[i][2] = pref[i]; }else{ dp[i][1] = dp[pos][1]+1; dp[i][2] = pref[i]-pref[pos]; } pos = binary(i, dp[i][2]); if (pos > n) continue; ind[pos] = i; while(vec.size() and vec.back() >= pos){ vec.pop_back(); } vec.pb(pos); } cout << dp[n][1]; }
#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...