Problem p03276 (C++)

Buggy submission (status: Runtime Error):
/// In the name of ALLAH

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

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;
typedef double dl;

#define PB push_back
// #define PB emplace_back
#define F first
#define S second
#define MP make_pair
#define endl '\n'
#define all(a) (a).begin(), (a).end()
#define sz(x) (int)x.size()
#define mx_int_prime 999999937

const double PI = acos(-1);
const double eps = 1e-9;
const int inf = 2000000000;
const ll infLL = 9000000000000000000;
#define MOD 1000000007
// #define harmonic(n) 0.57721566490153286l+log(n)

#define mem(a, b) memset(a, b, sizeof(a))
#define gcd(a, b) __gcd(a, b)
#define sqr(a) ((a) * (a))

#define optimize()                                                             \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define fraction()                                                             \
  cout.unsetf(ios::floatfield);                                                \
  cout.precision(10);                                                          \
  cout.setf(ios::fixed, ios::floatfield);
#define file()                                                                 \
  freopen("input.txt", "r", stdin);                                            \
  freopen("output.txt", "w", stdout);

typedef vector<int>::iterator vit;
typedef set<int>::iterator sit;

inline bool checkBit(ll n, int i) { return n & (1LL << i); }
inline ll setBit(ll n, int i) { return n | (1LL << i); }
inline ll resetBit(ll n, int i) { return n & (~(1LL << i)); }

struct edge {
  int u, v, w;
};

bool cmp(const edge &a, const edge &b) { return a.w < b.w; }

string to_s(int t) {
  stringstream ss;
  ss << t;
  return ss.str();
}

int dx[] = {0, 0, +1, -1};
int dy[] = {+1, -1, 0, 0};
// int dx[] = {+1, 0, -1, 0, +1, +1, -1, -1};
// int dy[] = {0, +1, 0, -1, +1, -1, +1, -1};
inline ll max(ll a, ll b) { return (a >= b) ? a : b; }
inline ll min(ll a, ll b) { return (a <= b) ? a : b; }
inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }
inline bool isLeapYear(ll year) {
  return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
}
inline ll lcm(ll a, ll b) { return (a * (b / gcd(a, b))); }
inline void normal(ll &a) {
  a %= MOD;
  (a < 0) && (a += MOD);
}
inline ll modMul(ll a, ll b) {
  a %= MOD, b %= MOD;
  normal(a), normal(b);
  return (a * b) % MOD;
}
inline ll modAdd(ll a, ll b) {
  a %= MOD, b %= MOD;
  normal(a), normal(b);
  return (a + b) % MOD;
}
inline ll modSub(ll a, ll b) {
  a %= MOD, b %= MOD;
  normal(a), normal(b);
  a -= b;
  normal(a);
  return a;
}
inline ll modPow(ll b, ll p) {
  ll r = 1;
  while (p) {
    if (p & 1)
      r = modMul(r, b);
    b = modMul(b, b);
    p >>= 1;
  }
  return r;
}
inline ll modInverse(ll a) { return modPow(a, MOD - 2); }
inline ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); }

/*
bool seive[1010000];
vi prime;

void seiveGen(int limit) {
    limit += 100;
    int sqrtn = sqrt(limit);
    for(int i = 3; i <= sqrtn; i += 2) {
            if(!seive[i>>1]) {
                    for(int j = i * i; j < limit; j += i + i) {
                            seive[j>>1] = 1;
                    }
            }
    }
    prime.PB(2);
    for(int i = 3; i < limit; i += 2) {
            if(!seive[i>>1]) prime.PB(i);
    }
}
*/

//
// debug
// #ifdef
template <typename F, typename S>
ostream &operator<<(ostream &os, const pair<F, S> &p) {
  return os << "(" << p.first << ", " << p.second << ")";
}

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  for (auto it = v.begin(); it != v.end(); ++it) {
    if (it != v.begin())
      os << ", ";
    os << *it;
  }
  return os << "}";
}

template <typename T> ostream &operator<<(ostream &os, const set<T> &v) {
  os << "[";
  for (auto it = v.begin(); it != v.end(); ++it) {
    if (it != v.begin())
      os << ", ";
    os << *it;
  }
  return os << "]";
}

template <typename T> ostream &operator<<(ostream &os, const multiset<T> &v) {
  os << "[";
  for (auto it = v.begin(); it != v.end(); ++it) {
    if (it != v.begin())
      os << ", ";
    os << *it;
  }
  return os << "]";
}

template <typename F, typename S>
ostream &operator<<(ostream &os, const map<F, S> &v) {
  os << "[";
  for (auto it = v.begin(); it != v.end(); ++it) {
    if (it != v.begin())
      os << ", ";
    os << it->first << " = " << it->second;
  }
  return os << "]";
}

#define dbg(args...)                                                           \
  do {                                                                         \
    cerr << #args << " : ";                                                    \
    faltu(args);                                                               \
  } while (0)

void faltu() { cerr << endl; }

template <typename T> void faltu(T a[], int n) {
  for (int i = 0; i < n; ++i)
    cerr << a[i] << ' ';
  cerr << endl;
}

template <typename T, typename... hello>
void faltu(T arg, const hello &...rest) {
  cerr << arg << ' ';
  faltu(rest...);
}
// #else
// #define dbg(args...)

const int mxx = 1e5 + 123;
int a[mxx];

struct info {
  int mn, mx;
} t[mxx];

void init(int id, int b, int e) {
  if (b == e) {
    t[id].mn = t[id].mx = a[b];
    return;
  }

  int mid = (b + e) >> 1;

  init(id * 2, b, mid);
  init(id * 2 + 1, mid + 1, e);

  t[id].mx = max(t[id * 2].mx, t[id * 2 + 1].mx);
  t[id].mn = min(t[id * 2].mn, t[id * 2 + 1].mn);
}

info ask(int id, int b, int e, int i, int j) {
  info ret;
  ret.mn = INT_MAX;
  ret.mx = INT_MIN;
  if (b > j || e < i)
    return ret;
  if (b >= i && e <= j)
    return t[id];

  int mid = (b + e) >> 1;

  info ret1 = ask(id * 2, b, mid, i, j);
  info ret2 = ask(id * 2 + 1, mid + 1, e, i, j);

  ret.mn = min(ret1.mn, ret2.mn);
  ret.mx = max(ret1.mx, ret2.mx);

  return ret;
}

int main() {
  optimize();

  int n, k, cnt = 0, id1 = 0, id2 = 0;
  ll ans = LLONG_MAX;
  cin >> n >> k;

  for (int i = 1; i <= n; i++)
    cin >> a[i];

  init(1, 1, n);

  for (int i = 1; i + k - 1 <= n; i++) {
    info ret = ask(1, 1, n, i, i + k - 1);
    //\ dbg ( i, ret.mn, ret.mx );
    if (ret.mn < 0 && ret.mx >= 0) {
      ans = min(ans, abs(ret.mn) + ret.mx + min(abs(ret.mn), ret.mx));
    } else if (ret.mn < 0 && ret.mx < 0)
      ans = min(ans, abs(ret.mn));
    else
      ans = min(ans, ret.mx);
  }

  cout << ans;

  return 0;
}


Fixed submission:
/// In the name of ALLAH

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

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;
typedef double dl;

#define PB push_back
// #define PB emplace_back
#define F first
#define S second
#define MP make_pair
#define endl '\n'
#define all(a) (a).begin(), (a).end()
#define sz(x) (int)x.size()
#define mx_int_prime 999999937

const double PI = acos(-1);
const double eps = 1e-9;
const int inf = 2000000000;
const ll infLL = 9000000000000000000;
#define MOD 1000000007
// #define harmonic(n) 0.57721566490153286l+log(n)

#define mem(a, b) memset(a, b, sizeof(a))
#define gcd(a, b) __gcd(a, b)
#define sqr(a) ((a) * (a))

#define optimize()                                                             \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define fraction()                                                             \
  cout.unsetf(ios::floatfield);                                                \
  cout.precision(10);                                                          \
  cout.setf(ios::fixed, ios::floatfield);
#define file()                                                                 \
  freopen("input.txt", "r", stdin);                                            \
  freopen("output.txt", "w", stdout);

typedef vector<int>::iterator vit;
typedef set<int>::iterator sit;

inline bool checkBit(ll n, int i) { return n & (1LL << i); }
inline ll setBit(ll n, int i) { return n | (1LL << i); }
inline ll resetBit(ll n, int i) { return n & (~(1LL << i)); }

struct edge {
  int u, v, w;
};

bool cmp(const edge &a, const edge &b) { return a.w < b.w; }

string to_s(int t) {
  stringstream ss;
  ss << t;
  return ss.str();
}

int dx[] = {0, 0, +1, -1};
int dy[] = {+1, -1, 0, 0};
// int dx[] = {+1, 0, -1, 0, +1, +1, -1, -1};
// int dy[] = {0, +1, 0, -1, +1, -1, +1, -1};
inline ll max(ll a, ll b) { return (a >= b) ? a : b; }
inline ll min(ll a, ll b) { return (a <= b) ? a : b; }
inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }
inline bool isLeapYear(ll year) {
  return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
}
inline ll lcm(ll a, ll b) { return (a * (b / gcd(a, b))); }
inline void normal(ll &a) {
  a %= MOD;
  (a < 0) && (a += MOD);
}
inline ll modMul(ll a, ll b) {
  a %= MOD, b %= MOD;
  normal(a), normal(b);
  return (a * b) % MOD;
}
inline ll modAdd(ll a, ll b) {
  a %= MOD, b %= MOD;
  normal(a), normal(b);
  return (a + b) % MOD;
}
inline ll modSub(ll a, ll b) {
  a %= MOD, b %= MOD;
  normal(a), normal(b);
  a -= b;
  normal(a);
  return a;
}
inline ll modPow(ll b, ll p) {
  ll r = 1;
  while (p) {
    if (p & 1)
      r = modMul(r, b);
    b = modMul(b, b);
    p >>= 1;
  }
  return r;
}
inline ll modInverse(ll a) { return modPow(a, MOD - 2); }
inline ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); }

/*
bool seive[1010000];
vi prime;

void seiveGen(int limit) {
    limit += 100;
    int sqrtn = sqrt(limit);
    for(int i = 3; i <= sqrtn; i += 2) {
            if(!seive[i>>1]) {
                    for(int j = i * i; j < limit; j += i + i) {
                            seive[j>>1] = 1;
                    }
            }
    }
    prime.PB(2);
    for(int i = 3; i < limit; i += 2) {
            if(!seive[i>>1]) prime.PB(i);
    }
}
*/

//
// debug
// #ifdef
template <typename F, typename S>
ostream &operator<<(ostream &os, const pair<F, S> &p) {
  return os << "(" << p.first << ", " << p.second << ")";
}

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  for (auto it = v.begin(); it != v.end(); ++it) {
    if (it != v.begin())
      os << ", ";
    os << *it;
  }
  return os << "}";
}

template <typename T> ostream &operator<<(ostream &os, const set<T> &v) {
  os << "[";
  for (auto it = v.begin(); it != v.end(); ++it) {
    if (it != v.begin())
      os << ", ";
    os << *it;
  }
  return os << "]";
}

template <typename T> ostream &operator<<(ostream &os, const multiset<T> &v) {
  os << "[";
  for (auto it = v.begin(); it != v.end(); ++it) {
    if (it != v.begin())
      os << ", ";
    os << *it;
  }
  return os << "]";
}

template <typename F, typename S>
ostream &operator<<(ostream &os, const map<F, S> &v) {
  os << "[";
  for (auto it = v.begin(); it != v.end(); ++it) {
    if (it != v.begin())
      os << ", ";
    os << it->first << " = " << it->second;
  }
  return os << "]";
}

#define dbg(args...)                                                           \
  do {                                                                         \
    cerr << #args << " : ";                                                    \
    faltu(args);                                                               \
  } while (0)

void faltu() { cerr << endl; }

template <typename T> void faltu(T a[], int n) {
  for (int i = 0; i < n; ++i)
    cerr << a[i] << ' ';
  cerr << endl;
}

template <typename T, typename... hello>
void faltu(T arg, const hello &...rest) {
  cerr << arg << ' ';
  faltu(rest...);
}
// #else
// #define dbg(args...)

const int mxx = 1e5 + 123;
int a[mxx];

struct info {
  int mn, mx;
} t[mxx * 3];

void init(int id, int b, int e) {
  if (b == e) {
    t[id].mn = t[id].mx = a[b];
    return;
  }

  int mid = (b + e) >> 1;

  init(id * 2, b, mid);
  init(id * 2 + 1, mid + 1, e);

  t[id].mx = max(t[id * 2].mx, t[id * 2 + 1].mx);
  t[id].mn = min(t[id * 2].mn, t[id * 2 + 1].mn);
}

info ask(int id, int b, int e, int i, int j) {
  info ret;
  ret.mn = INT_MAX;
  ret.mx = INT_MIN;
  if (b > j || e < i)
    return ret;
  if (b >= i && e <= j)
    return t[id];

  int mid = (b + e) >> 1;

  info ret1 = ask(id * 2, b, mid, i, j);
  info ret2 = ask(id * 2 + 1, mid + 1, e, i, j);

  ret.mn = min(ret1.mn, ret2.mn);
  ret.mx = max(ret1.mx, ret2.mx);

  return ret;
}

int main() {
  optimize();

  int n, k, cnt = 0, id1 = 0, id2 = 0;
  ll ans = LLONG_MAX;
  cin >> n >> k;

  for (int i = 1; i <= n; i++)
    cin >> a[i];

  init(1, 1, n);

  for (int i = 1; i + k - 1 <= n; i++) {
    info ret = ask(1, 1, n, i, i + k - 1);
    //\ dbg ( i, ret.mn, ret.mx );
    if (ret.mn < 0 && ret.mx >= 0) {
      ans = min(ans, abs(ret.mn) + ret.mx + min(abs(ret.mn), ret.mx));
    } else if (ret.mn < 0 && ret.mx < 0)
      ans = min(ans, abs(ret.mn));
    else
      ans = min(ans, ret.mx);
  }

  cout << ans;

  return 0;
}