#include <bits/stdc++.h>
using namespace std;
// #define int long long
#define ll long long
#define pii pair<int, int>
#define all(v) v.begin(), v.end()
#define oo 1e9
const int MAX = 2e5 + 5, MX = 1e7 + 7;
int n;
int arr[MAX];
int nxt[2 * MX];
vector<pii> E[2 * MX];
struct DSU{
int par[MAX];
void init(){
for(int i = 0; i < MAX; i++) par[i] = -1;
}
int get(int i){
if(par[i] < 0) return i;
return par[i] = get(par[i]);
}
bool unite(int u, int v){
u = get(u), v = get(v);
if(u == v) return 0;
if(-par[u] < -par[v]) swap(u, v);
par[u] += par[v];
par[v] = u;
return 1;
}
};
DSU dsu;
void solve(){
cin >> n;
dsu.init();
for(int i = 1; i <= n; i++){
cin >> arr[i];
if(nxt[arr[i]]) dsu.unite(i, nxt[arr[i]]);
nxt[arr[i]] = i;
}
int cur = 0;
for(int i = MX; i >= 1; i--){
if(nxt[i]) cur = nxt[i];
nxt[i] = cur;
}
for(int i = 1; i <= n; i++){
if(nxt[arr[i] + 1] && arr[nxt[arr[i] + 1]] < 2 * arr[i]) E[arr[nxt[arr[i] + 1]] - arr[i]].push_back({i, nxt[arr[i] + 1]});
for(int j = 2 * arr[i]; j < MX; j += arr[i]){
if(nxt[j] && (arr[nxt[j]] < j + arr[i])) E[arr[nxt[j]] - j].push_back({i, nxt[j]});
}
}
ll ans = 0;
for(int i = 0; i < MX; i++){
for(auto a : E[i]){
if(dsu.unite(a.first, a.second)) ans += i;
}
}
cout << ans << '\n';
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
while(t--){
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
212 ms |
509948 KB |
Output is correct |
2 |
Correct |
217 ms |
513264 KB |
Output is correct |
3 |
Correct |
198 ms |
510292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
257 ms |
509972 KB |
Output is correct |
2 |
Correct |
379 ms |
511056 KB |
Output is correct |
3 |
Correct |
199 ms |
510288 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
192 ms |
510036 KB |
Output is correct |
2 |
Correct |
212 ms |
509884 KB |
Output is correct |
3 |
Correct |
203 ms |
510036 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
299 ms |
519648 KB |
Output is correct |
2 |
Correct |
530 ms |
557720 KB |
Output is correct |
3 |
Correct |
336 ms |
530444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
206 ms |
511560 KB |
Output is correct |
2 |
Correct |
2195 ms |
781024 KB |
Output is correct |
3 |
Correct |
348 ms |
522380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
389 ms |
534688 KB |
Output is correct |
2 |
Correct |
692 ms |
587124 KB |
Output is correct |
3 |
Correct |
309 ms |
524668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
340 ms |
513728 KB |
Output is correct |
2 |
Correct |
704 ms |
579480 KB |
Output is correct |
3 |
Correct |
317 ms |
527324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
255 ms |
522576 KB |
Output is correct |
2 |
Runtime error |
758 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
261 ms |
522496 KB |
Output is correct |
2 |
Runtime error |
708 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
206 ms |
512360 KB |
Output is correct |
2 |
Runtime error |
752 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |