社区讨论

mayday! 求助:后面三个是什么魔鬼数据?

P1972[SDOI2009] HH 的项链参与者 3已保存回复 7

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
7 条
当前快照
1 份
快照标识符
@mi7xwxhj
此快照首次捕获于
2025/11/21 05:25
4 个月前
此快照最后确认于
2025/11/21 05:25
4 个月前
查看原帖
本蒻蒟调了两个小时的莫队,各种优化,还是80分TLE,后来试图写哈夫曼距离最小生成树+莫队写炸了
于是改写正解树状数组,结果只有70分TLE,求救
CPP
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 500100;
const int MAXM = 500100;
#define lowbit(x) ((x) & (-x))

struct node{
    int st, ed, id;
    node(int aid = 0, int ast = 0, int aed = 0){st = ast; ed = aed; id = aid;}
}que[MAXM];

int n, q;
int col[MAXN], pre[MAXN];
int tr[MAXN], ans[MAXM];

inline bool cmp(const node &x, const node &y){return x.ed < y.ed;}
inline void modify(int pos, const int &val);
inline int query(int pos);

int main(){
	scanf("%d", &n);
	for(int i = 1; i <= n; i++) scanf("%d", &col[i]);
	scanf("%d", &q);
	for(int st, ed, i = 1; i <= q; i++){
	    scanf("%d %d", &st, &ed);
	    que[i] = node(i, st, ed);
	}
	sort(que + 1, que + q + 1, cmp);
	memset(pre, -1, sizeof(pre));
	int nxt = 1;
	for(int i = 1; i <= q; i++){
	    for(int j = nxt; j <= que[i].ed; j++){
		    if(pre[col[j]] != -1) modify(pre[col[j]], -1);
			modify(j, 1); pre[col[j]] = j;
		}
		nxt = que[i].ed + 1;
		ans[que[i].id] = query(que[i].ed) - query(que[i].st - 1);
	}
	for(int i = 1; i <= q; i++) printf("%d\n", ans[i]);
    return 0;
}

inline void modify(int pos, const int &val){
    while(pos <= n) tr[pos] += val, pos += lowbit(pos);
}
inline int query(int pos){
    int ret = 0;
	while(pos != 0) ret += tr[pos], pos -= lowbit(pos);
	return ret;
}
mayday!
mayday!!
mayday!!!
向各位大佬求助...qwq

回复

7 条回复,欢迎继续交流。

正在加载回复...