結果

問題 No.1194 Replace
ユーザー 👑 tatyamtatyam
提出日時 2020-08-22 19:52:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 458 ms / 2,000 ms
コード長 1,154 bytes
コンパイル時間 2,704 ms
コンパイル使用メモリ 210,440 KB
実行使用メモリ 36,732 KB
最終ジャッジ日時 2024-04-23 12:19:07
合計ジャッジ時間 11,574 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 311 ms
24,852 KB
testcase_01 AC 342 ms
25,892 KB
testcase_02 AC 264 ms
22,304 KB
testcase_03 AC 216 ms
20,128 KB
testcase_04 AC 346 ms
26,008 KB
testcase_05 AC 325 ms
24,548 KB
testcase_06 AC 286 ms
23,532 KB
testcase_07 AC 447 ms
36,596 KB
testcase_08 AC 446 ms
36,596 KB
testcase_09 AC 442 ms
36,608 KB
testcase_10 AC 446 ms
36,604 KB
testcase_11 AC 450 ms
36,732 KB
testcase_12 AC 458 ms
36,608 KB
testcase_13 AC 288 ms
21,020 KB
testcase_14 AC 265 ms
17,236 KB
testcase_15 AC 198 ms
16,104 KB
testcase_16 AC 282 ms
20,668 KB
testcase_17 AC 185 ms
15,136 KB
testcase_18 AC 194 ms
14,832 KB
testcase_19 AC 279 ms
20,796 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 1 ms
5,376 KB
testcase_23 AC 77 ms
9,212 KB
testcase_24 AC 24 ms
5,632 KB
testcase_25 AC 14 ms
5,376 KB
testcase_26 AC 100 ms
9,808 KB
testcase_27 AC 20 ms
5,376 KB
testcase_28 AC 47 ms
6,656 KB
testcase_29 AC 5 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

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