Kick Start Candies Question

Problem Statement: https://codingcompetitions.withgoogle.com/kickstart/round/000000000019ff43/0000000000337b4d#problem (test set 1)

My code:

#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define ll long long
using namespace std;

void setIO(string s = "") {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  if ((int)s.size()) {
    freopen((s + ".in").c_str(), "r", stdin);
    freopen((s + ".out").c_str(), "w", stdout);
  }
}

const int MX = 2e5 + 5;

int n, q;
int A[MX];
ll psum[MX], ipsum[MX];

void prefix(int x) {
  for (int i = x; i < n; i++) {
    psum[i + 1] = psum[i] + A[i] * pow(-1, i);
    ipsum[i + 1] = ipsum[i] + A[i] * (i + 1) * pow(-1, i);
  }
}

int main() {
  setIO();
  int t;
  cin >> t;
  for (int test = 1; test <= t; test++) {
    cin >> n >> q;
    for (int i = 0; i < n; i++) {
      cin >> A[i];
    }
    ll sol = 0;
    prefix(0);
    while (q--) {
      char type;
      cin >> type;
      int l, r;
      cin >> l >> r;
      if (type == 'Q') {
        ll cur = ipsum[r] - ipsum[l - 1] - (l - 1) * (psum[r] - psum[l - 1]);
        sol += cur;
      } else {
        l--;
        A[l] = r;
        prefix(l);
      }
    }
    cout << "Case #" << test << ": " << sol << endl;
  }
  return 0;
}

It fails the first test case of the example, more specifically the first query I think. Any thoughts?

After reading the analysis, I see that I need to add cur = cur * pow(-1, l - 1);… but why?

Because you need to find the alternating sum?

It doesn’t say so in the problem statement correct me if I’m wrong


It says so right here?

No, thats the individual sweetness score… we are looking for the sum of all queries

Anyways, I understand this now :smiley: