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

// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

// conversion
//------------------------------------------
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// math
//-------------------------------------------
template <class T> inline T sqr(T x) { return x * x; }

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<long, long> PLL;
typedef long long LL;
// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);

// clear memory
#define CLR(a) memset((a), 0, sizeof(a))
#define INF 1000000000
#define MAX_N 100003
#define MAX_V 100003
#define MOD 1000000007
struct edge {
  int to, cost;
};
typedef pair<int, int> P; // firstは最短距離,secondは頂点番号

long bit[MAX_N], n;

LL sum(long i) {
  LL s = 0;
  while (i > 0) {
    s += bit[i];
    i -= i & -i; // iの最後の1のビットはi&-iで表せる
  }
  return s;
}

int add(long i, int x) {
  while (i <= n) {
    bit[i] += x;
    i += i & -i;
  }
  return 0;
}

LL dp[MAX_N + 1];
int main() {
  long N, R;
  dp[0] = 1;
  for (LL i = 1; i <= MAX_N; i++)
    dp[i] = dp[i - 1] * i % MOD;
  while (cin >> N) {
    if (N == 0)
      break;
    cin >> R;
    n = N;
    long a[MAX_N];
    REP(i, N) {
      a[i] = i + 1;
      bit[i] = 0;
    }
    bit[N] = 0;
    REP(i, R) {
      long s, t;
      cin >> s >> t;
      s--;
      t--;
      swap(a[s], a[t]);
    }
    LL res = 0;
    for (long i = N - 1, j = 0; i >= 0; i--, j++) {
      res += (sum(a[i]) * dp[j]);
      add(a[i], 1);
      res %= MOD;
      // cout<<kai<<" "<<sum(a[i])<<endl;
    }
    cout << res << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

// conversion
//------------------------------------------
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// math
//-------------------------------------------
template <class T> inline T sqr(T x) { return x * x; }

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<long, long> PLL;
typedef long long LL;
// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);

// clear memory
#define CLR(a) memset((a), 0, sizeof(a))
#define INF 1000000000
#define MAX_N 100003
#define MAX_V 100003
#define MOD 1000000007
struct edge {
  int to, cost;
};
typedef pair<int, int> P; // firstは最短距離,secondは頂点番号

int bit[MAX_N + 1], n;

LL sum(int i) {
  int s = 0;
  while (i > 0) {
    s += bit[i];
    i -= i & -i; // iの最後の1のビットはi&-iで表せる
  }
  return s;
}

int add(int i, int x) {
  while (i <= n) {
    bit[i] += x;
    i += i & -i;
  }
  return 0;
}

LL kai(int t) {
  if (t == 0)
    return 1;
  LL res = 1;
  for (int i = 1; i <= t; i++)
    res *= i;
  return res;
}

int main() {
  int N, R;

  while (cin >> N) {
    if (N == 0)
      return 0;
    cin >> R;
    n = N;
    CLR(bit);
    vector<int> a;
    REP(i, N) a.PB(i + 1);
    REP(i, R) {
      int s, t;
      cin >> s >> t;
      s--;
      t--;
      swap(a[s], a[t]);
    }
    LL res = 0;
    for (int i = SZ(a) - 1; i >= 0; i--) {
      res += sum(a[i]) * kai(SZ(a) - 1 - i) % MOD;
      add(a[i], 1);
    }
    cout << res << endl;
  }
  return 0;
}