D-query SPOJ - DQUERY(莫队算法)

这题的解法并不唯一,在网上看到的主要有主席树和莫队算法两种,但主席树的代码普遍比较长而且较难理解。而莫队算法代码量短而且叫容易理解。莫队算法也是一种暴力的算法,复杂度比纯暴力快了不少,但比主席树慢了一点。
在这里插入图片描述
在这里插入图片描述
题目大意:在数列a中,找出[L,R]中有多少个不同的数。

//#include <bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#define lowbit(x) ((x) & -(x))
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
const int maxn = 1e6;
typedef long long ll;
using namespace std;
const ll mod = 1e9 + 7;
const double pi = acos(-1.0);
const double c = 0.57721566490153286060651209;
const double esp = 1e-8;
const int INF = 0x7fffffff;
inline int read() {
    int x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9') {
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        x = (x << 1) + (x << 3) + (ch ^ 48);
        ch = getchar();
    }
    return x * f;
}
int n, q, sz, ans;
int a[maxn], block[maxn], vis[maxn], Ans[maxn];
struct node{
    int l, r, id;
}Q[maxn];
bool cmp(node a, node b){		// 莫队算法的核心之一,基本可以作为模板
    if (block[a.l] == block[b.l])
        return a.r < b.r;
    return a.l < b.l;
}
void add(int x){
    if (vis[a[x]] == 0)
        ans ++;
    vis[a[x]] ++;
}
void del(int x){
    vis[a[x]] --;
    if (vis[a[x]] == 0)
        ans --;
}
int main() {
    //ios::sync_with_stdio(false);
    //freopen("text.txt","r",stdin);
    //freopen("out1.txt","w",stdout);
    scanf("%d",&n);
    sz = sqrt(n);
    for (int i = 1; i <= n; i ++){
        scanf("%d",&a[i]);
        block[i] = (i - 1) / sz + 1;	// 分块操作
    }
    scanf("%d",&q);
    for (int i = 1; i <= q; i ++){
        scanf("%d %d",&Q[i].l,&Q[i].r);
        Q[i].id = i;		// 记录查询的第几次
    }
    sort(Q+1,Q+1+q,cmp);	// 对区间进行排序
    int l = 1, r = 0;
    for (int i = 1; i <= q; i ++){   // 莫队算法的核心,先扩后缩。add函数和del函数根据实际情况改变,这也是莫队算法的难点。 
        while (l > Q[i].l)
            add(--l);
        while (r < Q[i].r)
            add(++r);
        while (l < Q[i].l)  
            del(l ++);
        while (r > Q[i].r)
            del(r --);
        Ans[Q[i].id] = ans;
    }
    for (int i = 1; i <= q; i ++)
        printf("%d\n",Ans[i]);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
洛谷的SPOJ需要注册一个SPOJ账号并进行绑定才能进行交题。您可以按照以下步骤进行注册: 1. 打开洛谷网站(https://www.luogu.com.cn/)并登录您的洛谷账号。 2. 在网站顶部导航栏中找到“题库”选项,将鼠标悬停在上面,然后选择“SPOJ”。 3. 在SPOJ页面上,您会看到一个提示,要求您注册SPOJ账号并进行绑定。点击提示中的链接,将会跳转到SPOJ注册页面。 4. 在SPOJ注册页面上,按照要求填写您的用户名、密码和邮箱等信息,并完成注册。 5. 注册完成后,返回洛谷网站,再次进入SPOJ页面。您会看到一个输入框,要求您输入刚刚注册的SPOJ用户名。输入用户名后,点击“绑定”按钮即可完成绑定。 现在您已经成功注册并绑定了SPOJ账号,可以开始在洛谷的SPOJ题库上刷题了。祝您顺利完成编程练习!\[1\]\[2\] #### 引用[.reference_title] - *1* *3* [(洛谷入门系列,适合洛谷新用户)洛谷功能全解](https://blog.csdn.net/rrc12345/article/details/122500057)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [luogu p7492 序列](https://blog.csdn.net/zhu_yin233/article/details/122051384)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值