关于普及组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;
}

全部评论
你看看你的组合数预处理,是O(n^2)的...... 还有组合数 j 循环到 4 即可,否则这会 RE ........ 莫队本身不知道对不对,反正我莫队过了
点赞 回复 分享
发布于 2023-10-05 17:29 浙江

相关推荐

11-18 09:44
Java
小白也想要offer:简历别放洋屁,搞不还还放错了,当然你投外企除外,以上纯属个人观点
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务