Problem p00266 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

typedef struct {
  int original_order;
  int instant_order;
} alphabet_t;

inline void interchangeAlphabet(alphabet_t *a, alphabet_t *b) {
  alphabet_t temp = *a;
  *a = *b;
  *b = temp;
}

int main() {
  const long long MOD = 1000000007;
  int N, R, sr, tr;
  alphabet_t *alphabet = new alphabet_t[100000];
  long long *ans = new long long[100];
  long long *factorial = new long long[100000];
  int set_number;

  // 0から10万-1の階乗を10億7で割った余りを計算
  factorial[0] = 1;
  for (int n = 1; n < 100000; n++) {
    factorial[n] = n * factorial[n - 1] % MOD;
  }

  // 各デ−タセットの入力および答えを計算
  for (set_number = 0; set_number < 100; set_number++) {
    // 答えを初期化
    ans[set_number] = 0;

    // 文字の数を入力
    cin >> N;
    if (N == 0)
      break;

    // 単語を初期化
    for (int i = 1; i <= N; i++) {
      int index = i - 1;
      alphabet[index].original_order = i;
      alphabet[index].instant_order = 0;
    }

    // 文字の入れ替え回数を入力
    cin >> R;

    for (int r = 0; r < R; r++) {
      // 入れ替える文字の位置を入力
      cin >> sr >> tr;

      // 文字の位置を入れ替える
      for (int i = sr; i < tr; i++) {
        int index = i - 1;
        if (alphabet[index].original_order < alphabet[index + 1].original_order)
          alphabet[index + 1].instant_order++;
        else
          alphabet[index].instant_order--;

        interchangeAlphabet(&alphabet[index], &alphabet[index + 1]);
      }

      for (int i = tr - 1; i > sr; i--) {
        int index = i - 1;
        if (alphabet[index].original_order < alphabet[index - 1].original_order)
          alphabet[index - 1].instant_order--;
        else
          alphabet[index].instant_order++;

        interchangeAlphabet(&alphabet[index], &alphabet[index - 1]);
      }
    }

    for (int i = 1; i <= N; i++) {
      int index = i - 1;
      ans[set_number] += factorial[N - i] * alphabet[index].instant_order;
      ans[set_number] %= MOD;
    }
  }

  for (int s = 0; s < set_number; s++)
    cout << ans[s] << endl;

  delete[] ans;
  delete[] alphabet;
  delete[] factorial;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  const long long MOD = 1000000007;
  int N, R, sr, tr;
  int *word = new int[100000];
  long long *ans = new long long[100];
  int temp;
  long long *factorial = new long long[100000];
  int set_number;

  factorial[0] = 1;
  for (int i = 1; i < 100000; i++) {
    factorial[i] = i * factorial[i - 1] % MOD;
  }

  for (set_number = 0; set_number < 100; set_number++) {
    // 答えを初期化
    ans[set_number] = 0;

    // 文字の数を入力
    cin >> N;
    if (N == 0)
      break;

    // 単語を初期化
    for (int i = 0; i < N; i++) {
      word[i] = i;
    }

    // 文字の入れ替え回数を入力
    cin >> R;

    for (int r = 0; r < R; r++) {
      // 入れ替える文字の位置を入力
      cin >> sr >> tr;

      // 文字の位置を入れ替える
      temp = word[sr - 1];
      word[sr - 1] = word[tr - 1];
      word[tr - 1] = temp;
    }

    for (int i = 0; i < N; i++) {
      ans[set_number] += factorial[N - 1 - i] * word[i];
      ans[set_number] %= MOD;
      // ランクの更新
      for (int j = i + 1; j < N; j++) {
        if (word[j] > word[i])
          word[j]--;
      }
    }
  }

  for (int s = 0; s < set_number; s++)
    cout << ans[s] << endl;

  delete[] ans;
  delete[] word;
  delete[] factorial;

  return 0;
}