結果
問題 |
No.1194 Replace
|
ユーザー |
![]() |
提出日時 | 2020-08-22 16:40:03 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
TLE
(最新)
AC
(最初)
|
実行時間 | - |
コード長 | 1,349 bytes |
コンパイル時間 | 2,759 ms |
コンパイル使用メモリ | 209,484 KB |
最終ジャッジ日時 | 2025-01-13 10:17:20 |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 26 TLE * 1 |
ソースコード
#include <bits/stdc++.h> #define rep(i,n) for(int i = 0; i < (n); ++i) #define srep(i,s,t) for(int i = s; i < t; ++i) #define drep(i,n) for(int i = (n)-1; i >= 0; --i) using namespace std; typedef long long int ll; typedef pair<int,int> P; #define yn {puts("Yes");}else{puts("No");} #define MAX_N 500005 vector<int> G[MAX_N]; int main() { ll n, m; cin >> n >> m; int b[m], c[m]; set<int> se; rep(i,m){ cin >> b[i] >> c[i]; se.insert(b[i]); se.insert(c[i]); } ll ans = n * (n+1) / 2; map<int,int> mp; int w = se.size(); int d[w]; int cnt = 0; for(auto x : se){ mp[x] = cnt; d[cnt] = x; cnt++; } rep(i,m){ b[i] = mp[b[i]]; c[i] = mp[c[i]]; G[c[i]].push_back(b[i]); } int f[w]; rep(i,w)f[i] = i; drep(i,w){ if(f[i] > i)continue; queue<int> que; que.push(i); while(que.size()){ int x = que.front(); que.pop(); rep(j,G[x].size()){ int y = G[x][j]; if(f[y] < i){ f[y] = i; que.push(y); } } } } rep(i,w){ int x = d[i]; int y = d[f[i]]; ans += y - x; } cout << ans << endl; return 0; }