#include"railroad.h"
#include<algorithm>
#include<iostream>
#include<cassert>
#include<vector>
#include<string>
#include<array>
#include<stack>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define V vector
#define Int long long
#define rep(i,a,b) for(Int i=(Int)(a); i<(Int)(b); i++)
#define all(x) (x).begin(), (x).end()
#define nl '\n'
#define dbg(x) cout << "?" << #x << " : " << x << endl;
const Int INF = 1e9+5;
const Int N = 2e5+5;
Int n;
V<Int> s, t;
map<Int,Int> zip;
V<Int> unzip;
Int ans = 0;
#define mid ((tl+tr)/2)
#define lc (v+1)
#define rc (v+2*(mid-tl+1))
struct Segtree{
Int st[4*N];
void upd(Int l,Int r,Int x,Int v=0,Int tl=0,Int tr=2*N){
if(r<tl || tr<l) return;
if(l<=tl && tr<=r){
st[v] += x; return;
}
upd(l,r,x, lc,tl,mid);
upd(l,r,x, rc,mid+1,tr);
}
Int qry(Int i,Int v=0,Int tl=0,Int tr=2*N){
if(tl==tr) return st[v];
if(i<=mid) return st[v] + qry(i, lc,tl,mid);
else return st[v] + qry(i, rc,mid+1,tr);
}
} segtree;
struct Dsu{
Int p[2*N];
Int find(Int x){
if(p[x]==x) return x;
return p[x] = find(p[x]);
}
bool same(Int x,Int y){
return find(x)==find(y);
}
void unite(Int x,Int y){
if((x=find(x))==(y=find(y))) return;
p[x] = y;
}
} dsu;
Int plan_roller_coaster(V<int>_s,V<int>_t){
s = V<Int>(all(_s)); t = V<Int>(all(_t));
s.push_back(INF); t.push_back(0);
n = s.size();
rep(i,0,n){
zip[s[i]]; zip[t[i]];
}
Int timer = 0;
unzip = V<Int>(zip.size());
for(auto&[x,y] : zip) unzip[y=timer++] = x;
rep(i,0,n){
s[i] = zip[s[i]];
t[i] = zip[t[i]];
// cout << s[i] << " " << t[i] << nl;
if(s[i] < t[i]){
segtree.upd(s[i],t[i]-1, 1);
}
if(t[i] < s[i]){
segtree.upd(t[i],s[i]-1, -1);
}
dsu.unite(s[i], t[i]);
}
// {-w, u, v}
priority_queue<array<Int,3>> pq;
rep(i,0,zip.size()-1){
pq.push({-(unzip[i+1]-unzip[i]), i+1,i});;
Int cnt = segtree.qry(i);
if(cnt != 0){
dsu.unite(i+1, i);
}
if(cnt > 0){
// dbg(cnt);
// dbg((unzip[i+1]-unzip[i])*cnt);
ans += (unzip[i+1]-unzip[i])*cnt;
}
}
while(!pq.empty()){
auto[w,u,v] = pq.top(); pq.pop();
w *= -1;
if(dsu.same(u,v)) continue;
// dbg(w);
ans += w;
dsu.unite(u,v);
}
// cout << ans << nl;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
n = 2 |
2 |
Correct |
0 ms |
212 KB |
n = 2 |
3 |
Correct |
0 ms |
212 KB |
n = 2 |
4 |
Correct |
1 ms |
212 KB |
n = 2 |
5 |
Correct |
1 ms |
212 KB |
n = 2 |
6 |
Correct |
0 ms |
212 KB |
n = 2 |
7 |
Correct |
0 ms |
212 KB |
n = 3 |
8 |
Correct |
0 ms |
212 KB |
n = 3 |
9 |
Correct |
0 ms |
212 KB |
n = 3 |
10 |
Correct |
0 ms |
212 KB |
n = 8 |
11 |
Incorrect |
0 ms |
212 KB |
answer is not correct: 187084041 instead of 189002015 |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
n = 2 |
2 |
Correct |
0 ms |
212 KB |
n = 2 |
3 |
Correct |
0 ms |
212 KB |
n = 2 |
4 |
Correct |
1 ms |
212 KB |
n = 2 |
5 |
Correct |
1 ms |
212 KB |
n = 2 |
6 |
Correct |
0 ms |
212 KB |
n = 2 |
7 |
Correct |
0 ms |
212 KB |
n = 3 |
8 |
Correct |
0 ms |
212 KB |
n = 3 |
9 |
Correct |
0 ms |
212 KB |
n = 3 |
10 |
Correct |
0 ms |
212 KB |
n = 8 |
11 |
Incorrect |
0 ms |
212 KB |
answer is not correct: 187084041 instead of 189002015 |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
837 ms |
56556 KB |
n = 199999 |
2 |
Correct |
835 ms |
56556 KB |
n = 199991 |
3 |
Correct |
836 ms |
56680 KB |
n = 199993 |
4 |
Correct |
609 ms |
47480 KB |
n = 152076 |
5 |
Correct |
312 ms |
27680 KB |
n = 93249 |
6 |
Correct |
624 ms |
49720 KB |
n = 199910 |
7 |
Correct |
667 ms |
55736 KB |
n = 199999 |
8 |
Correct |
597 ms |
49992 KB |
n = 199997 |
9 |
Correct |
665 ms |
51748 KB |
n = 171294 |
10 |
Correct |
553 ms |
45020 KB |
n = 140872 |
11 |
Incorrect |
655 ms |
50340 KB |
answer is not correct: 0 instead of 1 |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
n = 2 |
2 |
Correct |
0 ms |
212 KB |
n = 2 |
3 |
Correct |
0 ms |
212 KB |
n = 2 |
4 |
Correct |
1 ms |
212 KB |
n = 2 |
5 |
Correct |
1 ms |
212 KB |
n = 2 |
6 |
Correct |
0 ms |
212 KB |
n = 2 |
7 |
Correct |
0 ms |
212 KB |
n = 3 |
8 |
Correct |
0 ms |
212 KB |
n = 3 |
9 |
Correct |
0 ms |
212 KB |
n = 3 |
10 |
Correct |
0 ms |
212 KB |
n = 8 |
11 |
Incorrect |
0 ms |
212 KB |
answer is not correct: 187084041 instead of 189002015 |
12 |
Halted |
0 ms |
0 KB |
- |