結果

問題 No.1194 Replace
ユーザー 👑 tatyam
提出日時 2020-08-22 19:52:53
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 538 ms / 2,000 ms
コード長 1,154 bytes
コンパイル時間 3,102 ms
コンパイル使用メモリ 209,128 KB
最終ジャッジ日時 2025-01-13 11:45:34
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
template<class T, class U> bool chmax(T& a, const U& b){ if(a < b){ a = b; return 1; } return 0; }


int main(){
    ll n, m;
    cin >> n >> m;
    vector<pll> edge(m);
    vector<ll> x(m * 2);
    for(ll i = 0; i < m; i++){
        ll a, b;
        cin >> a >> b;
        a--; b--;
        edge[i] = {a, b};
        x[i * 2] = a;
        x[i * 2 + 1] = b;
    }
    sort(x.begin(), x.end());
    x.erase(unique(x.begin(), x.end()), x.end());
    vector g(x.size(), vector<ll>{});
    for(auto& [a, b] : edge){
        a = lower_bound(x.begin(), x.end(), a) - x.begin();
        b = lower_bound(x.begin(), x.end(), b) - x.begin();
        g[b].push_back(a);
    }
    vector<ll> mx = x;
    priority_queue<pll> q;
    for(ll i = 0; i < x.size(); i++) q.emplace(x[i], i);
    while(q.size()){
        auto [c, at] = q.top();
        q.pop();
        if(mx[at] != c) continue;
        for(ll i : g[at]) if(chmax(mx[i], c)) q.emplace(c, i);
    }
    ll ans = n * (n + 1) / 2;
    for(ll i = 0; i < x.size(); i++) ans += mx[i] - x[i];
    cout << ans << endl;
}
0