Count substrings having frequency of a character exceeding that of another character in a string

#include using namespace std;void update(int ind, vector& segTree,            int n){        ind += n;        segTree[ind]++;    for (; ind > 1; ind >>= 1) {                segTree[ind >> 1] = segTree[ind]                            + segTree[ind ^ 1];    }}int query(int low, int high,          vector& segTree, int n){            low += n;    high += n;    int ans = 0;    while (low < high) {                        if (low % 2) {            ans += segTree[low];            low++;        }        if (high % 2) {            high--;            ans += segTree[high];        }                low >>= 1;        high >>= 1;    }    return ans;}void countSubstrings(string& s){        int n = s.length();        vector segTree(4 * n);    int count = 0;            update(n, segTree, 2 * n);        int ans = 0;        for (int i = 0; i < n; i++) {                if (s[i] == 'a')            count++;                else if (s[i] == 'c')            count--;                                int val = query(0, n + count,                        segTree, 2 * n);        ans += val;                        update(n + count, segTree, 2 * n);    }        cout