1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <iostream>
  #include <stdio.h>
  #include <cstdio>
  using namespace std;



  class Plane {
    public:
    int left;
    int right;
    int wing;
  };

  long long triangle(long long side) {
    return side <= 0
        ? 0
        : (side + 1) * side / 2;
  }

  long long surface(Plane plane) {
    if (plane.left < 0) {
      return 0;
    }
    long long result = 0;

    result += triangle(plane.wing + 1);
    result -= triangle(plane.wing - plane.left);
    result -= triangle(plane.wing - plane.right);
    // was removed twice
    result += triangle(plane.wing - 1 - plane.left - plane.right);
    return result;
  }

  Plane overlap(Plane first, Plane second) {
    int distance = first.left - second.left + first.right - second.right;
    int newWing = first.wing - distance;
    if (newWing >= 0) {
      Plane plane;
      plane.left = second.left;
      plane.right = second.right;
      plane.wing = newWing;
      return plane;
    } else {
      Plane plane;
      plane.left = -1;
      plane.right = -1;
      plane.wing = 0;
      return plane;
    }
  }

  bool ordered(int a, int b, int c) {
    return a <= b && b <= c;
  }

  int main() {
    int n;
    cin >> n;

    int numberAt[n + 1];
    int positionOf[n + 1];
    for (int i = 1; i <= n; i++) {
      int number;
      cin >> number;
      numberAt[i] = number;
      positionOf[number] = i;
    }

    int nLoggedPlanes = 0;
    Plane loggedPlanes[n + 1];

    {
      int plane = n;
      int left = positionOf[plane];
      int right = positionOf[plane];

      while (true) {
        // correct position
        int positionOfPlane = positionOf[plane];
        if (positionOfPlane < left) {
          left = positionOfPlane;
        }
        if (right < positionOfPlane) {
          right = positionOfPlane;
        }

        int allNumbers = right - left + 1;
        int highNumbers = n - plane + 1;
        int lowNumbers = allNumbers - highNumbers;
        int wing = highNumbers - 1 - lowNumbers;

        // skip illegal planes
        if (wing < 0) {
          if (plane > 1) {
            plane--;
            continue;
          } else {
            break;
          }
        }

        Plane logPlane;
        logPlane.left = left - 1;
        logPlane.right = n - right;
        logPlane.wing = wing;
        loggedPlanes[nLoggedPlanes] = logPlane;
        nLoggedPlanes++;

        if (left == 1 && right == n) {
          break;
        }
        plane--;
      }
    }

    // calculate area
    long long result = 0;
    for (int i = 0; i < nLoggedPlanes; i++) {
      result += surface(loggedPlanes[i]);
    }
    for (int i = 0; i < nLoggedPlanes - 1; i++) {
      Plane plane = overlap(loggedPlanes[i], loggedPlanes[i + 1]);
      result -= surface(plane);
    }

    printf("%d %ld", 2 * n + 1, result);

    return 0;
  }