#include <iostream>
#include <vector>
#include <string>
using namespace std;
vector<int> numberOfItems(string s, vector<int> startIndices, vector<int> endIndices) {
int n = s.size();
vector<int> prefix(n, 0), leftPipe(n, -1), rightPipe(n, -1);
int count = 0, lastPipe = -1;
// Step 1: Compute prefix sum of '*'
for (int i = 0; i < n; i++) {
if (s[i] == '|') lastPipe = i;
leftPipe[i] = lastPipe;
count += (s[i] == '*');
prefix[i] = count;
}
lastPipe = -1;
for (int i = n - 1; i >= 0; i--) {
if (s[i] == '|') lastPipe = i;
rightPipe[i] = lastPipe;
}
for (auto i : leftPipe) {
cout << i << " ";
}
cout << endl;
for (auto i : rightPipe) {
cout << i << " ";
}
cout << endl;
// Step 2: Answer queries efficiently
vector<int> result;
for (size_t i = 0; i < startIndices.size(); i++) {
int left = startIndices[i] - 1; // Convert to 0-based index
int right = endIndices[i] - 1;
int start = rightPipe[left];
int end = leftPipe[right];
if (start != -1 && end != -1 && start < end) {
result.push_back(prefix[end] - prefix[start]);
} else {
result.push_back(0);
}
}
return result;
}
int main() {
string s = "|**|*|*";
vector<int> startIndices = {1, 1, 3, 5};
vector<int> endIndices = {5, 6, 6, 7};
vector<int> res = numberOfItems(s, startIndices, endIndices);
for (int x : res) {
cout << x << " ";
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8c3RyaW5nPgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCnZlY3RvcjxpbnQ+IG51bWJlck9mSXRlbXMoc3RyaW5nIHMsIHZlY3RvcjxpbnQ+IHN0YXJ0SW5kaWNlcywgdmVjdG9yPGludD4gZW5kSW5kaWNlcykgewogICAgaW50IG4gPSBzLnNpemUoKTsKICAgIHZlY3RvcjxpbnQ+IHByZWZpeChuLCAwKSwgbGVmdFBpcGUobiwgLTEpLCByaWdodFBpcGUobiwgLTEpOwogICAgaW50IGNvdW50ID0gMCwgbGFzdFBpcGUgPSAtMTsKCiAgICAvLyBTd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