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

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

typedef unsigned long long ull;
const ull MOD = 1000000007;
const int MAX_N = 100000;
ull fact[MAX_N + 1];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  vector<ull> ans;

  fact[0] = 0;
  fact[1] = 1;
  for (int i = 2; i < MAX_N + 1; i++) {
    fact[i] = (fact[i - 1] * i) % MOD;
  }

  while (1) {
    int N, R;
    ull num = 0;

    cin >> N;
    if (N == 0)
      break;

    ans.push_back(0);
    vector<int> word(N);
    for (int i = 0; i < N; i++)
      word[i] = i;
    word.push_back(0);

    cin >> R;

    vector<int> s(2 * R + 1, 0);

    for (int i = 0; i < R; i++) {
      cin >> s[i] >> s[i + R];
      s[i]--;
      s[i + R]--;

      word[s[i]] ^= word[s[i + R]];
      word[s[i + R]] ^= word[s[i]];
      word[s[i]] ^= word[s[i + R]];
    }

    sort(s.begin(), s.end());
    s.erase(unique(s.begin(), s.end()), s.end());
    s.push_back(N);

    for (int i = 0, R = s.size() - 1; i < R; i++) {
      // 先頭の計算
      ull tmp = fact[N - s[i] - 1], tmp2 = 0;
      tmp *= word[s[i]];
      tmp = tmp % MOD;

      // 値を詰める
      for (int j = i; j < R; j++) {
        // 先頭
        word[s[j]] -= (word[s[j]] > word[s[i]] ? 1 : 0);
        // 後続
        if (s[j] + 1 != s[j + 1]) {
          word[s[j] + 1] -= (word[s[j] + 1] > word[s[i]] ? 1 : 0);
        }
      }

      // 後続の計算
      for (int j = s[i] + 1; j < s[i + 1]; j++) {
        tmp2 += fact[N - j - 1];
        //				cout << j << "("<< tmp2 << "),";
        tmp2 = tmp2 % MOD;
      }
      // 値を詰める
      for (int j = i + 1; j < R; j++) {
        // 先頭
        word[s[j]] -= (word[s[j]] > word[s[i] + 1] ? s[i + 1] - s[i] - 1 : 0);
        // 後続
        if (s[j] + 1 != s[j + 1]) {
          word[s[j] + 1] -=
              (word[s[j] + 1] > word[s[i] + 1] ? s[i + 1] - s[i] - 1 : 0);
        }
      }

      tmp2 *= word[s[i] + 1];
      //			cout << tmp << "," << tmp2 << ",";
      tmp += tmp2;
      tmp += *(ans.end() - 1); // end()は最後の要素の次の要素のiteratorを返す
      *(ans.end() - 1) = tmp % MOD;
      //			cout << *(ans.end() - 1) << endl;
    }
  }

  for (int i = 0, l = ans.size(); i < l; i++) {
    cout << ans[i] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

typedef unsigned long long ull;
const ull MOD = 1000000007;
const int MAX_N = 100000;
ull fact[MAX_N];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  vector<ull> ans;

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

  while (1) {
    int N, R, s, t;
    ull num = 0;

    cin >> N;
    if (N == 0)
      break;

    ans.push_back(0);
    vector<int> word(N);
    for (int i = 0; i < N; i++)
      word[i] = i;

    cin >> R;

    for (int i = 0; i < R; i++) {
      cin >> s >> t;
      s--;
      t--;

      word[s] ^= word[t];
      word[t] ^= word[s];
      word[s] ^= word[t];
    }
    //		cout << "intput end" << endl;

    for (int i = 0; i < N - 1; i++) {
      ull tmp = fact[N - i - 1];
      tmp *= word[i];
      tmp += *(ans.end() - 1);
      *(ans.end() - 1) = tmp % MOD;
      for (int j = i + 1; j < N; j++)
        word[j] -= (word[j] > word[i] ? 1 : 0);
    }
    //		cout << "calc end" << endl;
  }

  for (int i = 0, l = ans.size(); i < l; i++) {
    cout << ans[i] << endl;
  }

  return 0;
}