莫队,为什么RE了?
查看原帖
莫队,为什么RE了?
126972
Kevinx楼主2024/4/12 15:17

rt,貌似是数组越界?

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e5 + 20;
ll n, m, a[N], ans[N];
ll siz = 0;
ll len = 0, b[N], pos[N];
ll cnt[N];
struct query{
	ll l, r, id;
}q[N];
bool cmp(query a, query b) {
	return a.l/siz==b.l/siz ? a.r < b.r : a.l < b.l;
}

void add(ll x, ll pre, ll i) {if(cnt[x] == pre) ans[i]--; cnt[x]++; if(cnt[x] == pre) ans[i]++; }
void del(ll x, ll pre, ll i) {if(cnt[x] == pre) ans[i]--; cnt[x]--; if(cnt[x] == pre) ans[i]++;}

int main (){
	scanf("%d%d", &n, &m);
	for(int i = 1; i <= n; i++) {
		scanf("%d", &a[i]);
		b[i] = a[i];
	}
	siz = ceil((double)n / sqrt(n));

	sort(b + 1, b + n + 1);
	len = unique(b + 1, b + n + 1) - b - 1;
	for(int i = 1; i <= n; i++) {
		pos[i] = lower_bound(b + 1, b + len + 1, a[i]) - b;
		swap(pos[i], a[i]);
//		cout << pos[i] << "->" << a[i] << endl;
	}
	
	
	for(int i = 1; i <= m; i++) {
		ll l, r;
		scanf("%d%d", &l, &r);
		q[i] = (query){l, r, i};
	}
	
	sort(q + 1, q + m + 1, cmp);
//	for(int i = 1; i <= m; i++) {
//		ll ql = q[i].l, qr = q[i].r, id = q[i].id;
//	cout << ql << " " << qr << " " << id << endl;
//}
	ll l = 1, r = 0;
	for(int i = 1; i <= m; i++) {
		ll ql = q[i].l, qr = q[i].r, id = q[i].id;
		while(ql < l) add(a[l-1], pos[l-1], id) , l--;
		while(r < qr) add(a[r+1], pos[r+1], id), r++;
		while(l < ql) del(a[l], pos[l], id), l++;
		while(qr < r) del(a[r], pos[r], id), r--;
		ans[q[i+1].id] = ans[id];
	}
	for(int i = 1; i <= m; i++) printf("%d\n",  ans[i]);
	return 0;
} 
2024/4/12 15:17
加载中...