alexjercan/bugnet huggingface dataset:
Problem p00265 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
typedef long long int ll;
#define FOR(i, a, n) for (int i = (ll)a; i < (ll)n; ++i)
#define TFOR(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end();
const int INF = 1LL << 30;
const ll LLINF = 1LL << 60;
int main() {
  int n, q;
  std::cin >> n >> q;
  int max = 0;
  std::bitset<600000> bit;
  TFOR(i, n) {
    int nn;
    std::cin >> nn;
    bit[nn - 1] = 1;
    if (max < nn)
      max = nn;
  }
  TFOR(i, q) {
    int nn;
    std::cin >> nn;
    bool fl = 0;
    for (int j = nn - 1; j >= 0; --j) {
      for (int k = max / nn * nn + j; k > 0; k -= nn) {
        if (bit[k - 1] == 1) {
          std::cout << j << std::endl;
          fl = 1;
        }
        if (fl == 1)
          break;
      }
      if (fl == 1) {
        fl = 0;
        break;
      }
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
typedef long long int ll;
#define FOR(i, a, n) for (int i = (ll)a; i < (ll)n; ++i)
#define TFOR(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end();
const int INF = 1LL << 30;
const ll LLINF = 1LL << 60;
int main() {
  int n, q;
  std::cin >> n >> q;
  int max = 0;
  std::bitset<300000> bit;
  TFOR(i, n) {
    int nn;
    std::cin >> nn;
    bit[nn - 1] = 1;
    if (max < nn)
      max = nn;
  }
  TFOR(i, q) {
    int nn;
    std::cin >> nn;
    bool fl = 0;
    for (int j = nn - 1; j >= 0; --j) {
      for (int k = max / nn * nn + j; k > 0; k -= nn) {
        if (bit[k - 1] == 1) {
          std::cout << j << std::endl;
          fl = 1;
        }
        if (fl == 1)
          break;
      }
      if (fl == 1) {
        fl = 0;
        break;
      }
    }
  }
  return 0;
}
Problem p00265 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#define MAX_N 300000
#define MAX_C 300000
int main() {
  int N, Q;
  scanf("%d %d\\n", &N, &Q);
  int card[MAX_N];
  bool has_card[MAX_C + 1];

  for (int i = 0; i < N; i++) {
    scanf("%d", &card[i]);
    //  printf("card %d ", card[i]);
  }
  for (int i = 0; i <= MAX_C; i++) {
    has_card[i] = false;
  }
  for (int i = 0; i < N; i++) {
    has_card[card[i]] = true;
  }
  //  printf("\\n");
  for (int i = 0; i < Q; i++) {
    int q;
    scanf(" %d", &q);
    //  printf("q = %d\\n", q);
    bool found = false;
    for (int init = q - 1; init >= 0; init--) {
      for (int j = init; j < MAX_C; j += q) {
        if (has_card[j]) {
          printf("%d\\n", init);
          found = true;
          break;
        }
      }
      if (found) {
        break;
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#define MAX_N 300000
int main() {
  int N, Q;
  scanf("%d %d\\n", &N, &Q);
  int card[MAX_N];

  for (int i = 0; i < N; i++) {
    scanf("%d", &card[i]);
    //  printf("card %d ", card[i]);
  }
  //  printf("\\n");
  for (int i = 0; i < Q; i++) {
    int q;
    scanf(" %d", &q);
    //  printf("q = %d\\n", q);
    int max_rest = 0;
    for (int i = 0; i < N; i++) {
      int tmp = card[i] % q;
      //    printf("tmp = %d ? %d = %d\\n", card[i], q, tmp);
      if (tmp == q - 1) {
        max_rest = tmp;
        break;
      }
      if (max_rest < tmp) {
        max_rest = tmp;
      }
    }
    printf("%d\\n", max_rest);
  }
  return 0;
}
Problem p00265 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
int main() {
  int n[300010] = {0};
  int N, Q;
  cin >> N >> Q;
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    n[a] = a;
  }
  for (int i = 1; i < 300010; i++)
    n[i] = max(n[i], n[i - 1]);
  int ans = 0;
  for (int i = 0; i < Q; i++) {
    int q;
    cin >> q;
    for (int j = q; j <= 300010; j += q) {
      ans = max(n[j - 1] % q, ans);
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
  int n, q;
  cin >> n >> q;
  vector<int> T;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    T.push_back(a);
  }
  // sort(T.begin(),T.end());
  for (int i = 0; i < q; i++) {
    int Q;
    cin >> Q;
    int ans = 0;
    for (int j = 0; j < T.size(); j++) {
      if (ans == Q - 1)
        break;
      ans = max(ans, T[j] % Q);
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00265 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

bool c[300001];
int main() {
  int N, Q;
  scanf("%d %d", &N, &Q);

  for (int i = 0; i < N; i++) {
    int a;
    scanf("%d", &a);
    c[a] = true;
  }

  for (int i = 0; i < Q; i++) {
    int q;
    scanf("%d", &q);

    int res;
    int j = q - 1;
    bool f = false;
    while (!f) {
      for (int k = j; k < 300000; k += q) {
        if (c[k]) {
          printf("%d\\n", k % q);
          f = true;
          break;
        }
      }
      j--;
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
using namespace std;

int array[400000];
int q;

bool cmp(const int A, const int B) { return (A % q < B % q); }

int main() {
  int N, Q;

  scanf("%d %d", &N, &Q);
  for (int i = 0; i < N; i++) {
    scanf("%d", &array[i]);
  }

  for (int i = 0; i < Q; i++) {
    scanf("%d", &q);

    sort(array, array + N, cmp);

    printf("%d\\n", array[N - 1] % q);
  }

  return 0;
}
Problem p00265 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int main() {

  int n, m;
  int max_num = 0;
  int next[300001];

  fill(next, next + 300001, 0);

  cin >> n >> m;

  for (int i = 0; i < n; ++i) {
    int c;
    cin >> c;
    next[c + 1] = c;
    max_num = max(max_num, c);
  }

  for (int i = 1; i <= max_num; ++i) {
    if (!next[i]) {
      next[i] = next[i - 1];
    }
  }

  for (int i = 0; i < m; ++i) {

    int q;
    int ans, cur;

    cin >> q;

    ans = max_num % q;
    cur = max_num - (max_num % q);

    while (cur) {

      ans = max(ans, next[cur] % q);
      if (cur - q < 0)
        break;
      cur -= q;
    }

    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {

  int n, m;
  int max_num = 0;
  int next[300001];

  fill(next, next + 300001, 0);

  cin >> n >> m;

  for (int i = 0; i < n; ++i) {
    int c;
    cin >> c;
    next[c + 1] = c;
    max_num = max(max_num, c);
  }

  int last_num = 0;
  for (int i = 1; i <= max_num; ++i) {
    if (!next[i]) {
      next[i] = next[i - 1];
    }
  }

  for (int i = 0; i < m; ++i) {

    int q;
    int ans, cur, div;

    cin >> q;

    ans = max_num % q;
    cur = next[max_num - max_num % q];

    while (cur) {

      ans = max(ans, cur % q);
      cur = next[cur - q];
      // cout << "cur : " << cur << endl;
    }

    cout << ans << endl;
  }
}
Problem p00265 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n, q;
  vector<int> data(300001, 0);
  cin >> n >> q;
  int a;
  int MAX = 0;
  for (int i = 0; i < n; i++) {
    cin >> a;
    MAX = max(MAX, a);
    data[a] = 1;
  }
  for (int i = 0; i < q; i++) {
    cin >> a;
    bool hantei = false;
    for (int j = a - 1; j >= 0; j--) {
      for (int k = 0; a * k + j <= MAX; k++) {
        if (data[a * k + j] == 1) {
          cout << j << endl;
          hantei = true;
          break;
        }
      }
      if (hantei) {
        break;
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n, q;
  cin >> n >> q;
  vector<int> hyou(n);
  for (int i = 0; i < n; i++) {
    cin >> hyou[i];
  }
  int a;
  for (int i = 0; i < q; i++) {
    cin >> a;
    int MAX = hyou[0] % a;
    for (int j = 1; j < n; j++) {
      MAX = max(MAX, hyou[j] % a);
      if (MAX == a - 1) {
        break;
      }
    }
    cout << MAX << endl;
  }
  return 0;
}
Problem p00265 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1e9;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
int main() {
  int n, m;
  cin >> n >> m;
  vi in(n);
  rep(i, n) scanf("%d", &in[i]);
  sort(all(in));
  in.pb(inf);
  while (m--) {
    int a;
    scanf("%d", &a);
    int ma = 0;
    int c = in[0] / a + 1;
    while (1) {
      int t = lower_bound(all(in), c * a) - in.begin();
      if (t == 0)
        continue;
      ma = max(ma, in[t - 1] % a);
      if (t == n)
        break;
      c++;
    }
    printf("%d\\n", ma);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1e9;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
int main() {
  int n, m;
  cin >> n >> m;
  vi in(n);
  rep(i, n) scanf("%d", &in[i]);
  sort(all(in));
  while (m--) {
    int a;
    scanf("%d", &a);
    int ma = 0;
    int c = 1;
    bool h = false;
    while (1) {
      int t = lower_bound(all(in), c * a) - in.begin();
      if (t == 0)
        continue;
      if (h)
        break;
      if (t == n)
        h = true;
      ma = max(ma, in[t - 1] % a);
      c++;
    }
    printf("%d\\n", ma);
  }
}
Problem p00265 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
using namespace std;

#define MAX_N 300000
#define MAX_Q 100000
int N, Q;
bool c[MAX_N + 1] = {false}, found = false;
int q[MAX_Q];
int ub, lb, range;

int main() {
  int input;

  range = 0;
  scanf("%d%d", &N, &Q);
  for (int i = 0; i < N; i++) {
    scanf("%d", &input);
    c[input] = true;
    range = max(range, input);
  }
  for (int i = 0; i < Q; i++)
    scanf("%d", &q[i]);

  for (int i = 0; i < Q; i++) {
    lb = 0;
    ub = q[i];
    int bucket = 0;
    while (ub - lb > 1) {
      found = false;
      int mid = (lb + ub) / 2; // 目安
      for (int p = bucket * q[i] + mid; p <= range; p += mid) {
        for (int j = 0; j < (q[i] - mid); j++) {
          if (c[p]) {
            found = true;
            bucket = p / q[i];
            lb = p % q[i];
            break;
          }
          p++;
        }
        if (found)
          break;
      }
      if (!found) {
        ub = mid;
      }
    }
    printf("%d\\n", lb);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
using namespace std;

#define MAX_N 300000
#define MAX_Q 100000
int N, Q;
bool c[MAX_N + 1] = {false}, found = false;
int q[MAX_Q];
int ub, lb, range;

int main() {
  int input;

  range = 0;
  scanf("%d%d", &N, &Q);
  for (int i = 0; i < N; i++) {
    scanf("%d", &input);
    c[input] = true;
    range = max(range, input);
  }
  for (int i = 0; i < Q; i++)
    scanf("%d", &q[i]);

  for (int i = 0; i < Q; i++) {
    lb = 0;
    ub = q[i];
    int bucket = 0;
    while (ub - lb > 1) {
      found = false;
      int mid = (lb + ub) / 2; // 目安
      for (int p = bucket * q[i] + mid; p <= range; p += mid) {
        for (int j = 0; j < (q[i] - mid); j++) {
          if (c[p]) {
            found = true;
            bucket = p / q[i];
            lb = p % q[i];
            // break;
          }
          p++;
        }
        if (found)
          break;
      }
      if (!found) {
        ub = mid;
      }
    }
    printf("%d\\n", lb);
  }
  return 0;
}
Problem p00265 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, l, n) for (int i = l; i < n; i++)
#define all(a) a.begin(), a.end()
using namespace std;

int main() {
  int N, Q, q;
  cin >> N >> Q;
  vector<int> c(N);
  rep(i, 0, N) cin >> c[i];
  sort(all(c));
  rep(i, 0, Q) {
    cin >> q;
    int res = 0;
    for (int j = 1; (j - 1) * q < 1000000; j++) {
      vector<int>::iterator it;
      it = lower_bound(all(c), j * q);
      if (it == c.begin())
        continue;
      it--;
      res = max(res, (*it) % q);
    }
    cout << res << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, l, n) for (int i = l; i < n; i++)
using namespace std;

int main() {
  int N, Q, q;
  cin >> N >> Q;
  vector<int> c(N);
  rep(i, 0, N) cin >> c[i];
  rep(i, 0, Q) {
    cin >> q;
    int MAX = 0;
    rep(j, 0, N) {
      MAX = max(c[j] % q, MAX);
      if (MAX == q - 1)
        break;
    }
    cout << MAX << endl;
  }
}
Problem p00265 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
// constexpr int MOD = 1000000000 + 7;
constexpr int INF = 2000000000;
constexpr int HINF = INF / 2;
constexpr double DINF = 100000000000000000.0;
constexpr long long LINF = 9223372036854775807;
constexpr long long HLINF = 4500000000000000000;
const double PI = acos(-1);

int c[300300];
int n, q;
int main() {
  cin >> n >> q;
  for (int i = 0; i < n; i++) {
    cin >> c[i];
  }
  sort(c, c + n);
  int a;
  for (int i = 0; i < q; i++) {
    cin >> a;
    int ans = 0;
    for (int j = a; j - a <= c[n - 1]; j += a) {
      ans = max(ans, *(std::lower_bound(c, c + n, j) - 1) % a);
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

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

using namespace std;

int main() {
  int N, Q;
  cin >> N >> Q;
  vector<int> cards;
  for (int i = 0; i < N; ++i) {
    int j;
    std::cin >> j;
    cards.emplace_back(j);
  }
  sort(cards.begin(), cards.end());
  for (int i = 0; i < Q; ++i) {
    int q;
    cin >> q;
    int count = q;
    int A = 0;
    while (count < cards.back() + q) {
      for (int i = 0; i < cards.size(); ++i) {
        A = max(cards[i] % q, A);
      }
      count += q;
    }
    cout << A << endl;
  }
  return 0;
}
Problem p00265 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

#define max(x, y) (x) > (y) ? (x) : (y)

using namespace std;

int main() {
  int N, Q, c[300001] = {0}, m = 0, q;
  cin >> N >> Q;
  for (int i = 0; i < N; i++) {
    int t;
    cin >> t;
    c[t] = 1;
    m = max(m, t);
  }

  for (int i = 0; i < Q; i++) {
    cin >> q;
    if (q > m) {
      cout << m << endl;
      continue;
    }
    bool flag = false;
    for (int j = q - 1; j >= 0; j--) {
      for (int k = 0; k + j <= 300000; k += q) {
        if (c[k + j] == 1 && (k != 0 || j != 0)) {
          flag = true;
          break;
        }
      }
      if (flag) {
        cout << j << endl;
        break;
      }
    }
  }
}

Error: TLE

Fixed submission:


#include <iostream>

#define max(x, y) (x) > (y) ? (x) : (y)

using namespace std;

int main() {
  int N, Q, c[300000], m = 0, q;
  cin >> N >> Q;
  for (int i = 0; i < N; i++) {
    cin >> c[i];
    m = max(m, c[i]);
  }

  for (int i = 0; i < Q; i++) {
    cin >> q;
    if (q > m) {
      cout << m << endl;
      continue;
    }
    int ans = 0;
    bool flag = true;
    for (int j = 0; j < N; j++) {
      if (c[j] % q == q - 1) {
        cout << q - 1 << endl;
        flag = false;
        break;
      }
      ans = max(ans, c[j] % q);
    }
    if (flag) {
      cout << ans << endl;
    }
  }
}
Problem p00266 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const long long B = 1000000007;

bool countOne() {
  int N, R;
  cin >> N;
  if (N == 0)
    return false;
  cin >> R;
  int *counter = new int[N]();
  for (int r = 0; r < R; r++) {
    int s, t;
    cin >> s >> t;
    for (int i = t - 1; i >= s; i--) {
      int a = counter[i - 1];
      int b = counter[i];
      if (a <= b) {
        counter[i] = a;
        counter[i - 1] = b + 1;
      } else {
        counter[i] = a - 1;
        counter[i - 1] = b;
      }
    }
    for (int i = s + 1; i <= t - 1; i++) {
      int a = counter[i - 1];
      int b = counter[i];
      if (a <= b) {
        counter[i] = a;
        counter[i - 1] = b + 1;
      } else {
        counter[i] = a - 1;
        counter[i - 1] = b;
      }
    }
  }

  long long fact = 1, index = 0;
  for (int n = 1; n < N; n++) {
    index += counter[N - n - 1] * fact;
    fact = ((n + 1) * fact) % B;
    index = index % B;
  }
  cout << index << endl;
  delete[] counter;
  return true;
}

int main() {
  while (countOne()) {
  }
  return 0;
}

Error: TLE

Fixed submission:

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

const long long B = 1000000007;

bool countOne() {
  int N, R;
  cin >> N;
  if (N == 0)
    return false;
  cin >> R;

  int *word = new int[N];
  int *counter = new int[N];

  for (int n = 0; n < N; n++)
    word[n] = n;
  for (int r = 0; r < R; r++) {
    int s, t;
    cin >> s >> t;
    swap(word[s - 1], word[t - 1]);
  }

  for (int n = 0; n < N; n++) {
    int wn = word[n];
    counter[n] = std::count_if(word + n, word + N,
                               [wn](int wi) -> bool { return wn > wi; });
  }

  long long fact = 1, index = 0;
  for (int n = 1; n < N; n++) {
    index += counter[N - n - 1] * fact;
    fact = ((n + 1) * fact) % B;
  }
  cout << index << endl;

  delete[] word;
  delete[] counter;
  return true;
}

int main() {
  while (countOne()) {
  }
  return 0;
}
Problem p00266 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)

using namespace std;

typedef long long ll;
const ll MOD = 1000000007LL;
int N;
ll fact[200000];

int bit[200000];
int sum(int i) {
  int s = 0;
  while (i > 0) {
    s += bit[i];
    i -= i & -i;
  }
  return s;
}
void add(int i, int x) {
  while (i < 200000) {
    bit[i] += x;
    i += i & -i;
  }
}

ll compute(vector<int> vec) {
  ll ret = fact[vec.size()];
  set<int> S;
  rep(i, vec.size()) {
    ll coef = N - (sum(N) - sum(vec[i] - 1)) - vec[i];
    assert(coef >= 0);
    ret = (ret - (coef * fact[N - i - 1]) % MOD + MOD) % MOD;
    S.insert(vec[i]);
    add(vec[i], 1);
  }
  return (ret - 1LL + MOD) % MOD;
}

int main() {
  fact[0] = fact[1] = 1LL;
  REP(i, 2, 200000) fact[i] = (fact[i - 1] * ll(i)) % MOD;
  while (cin >> N, N) {
    rep(i, N + 1) bit[i] = 0;
    vector<int> vec(N);
    rep(i, N) vec[i] = i + 1;
    int R;
    cin >> R;
    rep(_, R) {
      int s, t;
      cin >> s >> t;
      --s, --t;
      swap(vec[s], vec[t]);
    }
    cout << compute(vec) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)

using namespace std;

typedef long long ll;
const ll MOD = 1000000007LL;
int N;
ll fact[200000];

ll compute(vector<int> vec) {
  ll ret = fact[vec.size()];
  set<int> S;
  rep(i, vec.size()) {
    ll coef = 0;
    for (int j = N; j > vec[i]; j--)
      if (!S.count(j)) {
        ++coef;
      }
    ret = (ret - (coef * fact[N - i - 1]) % MOD + MOD) % MOD;
    S.insert(vec[i]);
  }
  return (ret - 1LL + MOD) % MOD;
}

int main() {
  fact[0] = fact[1] = 1LL;
  REP(i, 2, 200000) fact[i] = (fact[i - 1] * ll(i)) % MOD;
  while (cin >> N, N) {
    vector<int> vec(N);
    rep(i, N) vec[i] = i + 1;
    int R;
    cin >> R;
    rep(_, R) {
      int s, t;
      cin >> s >> t;
      --s, --t;
      swap(vec[s], vec[t]);
    }
    cout << compute(vec) << endl;
  }
  return 0;
}
Problem p00266 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
int N;
int R;
int S, T;
int pos[114514];
int bit[(1 << 18) + 2];
ll kai[114514];
ll mod = 1000000007;
int sum(int i) {
  int s = 0;
  while (i > 0) {
    s += bit[i];
    i -= i & -i;
  }
  return s;
}
void add(int i) {
  while (i <= (1 << 18)) {
    bit[i]++;
    i += i & -i;
  }
}
int main() {
  while (true) {
    scanf("%d", &N);
    for (int i = 0; i < N; i++) {
      pos[i] = i;
    }
    for (int i = 0; i < (1 << 18) + 2; i++) {
      bit[i] = 0;
    }
    if (N == 0)
      return 0;
    scanf("%d", &R);
    for (int i = 0; i < R; i++) {
      scanf("%d%d", &S, &T);
      S--;
      T--;
      swap(pos[S], pos[T]);
    }
    kai[1] = 1;
    for (int i = 2; i < 114514; i++) {
      kai[i] = (kai[i - 1] * (ll)i) % mod;
    }
    ll ans = 0;
    for (int i = N - 1; i >= 0; i--) {
      ans += (kai[N - i - 1] * (ll)sum(pos[i])) % mod;
      // printf("%d\\n",sum(pos[i]));
      add(pos[i] + 1);
      ans %= mod;
    }
    printf("%lld\\n", ans);
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
int N, R;
int S, T;
vector<int> cha;
int pos[114514];
ll mod = 1000000007;
int main() {
  while (true) {
    scanf("%d", &N);
    for (int i = 0; i < N; i++) {
      pos[i] = i;
    }
    if (N == 0)
      return 0;
    scanf("%d", &R);
    for (int i = 0; i < R; i++) {
      scanf("%d%d", &S, &T);
      S--;
      T--;
      swap(pos[S], pos[T]);
      cha.push_back(S);
      cha.push_back(T);
    }
    sort(cha.begin(), cha.end());
    for (int i = 0; i < (int)cha.size() - 1; i++) {
      if (cha[i] == cha[i + 1]) {
        cha.erase(cha.begin() + i);
        i--;
      }
    }
    int indexcha = (int)cha.size() - 1;
    ll ans = 0;
    ll now = 1;
    for (int i = N - 1; i >= 0; i--) {
      ll cou = 0;
      if (i == cha[indexcha]) {
        for (int j = i + 1; j < N; j++) {
          if (pos[i] > pos[j])
            cou++;
        }
        ans += (cou * now);
        indexcha--;
      } else {
        for (int j = 0; j < (int)cha.size(); j++) {
          if (i < cha[j] && pos[i] > pos[cha[j]]) {
            cou++;
          }
        }
        ans += (cou * now);
      }
      now *= (ll)(N - i);
      now %= mod;
      if (ans >= 10000000000000000)
        ans %= mod;
    }
    printf("%lld\\n", ans % mod);
  }
}