#include <cstdio>
#include <stdio.h>
#include <stdbool.h>
#include <iostream>
#include <map>
#include <vector>
#include <climits>
#include <stack>
#include <string>
#include <queue>
#include <algorithm>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <cmath>
#include <cctype>
#include <bitset>
#include <iomanip>
#include <cstring>
#include <numeric>
#include <cassert>
#include <random>
#include <chrono>
#include <fstream>
using namespace std;
#define int long long
#define pii pair<int, int>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
struct node{
int s, e, m, val;
node *l, *r;
node(int S, int E){
s = S, e = E, m = (s+e)/2;
val=0;
if (s!=e){
l = new node(s, m);
r = new node(m+1, e);
}
}
void up(int ind, int nv){
if (s==e)val=nv;
else{
if (ind<=m)l->up(ind, nv);
else r->up(ind, nv);
val = min(l->val, r->val);
}
}
int query(int left, int right){
if (s==left && e==right)return val;
if (right<=m)return l->query(left, right);
else if (left>m)return r->query(left, right);
else return min(l->query(left, m), r->query(m+1, right));
}
}*st;
vector<int> vect;
int dnc(int l, int r, int h){
if (l>r)return 0;
if (l==r)return (h!=vect[l]);
int mn=st->query(l, r);
for (int i=l, j=r; i<=j; ++i, --j){
if (vect[i]==mn)return dnc(l, i-1, mn)+dnc(i+1, r, mn)+(mn!=h);
if (vect[j]==mn)return dnc(l, j-1, mn)+dnc(j+1, r, mn)+(mn!=h);
}
}
int32_t main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin>>n;
vect.resize(n);
st = new node(0, n-1);
for (int i=0; i<n; ++i)cin>>vect[i], st->up(i, vect[i]);
cout<<dnc(0, n-1, 0);
}
Compilation message (stderr)
Main.cpp: In function 'long long int dnc(long long int, long long int, long long int)':
Main.cpp:72:1: warning: control reaches end of non-void function [-Wreturn-type]
72 | }
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |