結果

問題 No.1194 Replace
ユーザー chocoruskchocorusk
提出日時 2020-08-22 14:37:05
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 890 ms / 2,000 ms
コード長 1,446 bytes
コンパイル時間 1,704 ms
コンパイル使用メモリ 138,836 KB
実行使用メモリ 41,480 KB
最終ジャッジ日時 2024-04-23 08:57:03
合計ジャッジ時間 15,971 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 575 ms
32,296 KB
testcase_01 AC 683 ms
33,520 KB
testcase_02 AC 472 ms
30,092 KB
testcase_03 AC 406 ms
27,868 KB
testcase_04 AC 664 ms
33,312 KB
testcase_05 AC 578 ms
32,184 KB
testcase_06 AC 563 ms
30,988 KB
testcase_07 AC 890 ms
41,412 KB
testcase_08 AC 886 ms
41,480 KB
testcase_09 AC 861 ms
41,348 KB
testcase_10 AC 886 ms
41,480 KB
testcase_11 AC 884 ms
41,396 KB
testcase_12 AC 881 ms
41,472 KB
testcase_13 AC 492 ms
26,312 KB
testcase_14 AC 410 ms
24,644 KB
testcase_15 AC 325 ms
24,668 KB
testcase_16 AC 479 ms
26,360 KB
testcase_17 AC 290 ms
23,756 KB
testcase_18 AC 299 ms
23,468 KB
testcase_19 AC 472 ms
26,464 KB
testcase_20 AC 6 ms
15,828 KB
testcase_21 AC 6 ms
15,704 KB
testcase_22 AC 6 ms
15,700 KB
testcase_23 AC 104 ms
19,652 KB
testcase_24 AC 34 ms
16,724 KB
testcase_25 AC 19 ms
15,452 KB
testcase_26 AC 147 ms
19,892 KB
testcase_27 AC 28 ms
16,172 KB
testcase_28 AC 63 ms
17,428 KB
testcase_29 AC 11 ms
16,336 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;

int main()
{
    int n, m; cin>>n>>m;
    int b[200020], c[200020];
    map<int, int> mp;
    for(int i=0; i<m; i++){
        cin>>b[i]>>c[i];
        mp[b[i]]=0, mp[c[i]]=0;
    }
    int v[400040];
    int l=0;
    for(auto &p:mp){
        p.second=l;
        v[l]=p.first;
        l++;
    }
    vector<int> g[400040];
    for(int i=0; i<m; i++){
        int x=mp[b[i]], y=mp[c[i]];
        g[y].push_back(x);
    }
    bool used[400040]={};
    ll cnt=0; ll sum=0;
    auto dfs=[&](auto dfs, int x, int mx)->void{
        used[x]=1;
        cnt++;
        sum+=v[x];
        for(auto y:g[x]){
            if(!used[y] && y<mx){
                dfs(dfs, y, mx);
            }
        }
    };
    ll ans=(ll)n*(n+1)/2;
    for(int i=l-1; i>=0; i--){
        if(used[i]) continue;
        cnt=0;
        sum=0;
        dfs(dfs, i, i);
        ans+=cnt*v[i]-sum;
    }
    cout<<ans<<endl;
    return 0;
}
0