参考博客传送门①
参考博客传送门②
题单的传送门
(注意巧妙使用玄学卡常技巧,然后指针必须能够O(1)移动)
文章目录
- 普通莫队
-
-
- [SPOJ - DQUERY](https://vjudge.net/problem/SPOJ-DQUERY)
- [P2709 小B的询问](https://www.luogu.com.cn/problem/P2709)
- [P1494 [国家集训队]小Z的袜子](https://www.luogu.com.cn/problem/P1494)
- [CF617E XOR and Favorite Number](https://www.luogu.com.cn/problem/CF617E)
- [P3709 大爷的字符串题](https://www.luogu.com.cn/problem/P3709)
-
- 带修改的莫队
- 树上莫队
普通莫队
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} } C2len∑C2cnt[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)}