目前只会普通莫队 qwq,带修莫队只会一点,回滚莫队只会板子,树上莫队不是很会。
莫队这玩意操作是很简单的,基本的思想是将查询离线下来按照一定的次序进行排序然后逐个进行处理,并且通过数列分块的技巧将时间复杂度巧妙的降到 左右。
对于基本的莫队,一般可以处理求区间不同种类的数的个数、区间众数的出现次数、区间 这些问题,在面对一些问题的时候巧妙的将问题转化为上面莫队可以处理的问题便可以使用莫队解决。
注意很多莫队的题目要求我们对离散化、STL 这一类工具和技巧比较熟悉,否则未必能够成功转化。
接下来就是例题了:
P3901 数列找不同
现有数列 , 个询问 ,询问 是否互不相同。
对每个询问输出一行,Yes 或 No。
对于 的数据,,,。
解题思路: 考虑把区间每个数互不相同转化成区间不同种类的数的个数为 。所以问题就变为求区间不同种类的数的个数,然后进行判断,可以使用莫队进行解决。
不过这里提一嘴,这个题目不仅可以使用莫队解决,同样是离线,参考类似 HH 的项链这个题目的树状数组做法进行微调也是可以通过这个题目的。
code :
#include <bits/stdc++.h>
#define ll long long
#define db double
#define vec vector
#define pll pair<ll, ll>
#define mkp make_pair
#define endl "\n"
using namespace std;
const ll mod = 998244353;
namespace fastio {
char buf[1 << 21], *p1 = buf, *p2 = buf;
const ll getc() {
return p1 == p2 && ( p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1 ++;
}
const ll read() {
ll x = 0, f = 1;
char ch = getc();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1; ch = getc();
}
while (ch >= '0' && ch <= '9') {
x = (x << 1) + (x << 3) + (ch ^ 48), ch = getc();
}
return x * f;
}
const void write(ll x) {
if (x < 0) {
putchar('-'), x = -x;
}
ll sta[35], top = 0;
do {
sta[top++] = x % 10, x /= 10;
} while (x);
while (top) putchar(sta[--top] + 48);
}
}
#define rd fastio::read
#define wt fastio::write
#define gc fastio::getc
ll n, m, q, bl, a[200005], cnt[200005], ask[200005]; string s;
ll opt, l, r, ans = 0;
struct query {
ll l, r, id;
friend bool operator < (query x, query y) {
ll xb = x.l / bl, yb = y.l / bl;
if (xb == yb) {
return x.r < y.r;
}
return xb < yb;
}
} qry[200005] ;
void add(ll x) {
ans += (cnt[a[x]] == 0), cnt[a[x]] ++;
}
void del(ll x) {
ans -= (cnt[a[x]] == 1), cnt[a[x]] --;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
ll i, j, k, x, y, z, res = 0, now;
n = rd(), m = rd(), bl = sqrt(n);
for (i = 1; i <= n; i++) a[i] = rd();
for (i = 1; i <= m; i++) qry[i].l = rd(), qry[i].r = rd(), qry[i].id = i;
sort(qry + 1, qry + m + 1);
ll l = 1, r = 0;
for (i = 1; i <= m; i++) {
while (l > qry[i].l) add(--l);
while (r < qry[i].r) add(++r);
while (l < qry[i].l) del(l++);
while (r > qry[i].r) del(r--);
ask[qry[i].id] = (ans == qry[i].r - qry[i].l + 1);
}
for (i = 1; i <= m; i++) puts(ask[i] ? "Yes" : "No");
return 0;
}P2709 小B的询问
小B 有一个长为 的整数序列 ,值域为 。 他一共有 个询问,每个询问给定一个区间 ,求:
其中 表示数字 在 中的出现次数。 小B请你帮助他回答询问。
对于 的数据,。
解题思路: 题目已经说了就是要知道区间每个数出现次数的平方和。那么对于莫队的 add 和 del 函数,考虑 所带来的增量和 所带来的减量。即:
最后用一下莫队,做完了。
code :
#include <bits/stdc++.h>
#define ll long long
#define db double
#define vec vector
#define pll pair<ll, ll>
#define mkp make_pair
#define endl "\n"
using namespace std;
const ll mod = 998244353;
namespace fastio {
char buf[1 << 21], *p1 = buf, *p2 = buf;
const ll getc() {
return p1 == p2 && ( p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1 ++;
}
const ll read() {
ll x = 0, f = 1;
char ch = getc();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1; ch = getc();
}
while (ch >= '0' && ch <= '9') {
x = (x << 1) + (x << 3) + (ch ^ 48), ch = getc();
}
return x * f;
}
const void write(ll x) {
if (x < 0) {
putchar('-'), x = -x;
}
ll sta[35], top = 0;
do {
sta[top++] = x % 10, x /= 10;
} while (x);
while (top) putchar(sta[--top] + 48);
}
}
#define rd fastio::read
#define wt fastio::write
#define gc fastio::getc
ll n, m, q, bl, a[200005], cnt[200005], ask[200005]; string s;
ll opt, l, r, ans = 0;
struct query {
ll l, r, id;
friend bool operator < (query x, query y) {
ll xb = x.l / bl, yb = y.l / bl;
if (xb == yb) {
return x.r < y.r;
}
return xb < yb;
}
} qry[200005] ;
void add(ll x) {
ans += (2 * cnt[a[x]] + 1), cnt[a[x]] ++;
}
void del(ll x) {
ans -= (2 * cnt[a[x]] - 1), cnt[a[x]] --;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
ll i, j, k, x, y, z, res = 0, now;
n = rd(), m = rd(), k = rd(), bl = sqrt(n);
for (i = 1; i <= n; i++) a[i] = rd();
for (i = 1; i <= m; i++) qry[i].l = rd(), qry[i].r = rd(), qry[i].id = i;
sort(qry + 1, qry + m + 1);
ll l = 1, r = 0;
for (i = 1; i <= m; i++) {
while (l > qry[i].l) add(--l);
while (r < qry[i].r) add(++r);
while (l < qry[i].l) del(l++);
while (r > qry[i].r) del(r--);
ask[qry[i].id] = ans;
}
for (i = 1; i <= m; i++) wt(ask[i]), puts("");
return 0;
}