結果

問題 No.1194 Replace
ユーザー fumofumofunifumofumofuni
提出日時 2021-07-28 15:38:51
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 339 ms / 2,000 ms
コード長 1,676 bytes
コンパイル時間 2,233 ms
コンパイル使用メモリ 207,632 KB
実行使用メモリ 28,484 KB
最終ジャッジ日時 2023-10-11 13:22:52
合計ジャッジ時間 13,021 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 243 ms
21,956 KB
testcase_01 AC 265 ms
23,592 KB
testcase_02 AC 202 ms
19,624 KB
testcase_03 AC 166 ms
17,500 KB
testcase_04 AC 262 ms
22,720 KB
testcase_05 AC 241 ms
21,648 KB
testcase_06 AC 220 ms
20,340 KB
testcase_07 AC 333 ms
28,000 KB
testcase_08 AC 336 ms
27,920 KB
testcase_09 AC 334 ms
28,200 KB
testcase_10 AC 333 ms
28,484 KB
testcase_11 AC 335 ms
28,048 KB
testcase_12 AC 339 ms
28,064 KB
testcase_13 AC 246 ms
19,596 KB
testcase_14 AC 212 ms
17,984 KB
testcase_15 AC 164 ms
15,704 KB
testcase_16 AC 234 ms
20,092 KB
testcase_17 AC 153 ms
15,256 KB
testcase_18 AC 163 ms
15,640 KB
testcase_19 AC 226 ms
18,780 KB
testcase_20 AC 3 ms
6,160 KB
testcase_21 AC 3 ms
6,144 KB
testcase_22 AC 3 ms
6,156 KB
testcase_23 AC 65 ms
10,428 KB
testcase_24 AC 21 ms
7,856 KB
testcase_25 AC 14 ms
6,984 KB
testcase_26 AC 93 ms
11,580 KB
testcase_27 AC 20 ms
7,228 KB
testcase_28 AC 44 ms
9,000 KB
testcase_29 AC 6 ms
6,436 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=(n)-1;i>=0;i--)
#define perl(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x,vector<x>,greater<x>>
#define all(x) (x).begin(),(x).end()
#define CST(x) cout<<fixed<<setprecision(x)
#define rev(x) reverse(x);
using ll=long long;
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using pl=pair<ll,ll>;
using vpl=vector<pl>;
using vvpl=vector<vpl>;
const ll MOD=1000000007;
const ll MOD9=998244353;
const int inf=1e9;
const ll INF=4e18;
const ll dy[8]={1,0,-1,0,0,1,-1,-1};
const ll dx[8]={0,1,0,-1,0,-1,1,-1};
template <typename T> inline bool chmax(T &a, T b) {
  return ((a < b) ? (a = b, true) : (false));
}
template <typename T> inline bool chmin(T &a, T b) {
  return ((a > b) ? (a = b, true) : (false));
}
vl seen(400010,-1);
void dfs(vvl &g,ll v,ll t){
  seen[v]=t;
  for(auto p:g[v]){
    if(seen[p]!=-1)continue;
    dfs(g,p,t);
  }
}
int main(){
  ll n,m;cin >> n >>m;
  vl pls;
  vl b(m),c(m);
  rep(i,m){
    cin >> b[i] >> c[i];
    pls.push_back(b[i]);
    pls.push_back(c[i]);
  }
  sort(all(pls));pls.erase(unique(all(pls)),pls.end());
  vvl g(pls.size());
  rep(i,m){
    ll f=lower_bound(all(pls),c[i])-pls.begin();
    ll t=lower_bound(all(pls),b[i])-pls.begin();
    g[f].push_back(t);
  }
  per(i,pls.size()){
    if(seen[i]!=-1)continue;
    dfs(g,i,i);
  }
  ll ans=n*(n+1)/2;
  //rep(i,pls.size())cout << seen[i] <<endl;
  rep(i,pls.size()){
    ans-=pls[i];
    ans+=pls[seen[i]];
  }
  cout << ans <<endl;
}
0