結果

問題 No.1194 Replace
ユーザー e869120e869120
提出日時 2021-03-05 19:33:57
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 394 ms / 2,000 ms
コード長 1,333 bytes
コンパイル時間 1,101 ms
コンパイル使用メモリ 95,564 KB
実行使用メモリ 31,452 KB
最終ジャッジ日時 2024-04-16 07:22:52
合計ジャッジ時間 9,026 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 279 ms
27,224 KB
testcase_01 AC 300 ms
28,116 KB
testcase_02 AC 232 ms
25,428 KB
testcase_03 AC 191 ms
24,024 KB
testcase_04 AC 300 ms
27,996 KB
testcase_05 AC 275 ms
26,968 KB
testcase_06 AC 252 ms
26,200 KB
testcase_07 AC 393 ms
31,192 KB
testcase_08 AC 393 ms
31,192 KB
testcase_09 AC 394 ms
31,320 KB
testcase_10 AC 392 ms
31,452 KB
testcase_11 AC 394 ms
31,452 KB
testcase_12 AC 392 ms
31,272 KB
testcase_13 AC 252 ms
26,328 KB
testcase_14 AC 220 ms
24,924 KB
testcase_15 AC 173 ms
23,384 KB
testcase_16 AC 241 ms
25,948 KB
testcase_17 AC 163 ms
22,872 KB
testcase_18 AC 170 ms
23,128 KB
testcase_19 AC 240 ms
25,864 KB
testcase_20 AC 8 ms
15,744 KB
testcase_21 AC 8 ms
15,872 KB
testcase_22 AC 9 ms
15,872 KB
testcase_23 AC 74 ms
19,032 KB
testcase_24 AC 28 ms
16,884 KB
testcase_25 AC 19 ms
16,384 KB
testcase_26 AC 99 ms
20,056 KB
testcase_27 AC 26 ms
16,616 KB
testcase_28 AC 50 ms
17,876 KB
testcase_29 AC 11 ms
16,000 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
using namespace std;
#pragma warning (disable: 4996)

long long N, M;
long long B[1 << 19], C[1 << 19];
long long col[1 << 19];
vector<long long> X;
vector<long long> G[1 << 19];

int main() {
	// Step #1. 入力
	cin >> N >> M;
	for (int i = 1; i <= M; i++) {
		cin >> B[i] >> C[i];
		X.push_back(B[i]);
		X.push_back(C[i]);
	}

	// Step #2. 座標圧縮
	sort(X.begin(), X.end());
	X.erase(unique(X.begin(), X.end()), X.end());
	for (int i = 1; i <= M; i++) B[i] = lower_bound(X.begin(), X.end(), B[i]) - X.begin();
	for (int i = 1; i <= M; i++) C[i] = lower_bound(X.begin(), X.end(), C[i]) - X.begin();
	for (int i = 1; i <= M; i++) G[C[i]].push_back(B[i]);

	// Step #3. 計算
	for (int i = 0; i < X.size(); i++) col[i] = -1;
	for (int i = X.size() - 1; i >= 0; i--) {
		if (col[i] != -1) continue;
		queue<int> Q; Q.push(i); col[i] = i;
		while (!Q.empty()) {
			int pos = Q.front(); Q.pop();
			for (int j : G[pos]) {
				if (col[j] == -1) { col[j] = i; Q.push(j); }
			}
		}
	}

	// Step #4. 答えを求める
	long long Answer = N * (N + 1LL) / 2LL;
	for (int i = 0; i < X.size(); i++) {
		Answer += (X[col[i]] - X[i]);
	}
	cout << Answer << endl;
	return 0;
}
0