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;
const long long inf = (long long) 1e18 + 10;
const int inf1 = (int) 1e9 + 10;
// #define int long long
#define dbl long double
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()
#define maxn 10200
int n, l, a[maxn], ps[2020][2020], qtd[2020][2020];
vector<int> pos[maxn];
long long pw[maxn], hsh[maxn], fq[1000100];
long long mod = 1e6 + 7;
void prephash() {
pw[0] = 1;
pw[1] = 5131;
for(int i = 2; i <= n; i++) {
pw[i] = pw[i-1]*pw[1];
pw[i]%= mod;
}
for(int i = 1; i <= n; i++) {
hsh[i] = hsh[i-1]*pw[1] + a[i];
hsh[i]%= mod;
}
}
long long subhash(int l, int r) {
int x = (hsh[r] - hsh[l-1]*pw[r-l+1])%mod;
if(x < 0) x += mod;
return x;
}
void solve() {
cin >> n >> l;
vector<int> cc;
for(int i = 1; i <= n; i++) {
cin >> a[i];
cc.pb(a[i]);
}
sort(all(cc));
cc.erase(unique(all(cc)),cc.end());
if(n <= 2000) {
for(int i = 1; i <= n; i++) {
a[i] = upper_bound(all(cc),a[i]) - cc.begin();
pos[a[i]].pb(i-l+1);
}
for(int i = 1; i <= n; i++) {
for(auto x : pos[i]) {
for(auto y : pos[i]) {
if(x == y) continue;
// cout << " " << x << " " << y << endl;
int x1 = x;
int y1 = y;
int x2 = x+l;
int y2 = y+l;
if(x1 <= 0) {
y1+= 1-x1;
x1 = 1;
}
if(y1 <= 0) {
x1+= 1-y1;
y1 = 1;
}
ps[x1][y1]++;
if(x2 <= n && y2 <= n) ps[x2][y2]--;
// cout << x1 << " " << y1 << " " << x2 << " " << y2 << endl;
}
}
}
for(int i = 1; i <= n; i++) {
//coordenada que comeca em (1,i);
int x = 2;
int y = i+1;
while(y <= n) {
ps[x][y]+= ps[x-1][y-1];
x++;
y++;
}
}
for(int i = 2; i <= n; i++) {
//coordenada que comeca em (i,1);
int x = i+1;
int y = 2;
while(x <= n) {
ps[x][y]+= ps[x-1][y-1];
x++;
y++;
}
}
// for(int i = 1; i <= n; i++) {
// for(int j = 1; j <= n; j++) {
// cout << i << " " << j << " " << ps[i][j] << " " << endl;
// }
// }
for(int i = 1; i <= n-l+1; i++) {
for(int j = 1; j <= n-l+1; j++) {
if(i == j) continue;
qtd[i][l-ps[i][j]]++;
}
for(int j = 1; j <= l; j++) {
qtd[i][j]+= qtd[i][j-1];
}
}
int q; cin >> q;
while(q--) {
int val; cin >> val;
for(int i = 1; i <= n-l+1; i++) {
cout << qtd[i][val] << " ";
}cout << endl;
}
}
else {
for(int i = 1; i <= n; i++) {
a[i] = upper_bound(all(cc),a[i]) - cc.begin();
prephash();
}
for(int i = 1; i <= n-l+1; i++) {
a[i] = subhash(i,i+l-1);
fq[a[i]]++;
}
int q; cin >> q >> q;
for(int i = 1; i <= n-l+1; i++) {
cout << fq[a[i]]-1 << " ";
}cout << endl;
}
}
int32_t main() {
ios::sync_with_stdio(false); cin.tie(0);
// freopen("in.in", "r", stdin);
//freopen("out.out", "w", stdout);
int tt = 1;
// cin >> tt;
while(tt--) solve();
}
# | 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... |