跳到正文

个人记录

1212

璃纸鸢约 5 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 5 分钟

版本

  1. v1当前
C++
#include<bits/stdc++.h>
#define pii pair<int, int>
#define ll long long
#define p first
#define x second
#define query qu
#define qsize bl
#define int long long

using namespace std;

inline int read();
template <typename T> inline void write(T x);

const int N = 4e6 + 5;

int n, m, qcnt, pcnt, bl, answer;
int c[N], t[N], ans[N];

pii opt[N];

char ch;

struct qu
{
	int id, t, l, r;
//	  bool operator<(query b) const {
//    if (l / qsize != b.l / qsize) {
//      return l / qsize > b.l / qsize;
//    } else if (r / qsize != b.r / qsize) {
//      return r / qsize > b.r / qsize;
//    } else {
//      return t > b.t;
//    }
//  }
	inline bool operator <(const qu &x) const{
		if (this->l /bl != x.l / bl) return this->l / bl > x.l / bl;
		else if (this->r / bl != x.r / bl) return this->r / bl > x.r / bl;
		else return this->t > x.t;
	}
}qa[N];

inline void add(int x) {if(!t[x]) answer++; t[x]++;}
inline void del(int x) {t[x]--; if(!t[x]) answer--;}

signed main(){
	n = read(), m = read(); bl = (int)(pow(n, 2.0 / 3.0));
	for (int i = 1; i <= n; i++) c[i] = read();
	for (int i = 1, l, r; i <= m; i++)
	{
		cin >> ch;
		if (ch == 'Q') {l = read(), r = read(), ++qcnt, qa[qcnt] = (qu){qcnt, pcnt, l, r};}
		else if (ch == 'R') {l = read(), r =read(), opt[++pcnt] = {l, r};}
	}
	sort(qa + 1, qa + 1 + qcnt);
//	for (int i = 1; i <= qcnt; i++) cout << qa[i].l << " " << qa[i].r << " " << qa[i].id << '\n';
	for (ll i = 1, l = 1, r = 0, t = 0; i <= qcnt; i++)
	{
//		cout << l << " " << r << '\n';
		while(l > qa[i].l) add(c[--l]); while(r < qa[i].r) add(c[++r]);
		while(l < qa[i].l) add(c[l++]); while(r > qa[i].r) del(c[r--]);
		while(t < qa[i].t) {++t; if(opt[t].p >= l && opt[t].p <= r) add(opt[t].x), del(c[opt[t].p]); swap(c[opt[t].p], opt[t].x);}
		while(t > qa[i].t) {if(opt[t].p >= l && opt[t].p <= r) add(opt[t].x), del(c[opt[t].p]); swap(c[opt[t].p], opt[t].x); --t;}
		ans[qa[i].id] = answer;
	}
	for (int i = 1; i <= qcnt; i++) cout << ans[i] << '\n';
	return 0;
}

/*
6 5
1 2 3 4 5 5
Q 1 4
Q 2 6
R 1 2
Q 1 4
Q 2 6
*/
inline int read()
{
	int x = 0, f = 1;
	char c = getchar();
	while(c < '0' || c > '9')
	{
		if (c == '-') f = 0;
		c = getchar();
	}
	while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
	return f ? x : -x;
}

template <typename T> inline void write(T x)
{
	if (x < 0) x = -x, putchar('-');
	if (x > 9) write(x / 10);
	putchar(x % 10 + 48);
}
原文