Codeforces940F Machine Learning (with Huo Mo team)

Topic link: Machine Learning

general idea

given a length of nnsequence of n , iii elements arewi w_iwi.

There are two operations:
1 l rdefine ci c_iciis the number of occurrences iiNumber of digits of i times. Query [ l , r ] [l, r][l,r] m e x ( { c 0 , c 1 , . . , c 1 0 9 } ) mex(\{ c_0, c_1, .., c_{10^9}\}) mex({ c0,c1,..,c109} ) .
2 a cput the sequenceaaThe number in the a position is modified toccc.

Problem solving ideas

Take the Huo Mo team Isn't this a naked question with the Huo Mo team?

We consider opening a bucket to count the number of occurrences of each value, and the number of occurrences is xxThe number of values ​​of x .

We maintain the interval [ l , r ] [l, r] by Mo team[l,r ] information.

Considering the answer query, I used the range block when I started to do the question . Because of the need to maintain a range block, the complexity was twice as high as others.

Later, when I saw the codes of the big guys on the Internet, they were all directly from 1 11 began to solve violently. So I thought about it, this is indeed feasible.

take into account mex mexThe maximum m e x is actually onlyn \sqrt{n}n .

Because 1 + 2 + 3 + . . . + n = n × ( n − 1 ) 2 1 + 2 + 3 + ... + n = \frac{n \times (n - 1)}{2}1+2+3+...+n=2n×(n1)

AC code

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 1; i <= (n); ++i)
using namespace std;
typedef long long ll;
const int N = 1E5 + 10; int B;
vector<int> v(1, -0x3f3f3f3f);
int find(int x) {
    
     return lower_bound(v.begin(), v.end(), x) - v.begin(); }

int w[N];
pair<int, int> change[N];
struct mo {
    
    
	int l, r, t, id;
	bool operator< (const mo& T) const {
    
    
		if (l / B != T.l / B) return l < T.l;
		if (r / B != T.r / B) return r < T.r;
		return t < T.t;
	}
}; vector<mo> area;


int res[N];
int L = 1, R = 0, T = 0;
int cou[N << 1], num[N];
void add(int c) {
    
     --num[cou[c]], ++num[++cou[c]]; }
void sub(int c) {
    
     --num[cou[c]], ++num[--cou[c]]; }
void modify(int t) {
    
    
	auto& [a, c] = change[t];
	if (L <= a and R >= a) {
    
    
		sub(w[a]), add(c);
	}
	swap(w[a], c);
}
int ask() {
    
    
	int res = 1;
	while (num[res]) res++;
	return res;
}

int main()
{
    
    
	int n, m; cin >> n >> m;
	B = pow(n, 2.0 / 3);
	rep(i, n) scanf("%d", &w[i]), v.push_back(w[i]);

	int version = 0;
	rep(i, m) {
    
    
		int tp; scanf("%d", &tp);
		if (tp == 1) {
    
    
			int l, r; scanf("%d %d", &l, &r);
			area.push_back({
    
     l, r, version, i });
		}
		else {
    
    
			int a, c; scanf("%d %d", &a, &c);
			change[++version] = {
    
     a, c };
			v.push_back(c);
		}
	}
	sort(area.begin(), area.end());

	sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end());
	rep(i, n) w[i] = find(w[i]);
	rep(i, version) change[i].second = find(change[i].second);


	for (auto& [l, r, t, id] : area) {
    
    
		while (l < L) add(w[--L]);
		while (r > R) add(w[++R]);
		while (L < l) sub(w[L++]);
		while (R > r) sub(w[R--]);
		while (t < T) modify(T--);
		while (T < t) modify(++T);

		res[id] = ask();
	}

	rep(i, m) if (res[i]) printf("%d\n", res[i]);

	return 0;
}

END

Guess you like

Origin blog.csdn.net/weixin_45799835/article/details/121344975