#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=zip.size()-1){
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=zip.size()-1){
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 = _s; t = _t; n = (int)s.size();
s.push_back(INF); t.push_back(0);
rep(i,0,n+1){
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+1){
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,i+1});;
int cnt = segtree.qry(i);
if(cnt > 0){
dsu.unite(i+1,i);
// 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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
n = 2 |
2 |
Correct |
1 ms |
212 KB |
n = 2 |
3 |
Correct |
1 ms |
308 KB |
n = 2 |
4 |
Correct |
1 ms |
212 KB |
n = 2 |
5 |
Correct |
1 ms |
212 KB |
n = 2 |
6 |
Correct |
1 ms |
212 KB |
n = 2 |
7 |
Correct |
0 ms |
276 KB |
n = 3 |
8 |
Correct |
0 ms |
212 KB |
n = 3 |
9 |
Correct |
1 ms |
212 KB |
n = 3 |
10 |
Correct |
0 ms |
212 KB |
n = 8 |
11 |
Incorrect |
1 ms |
304 KB |
answer is not correct: 187084041 instead of 189002015 |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
n = 2 |
2 |
Correct |
1 ms |
212 KB |
n = 2 |
3 |
Correct |
1 ms |
308 KB |
n = 2 |
4 |
Correct |
1 ms |
212 KB |
n = 2 |
5 |
Correct |
1 ms |
212 KB |
n = 2 |
6 |
Correct |
1 ms |
212 KB |
n = 2 |
7 |
Correct |
0 ms |
276 KB |
n = 3 |
8 |
Correct |
0 ms |
212 KB |
n = 3 |
9 |
Correct |
1 ms |
212 KB |
n = 3 |
10 |
Correct |
0 ms |
212 KB |
n = 8 |
11 |
Incorrect |
1 ms |
304 KB |
answer is not correct: 187084041 instead of 189002015 |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
769 ms |
40140 KB |
n = 199999 |
2 |
Correct |
713 ms |
40092 KB |
n = 199991 |
3 |
Correct |
814 ms |
40168 KB |
n = 199993 |
4 |
Correct |
512 ms |
32332 KB |
n = 152076 |
5 |
Correct |
297 ms |
19436 KB |
n = 93249 |
6 |
Correct |
569 ms |
34492 KB |
n = 199910 |
7 |
Correct |
583 ms |
38848 KB |
n = 199999 |
8 |
Correct |
550 ms |
34688 KB |
n = 199997 |
9 |
Correct |
602 ms |
35784 KB |
n = 171294 |
10 |
Correct |
522 ms |
30872 KB |
n = 140872 |
11 |
Incorrect |
612 ms |
34884 KB |
answer is not correct: 0 instead of 1 |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
n = 2 |
2 |
Correct |
1 ms |
212 KB |
n = 2 |
3 |
Correct |
1 ms |
308 KB |
n = 2 |
4 |
Correct |
1 ms |
212 KB |
n = 2 |
5 |
Correct |
1 ms |
212 KB |
n = 2 |
6 |
Correct |
1 ms |
212 KB |
n = 2 |
7 |
Correct |
0 ms |
276 KB |
n = 3 |
8 |
Correct |
0 ms |
212 KB |
n = 3 |
9 |
Correct |
1 ms |
212 KB |
n = 3 |
10 |
Correct |
0 ms |
212 KB |
n = 8 |
11 |
Incorrect |
1 ms |
304 KB |
answer is not correct: 187084041 instead of 189002015 |
12 |
Halted |
0 ms |
0 KB |
- |