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


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

#define fastio                                                                 \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);                                                                 \\
  cout << fixed;                                                               \\
  cout << setprecision(12);
#define randomINT                                                              \\
  mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define newl cout << "\\n"
#define DISP(as)                                                               \\
  for (auto it : as)                                                           \\
    cout << it << " ";                                                         \\
  newl;
#define all(x) (x).begin(), (x).end()
#define mset(x, val) memset(x, val, sizeof(x))
#define newl cout << "\\n"
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define dline cerr << "///REACHED///\\n";
#define deb1(x) cerr << #x << " = " << x << '\\n';
#define deb2(x, y)                                                             \\
  cerr << '[' << #x << ',' << #y << "] = " << '[' << x << ',' << y << ']'      \\
       << '\\n';
#define deb3(x, y, z)                                                          \\
  cerr << '[' << #x << ',' << #y << ',' << #z << "] = " << '[' << x << ','     \\
       << y << ',' << z << ']' << '\\n';

typedef long long ll;
typedef long double ld;
typedef vector<ll> vll;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pld;
typedef unordered_map<ll, ll> um;
typedef vector<pll> vpll;

const ll MAX5 = 1e+5 + 7;
const ll MAX7 = 1e+7 + 7;
const ll MAXN = 5e5;
const ll INF = 0x7f7f7f7f7f7f7f7f;
const int INFi = 0x7f7f7f7f;
const ll MOD = 1e+9 + 7;

vll adj[MAXN];
ll vis[MAXN] = {};
/*
int dx8[]={0,1,1,1,0,-1,-1,-1}, dy8[]={1,1,0,-1,-1,-1,0,1};
int dx4[]={0,1,0,-1}, dy4[]={1,0,-1,0};
*/
// <------------- Declare Variables Here ------------> //

ll T = 1;
ll n, x, k, m, r, sum = 0, a, b, l;
string s;

ll L, R;
ll cou, val, ans;

ll tree_l[2000000];
ll a_l[400000];

ll tree_r[2000000];
ll a_r[400000];

// <------------- Implement Functions Here ----------> //

long long exponentiation(long long base, long long exp, long long N) {
  if (base == 0)
    return 0;
  if (exp == 0)
    return 1 % N;

  if (exp == 1)
    return base % N;

  long long t = exponentiation(base, exp / 2, N);
  t = (t * t) % N;

  // if exponent is even value
  if (exp % 2 == 0)
    return t;

  // if exponent is odd value
  else
    return ((base % N) * t) % N;
}

void build1(ll start = 1, ll end = n, ll node = 1) {
  if (start == end) {

    tree_l[node] = a_l[start];
  } else {
    ll mid = (start + end) / 2;
    build1(start, mid, 2 * node);
    build1(mid + 1, end, 2 * node + 1);
    tree_l[node] = (tree_l[2 * node] + tree_l[2 * node + 1]) % L;
  }
}

void update1(ll indx, ll val, ll start = 1, ll end = n, ll node = 1) {
  if (start == end) {
    a_l[indx] = val;
    tree_l[node] = val;
  } else {
    ll mid = (start + end) / 2;
    if (start <= indx && indx <= mid)
      update1(indx, val, start, mid, 2 * node);
    else
      update1(indx, val, mid + 1, end, 2 * node + 1);
    tree_l[node] = (tree_l[2 * node] + tree_l[2 * node + 1]) % L;
  }
}

ll query1(ll l = 1, ll r = n, ll start = 1, ll end = n, ll node = 1) {
  if (r < start || end < l)
    return 0;
  else if (l <= start && end <= r) {
    return tree_l[node];
  } else {
    ll mid = (start + end) / 2;
    ll p1 = query1(l, r, start, mid, 2 * node);
    ll p2 = query1(l, r, mid + 1, end, 2 * node + 1);
    return (p1 + p2) % L;
  }
}

void build2(ll start = 1, ll end = n, ll node = 1) {
  if (start == end) {

    tree_r[node] = a_r[start];
  } else {
    ll mid = (start + end) / 2;
    build2(start, mid, 2 * node);
    build2(mid + 1, end, 2 * node + 1);
    tree_r[node] = (tree_r[2 * node] + tree_r[2 * node + 1]) % R;
  }
}

void update2(ll indx, ll val, ll start = 1, ll end = n, ll node = 1) {
  if (start == end) {
    a_r[indx] = val;
    tree_r[node] = val;
  } else {
    ll mid = (start + end) / 2;
    if (start <= indx && indx <= mid)
      update2(indx, val, start, mid, 2 * node);
    else
      update2(indx, val, mid + 1, end, 2 * node + 1);
    tree_r[node] = (tree_r[2 * node] + tree_r[2 * node + 1]) % R;
  }
}

ll query2(ll l = 1, ll r = n, ll start = 1, ll end = n, ll node = 1) {
  if (r < start || end < l)
    return 0;
  else if (l <= start && end <= r) {
    return tree_r[node];
  } else {
    ll mid = (start + end) / 2;
    ll p1 = query2(l, r, start, mid, 2 * node);
    ll p2 = query2(l, r, mid + 1, end, 2 * node + 1);
    return (p1 + p2) % R;
  }
}

// <------------- Start of main() -------------------> //

void MAIN() {
  //__builtin_popcount();
  cin >> n >> s;
  cou = count(all(s), '1');
  L = cou - 1;
  R = cou + 1;
  vll ANS;
  reverse(all(s));
  s = '#' + s;
  // cout<<s;newl;
  // cout<<cou<<" "<<L<<" "<<R;newl;
  for (ll i = 1; i <= n; i++) {
    if (L > 0) {
      a_l[i] = exponentiation(2 * (s[i] - '0'), i - 1, L) % L;
    }
    a_r[i] = exponentiation(2 * (s[i] - '0'), i - 1, R) % R;
  }
  if (L > 0)
    build1();
  build2();

  // cout<<query1();newl;newl;
  // cout<<query2();newl;newl;
  // dline;
  for (ll i = 1; i <= n; i++) {
    val = cou;
    if (s[i] == '0')
      val++;
    else
      val--;
    // dline;
    // deb1(i);
    // deb2(i,val);
    if (val < 2) {
      ANS.pb(val);
      continue;
    }

    if (val < cou) {
      ll temp = a_l[i];
      update1(i, 0);
      ll m = query1();

      // cout<<i<<" "<<m<<" ";newl;
      ans = 1;
      while (m) {
        ans++;
        m %= __builtin_popcount(m);
      }

      ANS.pb(ans);
      update1(i, temp);
    } else {
      // dline;
      ll temp = exponentiation(2, i - 1, R);
      // dline;
      update2(i, temp);
      // dline;

      ll m = query2();
      // cout<<i<<" "<<m<<" ";newl;
      ans = 1;
      // dline;
      while (m) {
        ans++;
        m %= __builtin_popcount(m);
      }
      ANS.pb(ans);
      update2(i, 0);
    }
  }
  // dline;
  reverse(all(ANS));
  for (auto it : ANS) {
    cout << it;
    newl;
  }
}

int main() {
  fastio;
  randomINT;
  // cin >> T;
  while (T--) {
    MAIN();
  }
  return 0;
}


Error: WA

Fixed submission:


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

#define fastio                                                                 \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);                                                                 \\
  cout << fixed;                                                               \\
  cout << setprecision(12);
#define randomINT                                                              \\
  mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define newl cout << "\\n"
#define DISP(as)                                                               \\
  for (auto it : as)                                                           \\
    cout << it << " ";                                                         \\
  newl;
#define all(x) (x).begin(), (x).end()
#define mset(x, val) memset(x, val, sizeof(x))
#define newl cout << "\\n"
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define dline cerr << "///REACHED///\\n";
#define deb1(x) cerr << #x << " = " << x << '\\n';
#define deb2(x, y)                                                             \\
  cerr << '[' << #x << ',' << #y << "] = " << '[' << x << ',' << y << ']'      \\
       << '\\n';
#define deb3(x, y, z)                                                          \\
  cerr << '[' << #x << ',' << #y << ',' << #z << "] = " << '[' << x << ','     \\
       << y << ',' << z << ']' << '\\n';

typedef long long ll;
typedef long double ld;
typedef vector<ll> vll;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pld;
typedef unordered_map<ll, ll> um;
typedef vector<pll> vpll;

const ll MAX5 = 1e+5 + 7;
const ll MAX7 = 1e+7 + 7;
const ll MAXN = 5e5;
const ll INF = 0x7f7f7f7f7f7f7f7f;
const int INFi = 0x7f7f7f7f;
const ll MOD = 1e+9 + 7;

vll adj[MAXN];
ll vis[MAXN] = {};
/*
int dx8[]={0,1,1,1,0,-1,-1,-1}, dy8[]={1,1,0,-1,-1,-1,0,1};
int dx4[]={0,1,0,-1}, dy4[]={1,0,-1,0};
*/
// <------------- Declare Variables Here ------------> //

ll T = 1;
ll n, x, k, m, r, sum = 0, a, b, l;
string s;

ll L, R;
ll cou, val, ans;

ll tree_l[2000000];
ll a_l[400000];

ll tree_r[2000000];
ll a_r[400000];

// <------------- Implement Functions Here ----------> //

long long exponentiation(long long base, long long exp, long long N) {
  if (exp == 0)
    return 1;

  if (exp == 1)
    return base % N;

  long long t = exponentiation(base, exp / 2, N);
  t = (t * t) % N;

  // if exponent is even value
  if (exp % 2 == 0)
    return t;

  // if exponent is odd value
  else
    return ((base % N) * t) % N;
}

void build1(ll start = 1, ll end = n, ll node = 1) {
  if (start == end) {

    tree_l[node] = a_l[start];
  } else {
    ll mid = (start + end) / 2;
    build1(start, mid, 2 * node);
    build1(mid + 1, end, 2 * node + 1);
    tree_l[node] = (tree_l[2 * node] + tree_l[2 * node + 1]) % L;
  }
}

void update1(ll indx, ll val, ll start = 1, ll end = n, ll node = 1) {
  if (start == end) {
    a_l[indx] = val;
    tree_l[node] = val;
  } else {
    ll mid = (start + end) / 2;
    if (start <= indx && indx <= mid)
      update1(indx, val, start, mid, 2 * node);
    else
      update1(indx, val, mid + 1, end, 2 * node + 1);
    tree_l[node] = (tree_l[2 * node] + tree_l[2 * node + 1]) % L;
  }
}

ll query1(ll l = 1, ll r = n, ll start = 1, ll end = n, ll node = 1) {
  if (r < start || end < l)
    return 0;
  else if (l <= start && end <= r) {
    return tree_l[node];
  } else {
    ll mid = (start + end) / 2;
    ll p1 = query1(l, r, start, mid, 2 * node);
    ll p2 = query1(l, r, mid + 1, end, 2 * node + 1);
    return (p1 + p2) % L;
  }
}

void build2(ll start = 1, ll end = n, ll node = 1) {
  if (start == end) {

    tree_r[node] = a_r[start];
  } else {
    ll mid = (start + end) / 2;
    build2(start, mid, 2 * node);
    build2(mid + 1, end, 2 * node + 1);
    tree_r[node] = (tree_r[2 * node] + tree_r[2 * node + 1]) % R;
  }
}

void update2(ll indx, ll val, ll start = 1, ll end = n, ll node = 1) {
  if (start == end) {
    a_r[indx] = val;
    tree_r[node] = val;
  } else {
    ll mid = (start + end) / 2;
    if (start <= indx && indx <= mid)
      update2(indx, val, start, mid, 2 * node);
    else
      update2(indx, val, mid + 1, end, 2 * node + 1);
    tree_r[node] = (tree_r[2 * node] + tree_r[2 * node + 1]) % R;
  }
}

ll query2(ll l = 1, ll r = n, ll start = 1, ll end = n, ll node = 1) {
  if (r < start || end < l)
    return 0;
  else if (l <= start && end <= r) {
    return tree_r[node];
  } else {
    ll mid = (start + end) / 2;
    ll p1 = query2(l, r, start, mid, 2 * node);
    ll p2 = query2(l, r, mid + 1, end, 2 * node + 1);
    return (p1 + p2) % R;
  }
}

// <------------- Start of main() -------------------> //

void MAIN() {
  //__builtin_popcount();
  cin >> n >> s;
  cou = count(all(s), '1');
  L = cou - 1;
  R = cou + 1;
  vll ANS;
  reverse(all(s));
  s = '#' + s;

  for (ll i = 1; i <= n; i++) {
    if (L > 0) {
      a_l[i] = exponentiation(2 * (s[i] - '0'), i - 1, L) % L;
    }
    a_r[i] = exponentiation(2 * (s[i] - '0'), i - 1, R) % R;
  }

  build1();
  build2();
  // dline;
  for (ll i = 1; i <= n; i++) {
    val = cou;
    if (s[i] == '0')
      val++;
    else
      val--;
    // dline;
    // deb1(i);
    // deb2(i,val);
    if (val < 2) {
      ANS.pb(val);
      continue;
    }

    if (val < cou) {
      ll temp = a_l[i];
      update1(i, 0);
      ll m = query1();
      cout << i << " " << m << " ";
      newl;
      ans = 1;
      while (m) {
        ans++;
        m %= __builtin_popcount(m);
      }

      ANS.pb(ans);
      update1(i, temp);
    } else {
      // dline;
      ll temp = exponentiation(2, i - 1, R);
      // dline;
      update2(i, temp);
      // dline;

      ll m = query2();
      cout << i << " " << m << " ";
      newl;
      ans = 1;
      // dline;
      while (m) {
        ans++;
        m %= __builtin_popcount(m);
      }
      ANS.pb(ans);
      update2(i, 0);
    }
  }
  // dline;
  reverse(all(ANS));
  for (auto it : ANS) {
    cout << it;
    newl;
  }
}

int main() {
  fastio;
  randomINT;
  // cin >> T;
  while (T--) {
    MAIN();
  }
  return 0;
}