莫队WA0求助
查看原帖
莫队WA0求助
374769
Epi4any楼主2023/1/7 18:27
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10;
struct node {
	int l, r, id;
} q[maxn];
int n, Q, b, pos[maxn], ans[maxn], cnt[maxn], a[maxn];
bool cmp(node x, node y) {
	if (pos[x.l] != pos[y.l]) {
		return pos[x.l] < pos[y.l];
	}
	if (pos[x.l] & 1) return x.r > y.r;
	return x.r < y.r;
}
int ANS;
void add(int x) {
	cnt[a[x]]++;
	if (cnt[a[x]] == 1) ANS++;
}
void del(int x) {
	cnt[a[x]]--;
	if (cnt[a[x]] == 0) ANS--;
}
void solve() {
	int l = 1, r = 0;
	sort(q, q + 1 + Q, cmp);
	for (int i = 1; i <= Q; i++) {
		const node &tmp = q[i];
		while (l > tmp.l) add(--l);
		while (r < tmp.r) add(++r);
		while (l < tmp.l) del(l++);
		while (r > tmp.r) del(r--);
		ans[tmp.id] = ANS;
	}
}
int main() {
	cin >> n >> Q;
	b = sqrt(n);
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		pos[i] = (i - 1) / b + 1;
	}
	for (int i = 1; i <= Q; i++) {
		cin >> q[i].l >> q[i].r;
		q[i].id = i;
	}
	sort(q + 1, q + 1 + Q, cmp);
	solve();
	for (int i = 1; i <= Q; i++) {
		if(ans[i]==q[i].r-q[i].l+1) cout<<"Yes"<<endl;
		else cout<<"No"<<endl;
	}
	return 0;
}
2023/1/7 18:27
加载中...