結果
問題 | No.1194 Replace |
ユーザー | tnakao0123 |
提出日時 | 2020-08-24 17:50:08 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 251 ms / 2,000 ms |
コード長 | 1,658 bytes |
コンパイル時間 | 1,048 ms |
コンパイル使用メモリ | 103,076 KB |
実行使用メモリ | 24,064 KB |
最終ジャッジ日時 | 2024-04-24 03:39:45 |
合計ジャッジ時間 | 6,045 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 164 ms
20,864 KB |
testcase_01 | AC | 178 ms
21,632 KB |
testcase_02 | AC | 151 ms
19,840 KB |
testcase_03 | AC | 121 ms
18,816 KB |
testcase_04 | AC | 185 ms
21,504 KB |
testcase_05 | AC | 160 ms
20,864 KB |
testcase_06 | AC | 147 ms
20,352 KB |
testcase_07 | AC | 231 ms
24,064 KB |
testcase_08 | AC | 244 ms
24,064 KB |
testcase_09 | AC | 251 ms
24,064 KB |
testcase_10 | AC | 241 ms
23,936 KB |
testcase_11 | AC | 237 ms
24,064 KB |
testcase_12 | AC | 245 ms
23,936 KB |
testcase_13 | AC | 154 ms
19,584 KB |
testcase_14 | AC | 133 ms
18,816 KB |
testcase_15 | AC | 111 ms
18,048 KB |
testcase_16 | AC | 147 ms
19,584 KB |
testcase_17 | AC | 99 ms
17,664 KB |
testcase_18 | AC | 101 ms
17,792 KB |
testcase_19 | AC | 144 ms
19,456 KB |
testcase_20 | AC | 9 ms
13,056 KB |
testcase_21 | AC | 8 ms
13,056 KB |
testcase_22 | AC | 9 ms
13,056 KB |
testcase_23 | AC | 48 ms
15,232 KB |
testcase_24 | AC | 20 ms
13,952 KB |
testcase_25 | AC | 15 ms
13,440 KB |
testcase_26 | AC | 64 ms
15,744 KB |
testcase_27 | AC | 19 ms
13,696 KB |
testcase_28 | AC | 33 ms
14,336 KB |
testcase_29 | AC | 10 ms
13,312 KB |
ソースコード
/* -*- coding: utf-8 -*- * * 1194.cc: No.1194 Replace - yukicoder */ #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<iostream> #include<string> #include<vector> #include<map> #include<set> #include<stack> #include<list> #include<queue> #include<deque> #include<algorithm> #include<numeric> #include<utility> #include<complex> #include<functional> using namespace std; /* constant */ const int MAX_M = 200000; const int MAX_K = MAX_M * 2; /* typedef */ typedef long long ll; typedef vector<int> vi; typedef queue<int> qi; typedef pair<int,int> pii; /* global variables */ pii es[MAX_M]; int uvs[MAX_K], mxs[MAX_K]; vi nbrs[MAX_K]; /* subroutines */ /* main */ int main() { int n, m; scanf("%d%d", &n, &m); int k = 0; for (int i = 0; i < m; i++) { int b, c; scanf("%d%d", &b, &c); es[i] = pii(b, c); uvs[k++] = b, uvs[k++] = c; } sort(uvs, uvs + k); k = unique(uvs, uvs + k) - uvs; //printf("k=%d\n", k); for (int i = 0; i < m; i++) { int bi = lower_bound(uvs, uvs + k, es[i].first) - uvs; int ci = lower_bound(uvs, uvs + k, es[i].second) - uvs; nbrs[ci].push_back(bi); } for (int st = k - 1; st >= 0; st--) if (! mxs[st]) { int x = mxs[st] = uvs[st]; qi q; q.push(st); while (! q.empty()) { int u = q.front(); q.pop(); vi &nbru = nbrs[u]; for (vi::iterator vit = nbru.begin(); vit != nbru.end(); vit++) { int v = *vit; if (! mxs[v]) { mxs[v] = x; q.push(v); } } } } ll sum = (ll)n * (n + 1) / 2; for (int i = 0; i < k; i++) sum += mxs[i] - uvs[i]; printf("%lld\n", sum); return 0; }