tle90 求调教
查看原帖
tle90 求调教
550775
rsy_楼主2025/1/19 15:01

大样例下下来 3s+,我的常数这么大了吗。

#include <bits/stdc++.h>
#define lb(x) (x&-x)
#define L(i,j,k) for(int i=(j);i<=(k);++i)
#define R(i,j,k) for(int i=(j);i>=(k);--i)

using namespace std;
using i64 = long long;

typedef pair<int, int> pii;
typedef long long ll;
typedef unsigned long long ull;
void chmin(int &x, int c) {x = min(x, c);}
void chmax(int &x, int c) {x = max(x, c);}

const int maxn = 3e5 + 10, mod = 998244353;
int tong[maxn], tong2[maxn], bl[maxn];
int N, M, A[maxn], res[maxn];
int lpos[maxn], rpos[maxn];
struct qwq {int l, r, id;} q[maxn];
void add (int x) {
	tong[x] ++ ;
}
void del (int x) {
	tong[x] -- ;
}

void solve() {
	cin >> N >> M;
	for (int i = 1; i <= N; i ++ )
		cin >> A[i];
	for (int i = 1; i <= M; i ++ ) {
		cin >> q[i].l >> q[i].r;
		q[i].id = i;
	}
const int B = 450;
	for (int i = 1; i <= N; i ++ ) {
		bl[i] = (i + B - 1) / B;
		if (!lpos[bl[i]]) lpos[bl[i]] = i;
		rpos[bl[i]] = max (rpos[bl[i]], i);
	}
	sort (q + 1, q + 1 + M, [](qwq i, qwq j){
		if (bl[i.l] == bl[j.l]) {
			return i.r < j.r;
		}
		return i.l < j.l;
	});
	int lstk = -114514;
	int mex = 0;
	int l = 1, r = 0;
	for (int i = 1; i <= M; i ++ ) {
		if (bl[q[i].l] == bl[q[i].r]) {
			for (int j = q[i].l; j <= q[i].r; j ++ )
				tong2[A[j]] ++ ;
			int Mex = 0;
			while (tong2[Mex]) Mex ++ ;
			for (int j = q[i].l; j <= q[i].r; j ++ )
				tong2[A[j]] -- ;
			res[q[i].id] = Mex; continue;
		}
		if (bl[q[i].l] != lstk) {
			memset (tong, 0, sizeof tong);
			l = rpos[bl[q[i].l]] + 1;
			r = rpos[bl[q[i].l]], mex = 0;
			lstk = bl[q[i].l];
		}
		while (r < q[i].r) {
			r ++ ;
			add (A[r]);
		}
		while (tong[mex]) mex ++ ;
		int Mex = mex;
		while (l > q[i].l) {
			l -- ;
			add (A[l]);
		}
		while (tong[Mex]) Mex ++ ;
		res[q[i].id] = Mex;
		while (l < rpos[bl[q[i].l]] + 1) {
			del (A[l]);
			l ++ ;
		}
	}
	for (int i = 1; i <= M; i ++ ) {
		cout << res[i] << '\n';
	}
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int T = 1;
  while (T--)solve();
  return 0;
}
2025/1/19 15:01
加载中...