这题纯分块能过吗
查看原帖
这题纯分块能过吗
743373
Vitamin_B楼主2024/8/10 10:34

rt,如果纯分块能过,那能帮我卡个常嘛qaq

//# pragma GCC optimize("Ofast,no-stack-protector")
//
//# pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

# include <bits/stdc++.h>

# define I return

# define AK 0

# define IOI ;

using namespace std;

typedef long long ll;

typedef pair <int, int> pii;

int n, m, a[133340], x, y, t, st[120], ed[120], s[120], col[133340], vis[120][1000005], mp[1000005];

char op;

void init () {

	t = sqrt (n);

	for (int i = 1; i <= t; ++ i)
		st[i] = n / t * (i - 1) + 1, ed[i] = n / t * i;

	ed[t] = n;

	for (int i = 1; i <= t; ++ i)
		for (int j = st[i]; j <= ed[i]; ++ j) {

			col[j] = i;

			if (! vis[i][j])
				++ s[i];

			++ vis[i][j];

		}

	return ;

}

void change (const int& x, const int& y) {

	const int c = col[x];

	if (! -- vis[c][x])
		-- s[c];

	if (! vis[c][a[x] = y])
		++ s[c];

	++ vis[c][y];

	return ;

}

inline int find (const int& l, const int& r) {

	int sum = 0;

	const int cl = col[l], cr = col[r];

	if (cl == cr) {

		for (int i = l; i <= r; ++ i)
			if (! mp[a[i]])
				mp[a[i]] = 1, ++ sum;

		for (int i = l; i <= r; ++ i)
			mp[a[i]] = 0;

		return sum;

	}

	for (int i = l; i <= ed[cl]; ++ i)
		if (! mp[a[i]])
			mp[a[i]] = 1, ++ sum;

	for (int i = l; i <= ed[cl]; ++ i)
		mp[a[i]] = 0;

	for (int i = st[cr]; i <= r; ++ i)
		if (! mp[a[i]])
			mp[a[i]] = 1, ++ sum;

	for (int i = st[cr]; i <= r; ++ i)
		mp[a[i]] = 0;

	for (int i = cl + 1; i < cr; ++ i)
		sum += s[i];

	return sum;

}

int main () {

	ios::sync_with_stdio (0);

	cin.tie (0);

	cout.tie (0);

	cin >> n >> m;

	for (int i = 1; i <= n; ++ i)
		cin >> a[i];

	while (m --) {

		cin >> op >> x >> y;

		if (op == 'R')
			change (x, y);
		else
			cout << find (x, y) << '\n';

	}

	I AK IOI

}
2024/8/10 10:34
加载中...