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...);
}
bool cmp_ff(pii XXX, pii YYY){
return (XXX.ff != YYY.ff) ? (XXX.ff < YYY.ff) : (XXX.ss < YYY.ss);
}
bool cmp_ss(pii XXX, pii YYY){
return (XXX.ss != YYY.ss) ? (XXX.ss < YYY.ss) : (XXX.ff < YYY.ff);
}
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;
}
db(ans+w);
return 0;
}
/*
1 5
2 4 7 1 5
*/
Compilation message (stderr)
Main.cpp: In function 'std::ostream& operator<<(std::ostream&, std::pair<int, int>)':
Main.cpp:13:1: warning: no return statement in function returning non-void [-Wreturn-type]
13 | }
| ^
# | 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... |