This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define pii pair<int,int>
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define STL(i, x) for(auto &i:x) cout << i << " "; cout << "\n";
using namespace std;
/*ostream& operator<<(ostream &os, pii XXX){
cout << "[" << XXX.ff << ", " << XXX.ss << "]";
}
void db(){
cout << "\n";
}
template<class T, class ...U>
void db(T XXX, U ...YYY){
cout << XXX; db(YYY...);
}*/
int main(){
int h, w;
cin >> h >> w;
int a[h+10];
stack<int> S1, S2;
long long ans = 0;
loop(i,0,w){
cin >> a[i];
while(true){
if(S1.empty()) break;
if(S1.top() > a[i]){
while(!S1.empty())
S1.pop();
}
else break;
}
S1.push(a[i]);
ans += S1.size() - 1;
while(true){
if(S2.empty()) break;
if(S2.top() < a[i]){
while(!S2.empty())
S2.pop();
}
else break;
}
S2.push(a[i]);
ans += S2.size() - 1;
}
cout << ans+w << "\n";
return 0;
}
/*
1 5
2 4 7 1 5
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |