12800 扶桑号战列舰

用栈维护左端点。从左向右遍历,
如果a[i] > a[i+1],则栈顶为左端点,i为右端点,输出并出栈。如果a[i-1] < a[i],则将i 入栈 。

#include <cstring>
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
const ll maxn = 1e5+10;
int a[maxn];
int main() {
#ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
#endif
    int n; scanf("%d",&n);
    ll sum = 0;
    for (int i=1;i<=n;i++) {
        scanf("%d",&a[i]);
        if (a[i]>a[i-1]) sum += a[i]-a[i-1];
    }
    printf("%lld\n",sum);
    stack<int> q;
    for (int i=1;i<=n;i++) {
        if (a[i]>a[i-1]) {
            for (int j=a[i];j>a[i-1];j--) q.push(i);
        }
        if (a[i]>a[i+1]) {
            for (int j=a[i];j>a[i+1]&&!q.empty();j--) {
                printf("%d %d\n",q.top(),i);
                q.pop();
            }
        }
    }
    return 0;
}