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;
using ll = long long;
#define pb push_back
#define sz(v) ((int)v.size())
const ll inf = 1e16;
const int N = 10000;
int n, l, q;
vector<int> nm, v, ks;
vector<array<int,100>> ans;
int j[N], lst[N], e[N], prec[N+1];
int bs(int x) {
int l, r, m;
l = 0;
r = sz(nm);
while (l < r) {
m = (l+r)/2;
if (nm[m] <= x)
l = m+1;
else
r = m;
}
return l - 1;
}
int bs2(int x) {
int l, r, m;
l = 0;
r = sz(ks);
while (l < r) {
m = (l+r)/2;
if (ks[m] <= x)
l = m+1;
else
r = m;
}
return l;
}
void _add(int x) {
if (j[x] == 0) return;
int d = x;
do {
d -= j[d];
++e[x-d];
} while (j[d]);
}
void _rem(int x) {
if (j[x] == 0) return;
int d = x;
do {
d -= j[d];
--e[x-d];
} while (j[d]);
}
void solve() {
for (int i = 0; i < n; ++i) {
j[i] = 0; lst[i] = -1; e[i] = 0;
}
for (int i = 0; i < n; ++i) {
if (lst[v[i]] != -1)
j[i] = i - lst[v[i]];
lst[v[i]] = i;
}
for (int i = 0; i < l; ++i)
_add(i);
for (int i = 1; i + l - 1 < n; ++i) {
_add(i+l-1); _rem(i-1);
for (int j = 1; j <= i; ++j) {
if (prec[e[j]]) ans[i][prec[e[j]]-1]++;
}
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> l;
v.assign(n, 0);
ans.resize(n-l+1);
for (int i = 0; i < n; ++i) cin >> v[i];
for (int i = 0; i < n - l + 1; ++i)
for (int j = 0; j < 100; ++j) ans[i][j] = 0;
nm = v; sort(nm.begin(), nm.end()); nm.erase(unique(nm.begin(), nm.end()), nm.end());
for (int i = 0; i < n; ++i) v[i] = bs(v[i]);
cin >> q;
vector<int> qq(q);
for (int i = 0; i < q; ++i)
cin>>qq[i];
for (int i = 0; i < q; ++i)
ks.pb(l - qq[i]);
sort(ks.begin(), ks.end()); ks.erase(unique(ks.begin(), ks.end()), ks.end());
for (int i = 0; i <= n; ++i)
prec[i] = bs2(i);
solve();
reverse(v.begin(), v.end());
reverse(ans.begin(), ans.end());
solve();
reverse(ans.begin(), ans.end());
for (int i = sz(ks)-1; i >= 0; --i)
for (int j = 0; j < n - l + 1; ++j)
ans[j][i] += ans[j][i+1];
for (int i = 0; i < q; ++i) {
for (int j = 0; j < n - l + 1; ++j)
cout << ans[j][bs2(l-qq[i])-1] << (j == n-l ? '\n' : ' ');
}
}
# | 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... |