关于普及组C题
为啥怎么看都像道莫队板子,我写了个莫队只给我40分。。。
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
struct Query {
int left; // The Left Point Of The Query
int right; // The Right Point
int k;
int id;
} q[N];
int block, num, pos[N], L[N], R[N];
int cnt[N];
bool cmp(Query x, Query y)
{
if (pos[x.left] == pos[y.left]) {
if (pos[x.left] % 2) {
return x.right > y.right;
} else {
return x.right < y.right;
}
} else {
return pos[x.left] < pos[y.left];
}
}
int ans;
int a[N];
inline void Add(int x)
{
if (cnt[a[x]] == a[x] - 1) {
ans++;
}
cnt[a[x]]++;
}
inline void Del(int x)
{
cnt[a[x]]--;
if (cnt[a[x]] == a[x] - 1) {
ans--;
}
}
int n, m;
long long answer[N];
long long c[N][5];
void init() {
for (int i = 0; i <= n; i++) {
c[i][0] = 1;
for (int j = 1; j <= i; j++)
c[i][j] = c[i - 1][j] + c[i - 1][j - 1];
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
init();
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
cin >> m;
for (int i = 1; i <= m; i++) {
cin >> q[i].left >> q[i].right >> q[i].k;
q[i].id = i;
}
block = sqrt(n);
num = (n - 1) / block + 1;
// for (int i = 1; i <= num; i++) {
// L[i] = (i - 1) * block + 1;
// R[i] = i * block;
// }
// R[num] = n;
for (int i = 1; i < num; i++) {
for (int j = (i - 1) * block + 1; j <= i * block; j++) {
pos[j] = i;
}
}
for (int i = (num - 1) * block + 1; i <= n; i++) {
pos[i] = num;
}
sort(q + 1, q + m + 1, cmp);
int l = 1, r = 0;
for (int i = 1; i <= m; i++) {
while (l > q[i].left)
Add(--l);
while (l < q[i].left)
Del(l++);
while (r > q[i].right)
Del(r--);
while (r < q[i].right)
Add(++r);
answer[q[i].id] = c[ans][q[i].k];
}
for (int i = 1; i <= m; i++) {
cout << answer[i] << endl;
}
return 0;
}
查看12道真题和解析