class Solution {
public:
int trap(vector<int>& height) {
int res = 0;
stack<int> st;
int n = height.size();
for (int i = 0; i < n; i ++ ) {
while (!st.empty() && height[i] >= height[st.top()]) {
int bottom_h = height[st.top()];
st.pop();
if (st.size() == 0) break;
int left = st.top();
int h = min(height[left], height[i]) - bottom_h;//高
res += h * (i - left - 1);//高 * 宽
}
st.push(i);
}
return res;
}
};