結果

問題 No.1194 Replace
ユーザー 沙耶花沙耶花
提出日時 2020-08-22 14:19:50
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 427 ms / 2,000 ms
コード長 1,146 bytes
コンパイル時間 2,510 ms
コンパイル使用メモリ 209,724 KB
実行使用メモリ 28,076 KB
最終ジャッジ日時 2024-04-23 08:40:30
合計ジャッジ時間 10,552 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 305 ms
19,936 KB
testcase_01 AC 323 ms
20,804 KB
testcase_02 AC 249 ms
18,072 KB
testcase_03 AC 207 ms
15,380 KB
testcase_04 AC 318 ms
20,660 KB
testcase_05 AC 295 ms
19,636 KB
testcase_06 AC 266 ms
18,804 KB
testcase_07 AC 424 ms
27,820 KB
testcase_08 AC 423 ms
27,944 KB
testcase_09 AC 422 ms
27,944 KB
testcase_10 AC 427 ms
27,820 KB
testcase_11 AC 427 ms
27,952 KB
testcase_12 AC 426 ms
28,076 KB
testcase_13 AC 283 ms
15,800 KB
testcase_14 AC 244 ms
13,100 KB
testcase_15 AC 185 ms
12,596 KB
testcase_16 AC 272 ms
15,852 KB
testcase_17 AC 180 ms
11,904 KB
testcase_18 AC 187 ms
11,604 KB
testcase_19 AC 263 ms
15,940 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 75 ms
7,544 KB
testcase_24 AC 24 ms
5,376 KB
testcase_25 AC 14 ms
5,376 KB
testcase_26 AC 100 ms
7,504 KB
testcase_27 AC 20 ms
5,376 KB
testcase_28 AC 47 ms
5,388 KB
testcase_29 AC 5 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define modulo 998244353
#define mod(mod_x) ((((long long)mod_x+modulo))%modulo)
#define Inf 1000000005


int main(){
	
	int N,M;
	cin>>N>>M;
	
	vector<int> B(M),C(M);
	vector<int> t;
	for(int i=0;i<M;i++){
		cin>>B[i]>>C[i];
		
		t.push_back(B[i]);
		t.push_back(C[i]);
	}
	
	sort(t.begin(),t.end());
	t.erase(unique(t.begin(),t.end()),t.end());
	vector<vector<int>> E(t.size(),vector<int>());
	for(int i=0;i<M;i++){
		B[i] = distance(t.begin(),lower_bound(t.begin(),t.end(),B[i]));
		C[i] = distance(t.begin(),lower_bound(t.begin(),t.end(),C[i]));
		E[C[i]].push_back(B[i]);
	}
	
	vector<int> dis(t.size());
	priority_queue<pair<int,int>> Q;
	
	for(int i=0;i<t.size();i++){
		dis[i] = t[i];
		Q.emplace(t[i],i);
	}
	
	while(Q.size()!=0){
		int D = Q.top().first;
		int u = Q.top().second;
		Q.pop();
		
		if(dis[u]!=D)continue;
		
		for(int i=0;i<E[u].size();i++){
			int v = E[u][i];
			if(dis[v]<D){
				dis[v] = D;
				Q.emplace(dis[v],v);
			}
		}
	}
	
	long long ans = (long long)N*(N+1)/2;
	
	for(int i=0;i<t.size();i++){
		ans -= t[i];
		ans += dis[i];
	}
	
	cout<<ans<<endl;
	
	return 0;
}
0