結果

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

ソースコード

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