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

#include <iostream>
#include <map>
#include <vector>
using namespace std;

const int divisor = 1000000007;

int main() {
  vector<int> ansvec;

  // 100000!までの数をdivisorで割った余りを格納しておく
  long long *factmodary = new long long[100001];
  long long fact = 1;
  for (int i = 1; i <= 100000; i++) {
    fact *= i;
    fact %= divisor;
    factmodary[i] = fact;
  }

  for (;;) {
    int N, R;
    cin >> N;
    if (N == 0)
      break;
    cin >> R;

    int *word = new int[N];
    for (int i = 0; i < N; i++)
      word[i] = i + 1; // まず0番目の単語を入れておく

    for (int i = 0; i < R; i++) {
      int s, t;
      cin >> s >> t;
      /* swap */
      int tmp = word[s - 1];
      word[s - 1] = word[t - 1];
      word[t - 1] = tmp;
    }

    long long ans = 0;
    map<int, int> bubblehist;

    for (int i = 0; i < N; i++) {
      int facttime = word[i] - 1;
      int bubblenum;
      map<int, int>::iterator it = bubblehist.find(word[i] - 1);

      if (it != bubblehist.end()) {
        bubblenum = it->second + 1;
      } else {
        it = bubblehist.find(word[i] + 1);
        if (it != bubblehist.end()) {
          bubblenum = it->second;
        } else {
          bubblenum = 0;
          for (int j = 0; j < i; j++) {
            if (word[j] < word[i])
              bubblenum++;
          }
        }
      }
      pair<int, int> p;
      p.first = word[i];
      p.second = bubblenum;
      bubblehist.insert(it, p);
      for (map<int, int>::iterator it2 = bubblehist.upper_bound(word[i]);
           it2 != bubblehist.end(); it2++)
        ++(it2->second);

      facttime -= bubblenum;
      ans += factmodary[N - (i + 1)] * facttime;
      ans %= divisor;
    }

    delete[] word;
    ansvec.push_back((int)ans);
  }

  for (vector<int>::iterator it = ansvec.begin(); it != ansvec.end(); it++) {
    cout << *it << endl;
  }

  delete[] factmodary;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <vector>
using namespace std;

const int divisor = 1000000007;

int main() {
  vector<int> ansvec;

  // 100000!までの数をdivisorで割った余りを格納しておく
  int *factmodary = new int[100001];
  long long fact = 1;
  for (int i = 1; i <= 100000; i++) {
    fact *= i;
    fact %= divisor;
    factmodary[i] = (int)fact;
  }

  for (;;) {
    int N, R;
    cin >> N;
    if (N == 0)
      break;
    cin >> R;

    int *word = new int[N];
    for (int i = 0; i < N; i++)
      word[i] = i + 1; // まず0番目の単語を入れておく

    for (int i = 0; i < R; i++) {
      int s, t;
      cin >> s >> t;
      /* swap */
      int tmp = word[s - 1];
      word[s - 1] = word[t - 1];
      word[t - 1] = tmp;
    }

    long long ans = 0;
    map<int, int> bubblehist;

    for (int i = 0; i < N; i++) {
      int facttime = word[i] - (i + 1);
      int bubblenum;
      map<int, int>::iterator it = bubblehist.find(word[i] + 1);

      if (it != bubblehist.end())
        bubblenum = it->second + 1;
      else {
        it = bubblehist.find(word[i] - 1);
        if (it != bubblehist.end()) {
          bubblenum = it->second;
        } else {
          bubblenum = 0;
          for (int j = 0; j < i; j++) {
            if (word[j] > word[i])
              bubblenum++;
          }
        }
      }
      pair<int, int> p;
      p.first = word[i];
      p.second = bubblenum;
      bubblehist.insert(it, p);
      for (map<int, int>::iterator it2 = bubblehist.begin();
           it2 != bubblehist.lower_bound(word[i]); it2++)
        it2->second++;

      facttime += bubblenum;
      ans += factmodary[N - (i + 1)] * facttime;
      ans %= divisor;
    }

    delete[] word;
    ansvec.push_back(ans % divisor);
  }

  for (vector<int>::iterator it = ansvec.begin(); it != ansvec.end(); it++) {
    cout << *it << "\\n";
  }

  delete[] factmodary;
  return 0;
}