Problem p02590 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define SZ(x) ((int)x.size())
#define ALL(x) x.begin(), x.end()
#define L(i, u) for (register int i = head[u]; i; i = nxt[i])
#define rep(i, a, b) for (register int i = (a); i <= (b); i++)
#define per(i, a, b) for (register int i = (a); i >= (b); i--)
using namespace std;
typedef long double ld;
typedef long long ll;
typedef unsigned int ui;
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;
typedef vector<int> Vi;
template <class T> inline void read(T &x) {
  x = 0;
  char c = getchar();
  int f = 1;
  while (!isdigit(c)) {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = x * 10 + c - '0';
    c = getchar();
  }
  x *= f;
}
template <class T> T gcd(T a, T b) { return !b ? a : gcd(b, a % b); }
template <class T> inline void umin(T &x, T y) { x = x < y ? x : y; }
template <class T> inline void umax(T &x, T y) { x = x > y ? x : y; }
mt19937 R(chrono::system_clock().now().time_since_epoch().count());
#define pi acos(-1)
const int N = 1 << 19 | 3;
// inline void add(int &x, int y){x=x+y<mo?x+y:x+y-mo;}
// inline void sub(int &x, int y){x=x-y>=0?x-y:x-y+mo;}
inline int power(int a, int n, int mo) {
  int res = 1;
  while (n) {
    if (n & 1)
      res = 1LL * res * a % mo;
    a = 1LL * a * a % mo;
    n >>= 1;
  }
  return res;
}

struct E {
  double a, b; // a+bi
  double imag() { return b; }
  double real() { return a; }
  E(double A = 0, double B = 0) {
    a = A;
    b = B;
  }
};
E operator+(const E &x, const E &y) { return E(x.a + y.a, x.b + y.b); }
E operator-(const E &x, const E &y) { return E(x.a - y.a, x.b - y.b); }
E operator*(const E &x, const E &y) {
  return E(x.a * y.a - x.b * y.b, x.a * y.b + x.b * y.a);
}
E w[N], a[N];
ll c[N];
inline void fft(E *a, int n, int tp) {
  for (int i = 1, j = 0; i < n; i++) {
    for (int k = (n >> 1); !((j ^= k) & k); k >>= 1)
      ;
    if (i < j)
      swap(a[i], a[j]);
  }
  for (int j = 2; j <= n; j <<= 1) {
    w[0] = 1;
    w[1] = E(cos(2 * pi / j), tp * sin(2 * pi / j));
    int m = (j >> 1) - 1;
    rep(i, 2, m) w[i] = w[i - 1] * w[1];
    for (register int i = 0; i < n; i += j)
      rep(k, 0, m) {
        E x = a[i + k + (j >> 1)] * w[k];
        a[i + k + (j >> 1)] = a[i + k] - x;
        a[i + k] = a[i + k] + x;
      }
  }
}
inline void mul(E *a, E *b, int l1, int l2, ll *c) { // a[0..l1] * b[0..l2]
  int n;
  for (n = 1; n <= l1 + l2; n <<= 1)
    ;
  rep(i, l1 + 1, n - 1) a[i] = 0;
  rep(i, l2 + 1, n - 1) b[i] = 0;
  fft(a, n, 1); // fft(b,n,1);
  rep(i, 0, n - 1) a[i] = a[i] * b[i];
  fft(a, n, -1);
  rep(i, 0, n - 1) c[i] = (a[i].real()) / n + 0.5;
}
int n, g = 2, P = 200003;
Vi pr = {2, 11, 9091};
int pos[266666];
bool ck(int g) {
  for (int x : pr)
    if (power(g, (P - 1) / x, P) == 1)
      return 0;
  return 1;
}
int main() {
  while (!ck(g))
    g++;
  cerr << g << endl;
  memset(pos, -1, sizeof(pos));
  for (int i = 0, x = 1; i < P - 1; i++, x = 1ll * x * g % P) {
    //	cerr<<x<<endl;
    assert(pos[x] == -1);
    pos[x] = i;
  }
  read(n);
  ll res = 0;
  rep(i, 1, n) {
    int x;
    read(x);
    if (x)
      a[pos[x]].a++;
    res = (res - 1ll * x * x % P);
  }
  mul(a, a, P, P, c);

  rep(i, 0, P * 2) if (c[i]) {
    res = (res + 1ll * c[i] * power(g, i, P));
    //	cerr<<i<<' '<<c[i]<<' '<<power(g,i,P)<<endl;
  }
  //	assert(res%2==0);
  res /= 2;
  cout << res << endl;
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define SZ(x) ((int)x.size())
#define ALL(x) x.begin(), x.end()
#define L(i, u) for (register int i = head[u]; i; i = nxt[i])
#define rep(i, a, b) for (register int i = (a); i <= (b); i++)
#define per(i, a, b) for (register int i = (a); i >= (b); i--)
using namespace std;
typedef long double ld;
typedef long long ll;
typedef unsigned int ui;
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;
typedef vector<int> Vi;
template <class T> inline void read(T &x) {
  x = 0;
  char c = getchar();
  int f = 1;
  while (!isdigit(c)) {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = x * 10 + c - '0';
    c = getchar();
  }
  x *= f;
}
template <class T> T gcd(T a, T b) { return !b ? a : gcd(b, a % b); }
template <class T> inline void umin(T &x, T y) { x = x < y ? x : y; }
template <class T> inline void umax(T &x, T y) { x = x > y ? x : y; }
mt19937 R(chrono::system_clock().now().time_since_epoch().count());

const int N = 4e6 + 11;
int n, A, B, a[N], b[N], ord[N], zhi[N];
Pii v[N];
void tian(int B) {
  int l = 1, r = n, p = 1;
  //	reverse(ord+1,ord+n+1);
  while (p <= n) {
    rep(i, 1, A - B) if (l <= r) b[p++] = ord[l++];
    rep(i, 1, B) if (l <= r) b[p++] = ord[r--];
  }
  /*if(n&1){
          b[n+1>>1]=ord[p++];
          rep(i,1,n>>1)b[n/2+1+i]=ord[p++],b[n/2+1-i]=ord[p++];
  }
  else{
          rep(i,1,n>>1)b[n/2+i]=ord[p++],b[n/2+1-i]=ord[p++];
  }*/
  //	rep(i,1,n)printf("%d ",b[i]);puts("");
}

int val[N << 2];
void build(int k, int l, int r) {
  val[k] = 0;
  if (l == r)
    return;
  int mid = (l + r) >> 1;
  build(k << 1, l, mid);
  build(k << 1 | 1, mid + 1, r);
}
void ins(int k, int p, int x, int l, int r) {
  val[k] += x;
  if (l == r)
    return;
  int mid = (l + r) >> 1;
  if (p <= mid)
    ins(k << 1, p, x, l, mid);
  else
    ins(k << 1 | 1, p, x, mid + 1, r);
}
int qry(int k, int p, int l, int r) {
  if (l == r)
    return l;
  int mid = (l + r) >> 1;
  //	printf("%d %d %d %d:%d\\n",k,p,l,r,val[k<<1|1]);
  if (val[k << 1 | 1] >= p)
    return qry(k << 1 | 1, p, mid + 1, r);
  return qry(k << 1, p - val[k << 1 | 1], l, mid);
}
ll getans() {
  int l = 1, r = 0;
  ll res = 0;
  build(1, 1, n);
  rep(i, 1, n - A + 1) {
    while (r + 1 <= i + A - 1)
      r++, ins(1, b[r], 1, 1, n);
    while (l < i)
      ins(1, b[l], -1, 1, n), l++;
    //	assert(val[1]==A);
    //	printf("%d:%d (%d %d)\\n",i,qry(1,B,1,n),val[3],B);
    res += zhi[qry(1, B, 1, n)];
  }
  return res;
}
int main() {
  int T;
  read(T);
  while (T--) {
    read(n);
    read(A);
    read(B);
    rep(i, 1, n) read(a[i]), v[i] = mp(a[i], i);
    sort(v + 1, v + n + 1);
    rep(i, 1, n) zhi[i] = v[i].fi;
    rep(i, 1, n) ord[i] = i;
    tian(B);
    printf("%lld ", getans());
    rep(i, 1, n) ord[i] = n - i + 1;
    tian(A - B + 1);
    printf("%lld\\n", getans());
  }
  return 0;
}