#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[50050];
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();
break;
}
else break;
}
S1.push(a[i]);
ans += (int)(S1.size()) - 1;
while(true){
if(S2.empty()) break;
if(S2.top() < a[i]){
while(!S2.empty())
S2.pop();
break;
}
else break;
}
S2.push(a[i]);
ans += (int)(S2.size()) - 1;
}
cout << ans+w << "\n";
return 0;
}
/*
1 5
2 4 7 1 5
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
468 KB |
Output is correct |
2 |
Correct |
16 ms |
1028 KB |
Output is correct |
3 |
Correct |
16 ms |
868 KB |
Output is correct |
4 |
Correct |
16 ms |
964 KB |
Output is correct |
5 |
Correct |
16 ms |
852 KB |
Output is correct |
6 |
Correct |
20 ms |
852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |