結果

問題 No.1194 Replace
ユーザー e869120e869120
提出日時 2021-03-05 19:30:19
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 1,333 bytes
コンパイル時間 1,210 ms
コンパイル使用メモリ 95,700 KB
実行使用メモリ 25,564 KB
最終ジャッジ日時 2024-04-16 07:20:53
合計ジャッジ時間 12,608 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 177 ms
24,916 KB
testcase_16 WA -
testcase_17 AC 158 ms
23,508 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 7 ms
15,872 KB
testcase_21 AC 8 ms
15,744 KB
testcase_22 AC 8 ms
15,744 KB
testcase_23 AC 74 ms
19,672 KB
testcase_24 AC 30 ms
18,208 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 AC 13 ms
17,408 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 <= N; i++) B[i] = lower_bound(X.begin(), X.end(), B[i]) - X.begin();
	for (int i = 1; i <= N; i++) C[i] = lower_bound(X.begin(), X.end(), C[i]) - X.begin();
	for (int i = 1; i <= N; 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