问
  • 板块灌水区
  • 楼主__zhuruirong__
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/7/28 12:43
  • 上次更新2024/7/28 14:33:29
查看原帖
问
764442
__zhuruirong__楼主2024/7/28 12:43

为什么这份代码会RE?

#include <bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;

const int N = 1e6 + 10;
struct Node {
	int l, r, c;
	bool operator < (const Node& x) const {
		return c < x.c;
	}
}num[N];
int n, q, f[N], nxt[N], ans, cnt;

int find(int x) {
	return x == f[x] ? x : f[x] = find(f[x]);
}

signed main() {
	
	cin >> n >> q;
	cnt = n;
	for(int i = 1; i <= n; i++) {
		f[i] = nxt[i] = i;
	}
	for(int i = 1; i <= q; i++) {
		cin >> num[i].l >> num[i].r >> num[i].c;
	}
	sort(num + 1, num + q + 1);
	for(int i = 1; i <= q; i++) {
		int l = num[i].l, r = num[i].r, c = num[i].c;
		for(int j = l; j <= r; j++) {
			j = nxt[find(j)];
			ans += c;
			if(j + 1 <= r) {
				int nr = nxt[find(j + 1)];
				if(find(j) != find(j + 1)) {
					cnt--;
				}
				f[find(j)] = find(j + 1);
				nxt[find(j)] = nr;
			}
		}
	}
	cout << (cnt == 1 ? ans : -1) << "\n";

	return 0;
}

RE的数据:

200000 4
1 200000 1000000000
1 200000 998244353
1 200000 999999999
1 200000 999999999

2024/7/28 12:43
加载中...