莫队入门题单

本文详细介绍了莫队算法的应用,包括普通莫队、带修改的莫队和树上莫队。通过实例解析了如何解决区间不同数的个数、随机取数概率、子段异或结果、字符串众数等经典问题,并给出了相应的题单供练习。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考博客传送门①
参考博客传送门②
题单的传送门
(注意巧妙使用玄学卡常技巧,然后指针必须能够O(1)移动)

普通莫队

SPOJ - DQUERY

题意:给定正整数序列,m次询问,每次询问区间不同数的个数。
(莫队板题)

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
const int maxn = 1e6+10;
const int mx = 40;
const int mod = 1e9+7;
const ll inf = 34359738370;
const int INF = 1e9+7;
const double pi = acos(-1.0);
//每次求 [l,r] 不同的数的个数
int n,m,a[maxn];
int block;
int color[maxn];
int ans;
int ANS[maxn];
struct node 
{
   
    int l,r,id;
}q[maxn];
inline bool cmp(const node &a,const node &b) //奇偶排序
{
   
    return (a.l/block)^(b.l/block)?a.l<b.l:(((a.l/block)&1)?a.r<b.r:a.r>b.r);
}
inline void del(int x) 
{
   
    color[a[x]]--;
    if(color[a[x]] == 0) ans--;
}
inline void add(int x) 
{
   
    color[a[x]]++;
    if(color[a[x]] == 1) ans++;
}
int main()
{
   
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d",a+i);
    scanf("%d",&m);
    for(int i=1;i<=m;i++) 
    {
   
        scanf("%d %d",&q[i].l,&q[i].r);
        q[i].id=i;
    }
    block=n/sqrt(m*2/3);
    sort(q+1,q+m+1,cmp);
    int l=q[1].l,r=q[1].l-1;
    for(int i=1;i<=m;i++) 
    {
   
        int ql=q[i].l,qr=q[i].r;
        while(l<ql) del(l++);
        while(r>qr) del(r--);
        while(l>ql) add(--l);
        while(r<qr) add(++r);
        ANS[q[i].id]=ans;
    }
    for(int i=1; i<=m ;i++) {
   printf("%d\n",ANS[i]); }
    return 0;
}

P2709 小B的询问

题意:给定整数序列,每次询问一个区间内的 ∑ l r ( c [ i ] ) 2 \sum_{l}^{r} (c[i])^2 lr(c[i])2,c[i]表示数字i在[l,r]中出现的次数
(莫队板题,维护区间内数的个数,个数的平方数变化产生的贡献)

  • add : n² ->(n+1)² ,答案增加2n+1
  • del : n² ->(n-1)² ,答案增加-2n+1
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define re register
const int maxn = 5e4+10;
const int mx = 40;
const int mod = 1e9+7;
const ll inf = 34359738370;
const int INF = 1e9+7;
const double pi = acos(-1.0);
//给定序列 求[l,r] 中 Σci² ci表示i出现的次数
int n,m,k;
int num[maxn];
int a[maxn];
ll ans=0;
ll ANS[maxn];
struct node 
{
   
    int l,r,id;
}q[maxn];
int block;
inline bool cmp(const node &a,const node &b) //奇偶排序
{
   
    return (a.l/block)^(b.l/block)?a.l<b.l:(((a.l/block)&1)?a.r<b.r:a.r>b.r);
}
inline void del(int x) 
{
   
    ans += (-2*num[a[x]]+1) ;
    num[a[x]]--;
}
inline void add(int x)
{
   
    ans += (2*num[a[x]]+1) ;
    num[a[x]]++;
}

int main()
{
   
    scanf("%d %d %d",&n,&m,&k);
    block=n/sqrt(m*2/3);
    for(re int i=1;i<=n;i++) 
    {
   
        scanf("%d",a+i);
    }
    for(re int i=1;i<=m;i++)
    {
   
        scanf("%d %d",&q[i].l,&q[i].r);
        q[i].id=i;
    }
    sort(q+1,q+m+1,cmp);
    int l=q[1].l,r=q[1].l-1;
    for(re int i=1;i<=m;i++) 
    {
   
        int ql=q[i].l,qr=q[i].r;
        while(l < ql) del(l++);
        while(r > qr) del(r--);
        while(l > ql) add(--l);
        while(r < qr) add(++r);
        ANS[q[i].id]=ans;
    }
    for(int i=1;i<=m;i++) printf("%lld\n",ANS[i]);
    return 0;
}

P1494 [国家集训队]小Z的袜子

题意:给定整数序列,m次询问,每次询问在区间[l,r]中随机取两个数时取中两个相同的数的概率。(最简分数表示)

用cnt[x]表示数x在区间内出现的次数,len表示当前区间长度,那么概率就为:
∑ C 2 c n t [ x ] C 2 l e n \frac{\sum C_{2}^{cnt[x]} }{C_{2}^{len} } C2lenC2cnt[x]

化简: ∑ ( c n t [ x ] 2 − c n t [ x ] ) l e n ∗ ( l e n − 1 ) \frac{\sum (cnt[x]^2 -cnt[x]) }{len*(len-1)}

内容概要:本文将金属腐蚀现象比作游戏角色受到持续伤害(debuff),并采用浓度迁移和损伤方程来建模这一过程。文中首先介绍了浓度迁移的概念,将其比喻为游戏中使角色持续掉血的毒雾效果,并展示了如何利用Numpy矩阵存储浓度场以及通过卷积操作实现浓度扩散。接着引入了损伤方程,用于评估材料随时间累积的损伤程度,同时考虑到材料自身的抗性特性。作者还提供了完整的Python代码示例,演示了如何在一个二维网格环境中模拟24小时内金属表面发生的腐蚀变化,最终得到类似珊瑚状分形结构的腐蚀形态。此外,文章提到可以通过调整模型参数如腐蚀速率、材料抗性等,使得模拟更加贴近实际情况。 适合人群:对材料科学、物理化学感兴趣的科研工作者和技术爱好者,尤其是那些希望通过编程手段深入理解金属腐蚀机制的人群。 使用场景及目标:适用于希望借助数值模拟方法研究金属腐蚀行为的研究人员;可用于教学目的,帮助学生更好地掌握相关理论知识;也可作为工程项目前期评估工具,预测不同条件下金属构件可能遭受的腐蚀损害。 阅读建议:由于文中涉及较多数学公式和编程细节,建议读者具备一定的Python编程基础以及对线性代数有一定了解。对于想要进一步探索该领域的学者来说,可以尝试修改现有代码中的参数设置或者扩展模型维度,从而获得更丰富的研究成果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值