結果

問題 No.1194 Replace
ユーザー HIR180HIR180
提出日時 2020-08-22 14:22:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,050 ms / 2,000 ms
コード長 1,894 bytes
コンパイル時間 3,353 ms
コンパイル使用メモリ 257,808 KB
実行使用メモリ 61,040 KB
最終ジャッジ日時 2023-08-05 11:33:28
合計ジャッジ時間 20,118 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 675 ms
39,356 KB
testcase_01 AC 717 ms
41,708 KB
testcase_02 AC 560 ms
34,424 KB
testcase_03 AC 452 ms
29,816 KB
testcase_04 AC 721 ms
41,392 KB
testcase_05 AC 643 ms
38,812 KB
testcase_06 AC 579 ms
36,640 KB
testcase_07 AC 1,015 ms
61,040 KB
testcase_08 AC 1,013 ms
60,824 KB
testcase_09 AC 1,042 ms
60,724 KB
testcase_10 AC 1,022 ms
60,884 KB
testcase_11 AC 1,027 ms
60,772 KB
testcase_12 AC 1,050 ms
60,892 KB
testcase_13 AC 529 ms
23,108 KB
testcase_14 AC 462 ms
19,384 KB
testcase_15 AC 378 ms
21,836 KB
testcase_16 AC 527 ms
23,436 KB
testcase_17 AC 357 ms
19,816 KB
testcase_18 AC 340 ms
17,380 KB
testcase_19 AC 543 ms
24,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 136 ms
11,564 KB
testcase_24 AC 42 ms
7,092 KB
testcase_25 AC 15 ms
4,376 KB
testcase_26 AC 155 ms
9,672 KB
testcase_27 AC 23 ms
4,380 KB
testcase_28 AC 68 ms
6,308 KB
testcase_29 AC 7 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//Let's join Kaede Takagaki Fan Club !!
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define fi first
#define sc second
#define rep(i,x) for(int i=0;i<x;i++)
#define repn(i,x) for(int i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
#define all(x) x.begin(),x.end()
template<class T>
void dmp(T a){
	rep(i,a.size()) cout << a[i] << " ";
	cout << endl;
}
template<class T>
bool chmax(T&a, T b){
	if(a < b){
		a = b;
		return 1;
	}
	return 0;
}
template<class T>
bool chmin(T&a, T b){
	if(a > b){
		a = b;
		return 1;
	}
	return 0;
}
template<class T>
void g(T &a){
	cin >> a;
}
template<class T>
void o(const T &a,bool space=false){
	cout << a << (space?' ':'\n');
}
//ios::sync_with_stdio(false);
const ll mod = 998244353;
template<class T>
void add(T&a,T b){
	a+=b;
	if(a >= mod) a-=mod;
}

map<int, int>M;
int n, m;
map<int, vector<int>>E;
vector<int>vec;
int main(){
	cin >> n >> m;
	rep(i, m){
		int a, b; cin >> a >> b;
		vec.pb(a); vec.pb(b);
		E[b].pb(a);
	}
	SORT(vec); ERASE(vec);
	reverse(all(vec));
	rep(i, vec.size()){
		int q = vec[i];
		queue<int>que; que.push(q);
		while(!que.empty()){
			int q = que.front(); que.pop();
			if(M.find(q) != M.end()) continue;
			M[q] = vec[i];
			for(auto at:E[q]){
				if(M.find(at) == M.end()) que.push(at);
			}
		}
	}
	ll ans = 1LL*n*(n+1)/2;
	rep(i, vec.size()) ans += M[vec[i]] - vec[i];
	cout << ans << endl;
}
0