带修莫队求助,块长、输入输出都对但是就是TLE 70分
查看原帖
带修莫队求助,块长、输入输出都对但是就是TLE 70分
558911
empty_楼主2022/9/27 10:20

# include<iostream>
# include<bits/stdc++.h>
using namespace std;
//# define int long long
# define endl "\n"
namespace IO {
	template<class T>void input(T&x) {
		bool f;
		char c;
		for (f = 0; !isdigit(c = getchar());)if (c == '-')f = 1;
		for (x = 0; isdigit(c); c = getchar())x = (x << 1) + (x << 3) + (c^'0');
		if (f)x = -x;
	}
	template<class T>void output(T x, char c = 10) {
		static char f[97];
		int pf = 0;
		if (!x)return putchar('0'), putchar(c), void();
		if (x < 0)x = -x, putchar('-');
		while (x)f[pf++] = x % 10 + '0', x /= 10;
		while (pf)putchar(f[--pf]);
		putchar(c);
	}
} using namespace IO;
const int N = 1e6 + 10;
int c[N];
int pos[N];
int qnum, cnum;
struct change {
	int pos, val;
} ch[N];
struct node {
	int l, r;
	int pre, id;
} Q[N];

bool cmp(node& a, node& b) {
	return (pos[a.l] ^ pos[b.l]) ? pos[a.l] < pos[b.l] : (pos[a.r] ^ pos[b.r]) ? pos[a.r] < pos[b.r] : a.pre < b.pre;
}
int cnt[N];
int ans = 0;
void add(int p) {
	ans += !cnt[c[p]]++;
}
void del(int p) {
	ans -= !--cnt[c[p]];
}

void modify(int now, int i) {
	if (ch[now].pos >= Q[i].l && ch[now].pos <= Q[i].r) {
		if (--cnt[c[ch[now].pos]] == 0) ans--;
		if (++cnt[ch[now].val] == 1) ans++;
	}
	swap(ch[now].val, c[ch[now].pos]);
}
int res[N];
void solve() {
	int n, m;
	input(n), input(m);
	int block = pow(n,0.66666667);
	for (int i = 1; i <= n; ++i) {
		input(c[i]);
		pos[i] = i - 1 / block + 1;
	}

	while (m--) {
		char op[5];
		scanf("%s", op);
		if (op[0] == 'Q') {
			int x, y;
			input(x), input(y);
			Q[++qnum] = {x, y, cnum, qnum};
		} else {
			int p;
			int val;
			input(p), input(val);
			ch[++cnum] = {p, val};
		}
	}
	sort(Q + 1, Q + 1 + qnum, cmp);
	int l = 1, r = 0, now = 0;
	for (int i = 1; i <= qnum; ++i) {
		while (l < Q[i].l) del(l++);
		while (l > Q[i].l) add(--l);
		while (r < Q[i].r) add(++r);
		while (r > Q[i].r) del(r--);
		while (now < Q[i].pre) modify(++now, i);
		while (now > Q[i].pre) modify(now--, i);
		res[Q[i].id] = ans;
	}

	for (int i = 1; i <= qnum; ++i) {
		output(res[i]);
	}
}
int tt;
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	tt = 1;


//	cin >> tt;
	while (tt--)solve();


	return 0;
}

求助,块长、输入输出都对但是就是TLE 7,8,9,10,11 有无大佬帮忙康康

2022/9/27 10:20
加载中...