This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define MAXN 10000
#define MAXQ 100
int n, l, a[MAXN + 3], q, mapToSortedUniqQuery[MAXN + 3];
int ans[MAXQ + 3][MAXN + 3];
vector<int> queries, sortedUniqQueries;
int main() {
cin >> n >> l;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
cin >> q;
for (int i = 0; i < q; i++) {
int num;
cin >> num;
queries.push_back(num);
sortedUniqQueries.push_back(num);
}
// Populate mapToSortedUniqQuery
sort(sortedUniqQueries.begin(), sortedUniqQueries.end());
sortedUniqQueries.erase(unique(sortedUniqQueries.begin(), sortedUniqQueries.end()), sortedUniqQueries.end());
for (int k = 1; k <= n; k++) {
mapToSortedUniqQuery[k] = lower_bound(sortedUniqQueries.begin(), sortedUniqQueries.end(), k) - sortedUniqQueries.begin() + 1;
}
memset(ans, 0, sizeof(ans));
for (int d = 1; (1 + d) + (l - 1) <= n; d++) {
int currentDiff = 0;
for (int i = 1; i < l; i++) {
currentDiff += (a[i] != a[i + d]);
}
int leftPtr = 1;
for (int i = l; i + d <= n; i++) {
currentDiff += (a[i] != a[i + d]);
while (i - leftPtr + 1 > l) {
currentDiff -= (a[leftPtr] != a[leftPtr + d]);
leftPtr += 1;
}
// cout << "currentDiff " << leftPtr << " " << d << " " << currentDiff << " " << mapToSortedUniqQuery[currentDiff] << endl;
ans[mapToSortedUniqQuery[currentDiff]][leftPtr] += 1;
ans[mapToSortedUniqQuery[currentDiff]][leftPtr + d] += 1;
}
}
// Apply the prefix sum
for (int idx = 1; idx <= n - l + 1; idx++) {
for (int qIndex = 1; qIndex <= sortedUniqQueries.size(); qIndex++) {
ans[qIndex][idx] += ans[qIndex - 1][idx];
}
}
for (int i = 0; i < q; i++) {
int queryIndex = mapToSortedUniqQuery[queries[i]];
for (int j = 1; j <= n - l + 1; j++) {
if (j > 1) {
cout << " ";
}
cout << ans[queryIndex][j];
}
cout << endl;
}
}
Compilation message (stderr)
lot.cpp: In function 'int main()':
lot.cpp:57:33: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
57 | for (int qIndex = 1; qIndex <= sortedUniqQueries.size(); qIndex++) {
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |