萌新求助
查看原帖
萌新求助
51568
TPR123456楼主2021/3/29 11:57

过了样例但不知道错在哪里了 /kk

不带修部分应该没问题。过了 #11 #12 #13

#include <bits/stdc++.h>
#define D cerr << "Passed [" << __FUNCTION__ << "] on line " << __LINE__ << endl
#define d(x) cerr << #x << " => " << x << endl
#define ms(x) memset(x, 0, sizeof x)
// #define int long long
using namespace std;
int rd() {
    int f = 1, val = 0; char ch = getchar();
    while(!isdigit(ch)) f = (ch == '-' ? -1 : 1), ch = getchar();
    while(isdigit(ch)) val = val * 10 + ch - '0', ch = getchar();
    return f * val;
}

const int N = 1e6 + 5;
int col[N], tot_query, tot_modify;

struct Query {
	int l, r, ti, id, lb, rb, ans;
	bool operator<(const Query& b) const {
		if(lb == b.lb && rb == b.rb) return ti < b.ti;
		if(lb == b.lb) return rb < b.rb;
		return lb < b.lb;
	}
} query[N];

struct Modify {
	int p, col;
} modify[N];

int now;

int buc[N], ans;

void add(int x) {
	if(!buc[col[x]]) ++ans;
	++buc[col[x]];
}

void remove(int x) {
	--buc[col[x]];
	if(!buc[col[x]]) --ans;
}

void moveTime(int x, int i) {
	if(query[i].l <= modify[x].p && modify[x].p <= query[i].r) remove(modify[x].p);
	col[modify[x].p] = modify[x].col;
	if(query[i].l <= modify[x].p && modify[x].p <= query[i].r) add(modify[x].p);
}

void file() {
    freopen("in.in", "r", stdin);
    // freopen("out.out", "w", stdout);
}

signed main() {
// #ifdef LOCAL
//     file();
// #endif
    int n = rd(), m = rd();
	
	for(int i = 1; i <= n; i++) {
		col[i] = rd();
	}

	for(int i = 1; i <= m; i++) {
		char opt[5];
		scanf("%s", opt + 1);
		if(opt[1] == 'Q') {
			query[++tot_query].l = rd(), query[tot_query].r = rd();
			query[tot_query].id = i; query[tot_query].ti = tot_modify;
		} else {
			modify[++tot_modify].p = rd(), modify[tot_modify].col = rd();
		}
	}

	int sz = pow(n, 0.6666666666666666) + 4;
	// cout << sz << endl;

	for(int i = 1; i <= tot_query; i++) {
		query[i].lb = query[i].l / sz;
		query[i].rb = query[i].r / sz;
	}

	sort(query + 1, query + tot_query + 1);
	// cout << tot_query << " " << tot_modify << endl;
	for(int i = 1; i <= tot_query; i++) {
		cout << query[i].l << " " << query[i].r << " " << query[i].id << " " << query[i].lb << " " << query[i].rb << " " << query[i].ti << endl;
	}

	int l = 1, r = 1; now = 0; add(1);
	for(int i = 1; i <= tot_query; i++) {
		while(l < query[i].l) remove(l++);
		while(l > query[i].l) add(--l);
		while(r < query[i].r) add(++r);
		while(r > query[i].r) remove(r--);
		while(now < query[i].ti) moveTime(++now, i);
		while(now > query[i].ti) moveTime(now--, i);
		query[i].ans = ans;
	}

	sort(query + 1, query + tot_query + 1, [](const Query& a, const Query& b) -> bool { return a.id < b.id; });
	for(int i = 1; i <= tot_query; i++) {
		printf("%d\n", query[i].ans);
	}

    system("pause");
    return 0;
}
2021/3/29 11:57
加载中...