#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pii;
pii s[1<<20];
pii merge(pii a, pii b) {return pii(min(a.first,b.first),min(a.second,b.second));};
void update(int l, int r, int idx, int x, ll val, int mode) { //0 - to left-most, 1 - right-most
if (x>r || x<l) return;
if (l==r) {
if (mode==0) s[idx].first=min(s[idx].first,val);
else s[idx].second=min(s[idx].second,val);
} else {
int mid=(l+r)/2;
update(l,mid,2*idx,x,val,mode);
update(mid+1,r,2*idx+1,x,val,mode);
s[idx]=merge(s[2*idx],s[2*idx+1]);
}
}
pii query(int l, int r, int idx, int x, int y) {
if (x>r || y<l) return pii(LLONG_MAX,LLONG_MAX);
if (x<=l && r<=y) return s[idx];
int mid=(l+r)/2;
return merge(query(l,mid,2*idx,x,y),query(mid+1,r,2*idx+1,x,y));
}
int mx,n,a[100001],b[100001],c[100001];
ll w[100001];
set<int> ss;
map<int,int> pos;
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
for (int i=0; i<(1<<20); ++i) s[i]=pii(LLONG_MAX,LLONG_MAX);
cin>>n>>mx;
ss.insert(1);
ss.insert(mx);
for (int i=0; i<n; ++i) {
cin>>a[i]>>b[i]>>c[i]>>w[i];
ss.insert(a[i]);
ss.insert(b[i]);
ss.insert(c[i]);
}
int cnt=0;
for (auto h : ss) pos[h]=++cnt;
ll ans=LLONG_MAX;
for (int i=0; i<n; ++i) {
int l=pos[a[i]], r=pos[b[i]], hole=pos[c[i]];
pii res=query(1,cnt,1,l,r);
ll Ans=w[i], toL=w[i], toR=w[i];
bool haveL=true, haveR=true;
if (r!=cnt) {
if (res.second==LLONG_MAX) haveR=false;
else Ans+=res.second, toR+=res.second;
}
if (l!=1) {
if (res.first==LLONG_MAX) haveL=false;
else Ans+=res.first, toL+=res.first;
}
if (haveL && haveR) ans=min(ans,Ans);
if (haveL) update(1,cnt,1,hole,toL,0);
if (haveR) update(1,cnt,1,hole,toR,1);
}
if (ans==LLONG_MAX) cout<<-1;
else cout<<ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
16736 KB |
Output is correct |
2 |
Correct |
8 ms |
16724 KB |
Output is correct |
3 |
Correct |
8 ms |
16724 KB |
Output is correct |
4 |
Correct |
9 ms |
16668 KB |
Output is correct |
5 |
Correct |
8 ms |
16660 KB |
Output is correct |
6 |
Correct |
7 ms |
16724 KB |
Output is correct |
7 |
Correct |
8 ms |
16696 KB |
Output is correct |
8 |
Correct |
8 ms |
16744 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
16736 KB |
Output is correct |
2 |
Correct |
8 ms |
16724 KB |
Output is correct |
3 |
Correct |
8 ms |
16724 KB |
Output is correct |
4 |
Correct |
9 ms |
16668 KB |
Output is correct |
5 |
Correct |
8 ms |
16660 KB |
Output is correct |
6 |
Correct |
7 ms |
16724 KB |
Output is correct |
7 |
Correct |
8 ms |
16696 KB |
Output is correct |
8 |
Correct |
8 ms |
16744 KB |
Output is correct |
9 |
Correct |
7 ms |
16724 KB |
Output is correct |
10 |
Correct |
7 ms |
16748 KB |
Output is correct |
11 |
Correct |
7 ms |
16740 KB |
Output is correct |
12 |
Correct |
7 ms |
16724 KB |
Output is correct |
13 |
Correct |
8 ms |
16752 KB |
Output is correct |
14 |
Correct |
8 ms |
16724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
16736 KB |
Output is correct |
2 |
Correct |
8 ms |
16724 KB |
Output is correct |
3 |
Correct |
8 ms |
16724 KB |
Output is correct |
4 |
Correct |
9 ms |
16668 KB |
Output is correct |
5 |
Correct |
8 ms |
16660 KB |
Output is correct |
6 |
Correct |
7 ms |
16724 KB |
Output is correct |
7 |
Correct |
8 ms |
16696 KB |
Output is correct |
8 |
Correct |
8 ms |
16744 KB |
Output is correct |
9 |
Correct |
7 ms |
16724 KB |
Output is correct |
10 |
Correct |
7 ms |
16748 KB |
Output is correct |
11 |
Correct |
7 ms |
16740 KB |
Output is correct |
12 |
Correct |
7 ms |
16724 KB |
Output is correct |
13 |
Correct |
8 ms |
16752 KB |
Output is correct |
14 |
Correct |
8 ms |
16724 KB |
Output is correct |
15 |
Correct |
10 ms |
16728 KB |
Output is correct |
16 |
Correct |
10 ms |
16724 KB |
Output is correct |
17 |
Correct |
9 ms |
16852 KB |
Output is correct |
18 |
Correct |
8 ms |
16744 KB |
Output is correct |
19 |
Correct |
9 ms |
17144 KB |
Output is correct |
20 |
Correct |
8 ms |
16892 KB |
Output is correct |
21 |
Correct |
8 ms |
16852 KB |
Output is correct |
22 |
Correct |
9 ms |
16980 KB |
Output is correct |
23 |
Correct |
9 ms |
17060 KB |
Output is correct |
24 |
Correct |
9 ms |
16980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
16736 KB |
Output is correct |
2 |
Correct |
8 ms |
16724 KB |
Output is correct |
3 |
Correct |
8 ms |
16724 KB |
Output is correct |
4 |
Correct |
9 ms |
16668 KB |
Output is correct |
5 |
Correct |
8 ms |
16660 KB |
Output is correct |
6 |
Correct |
7 ms |
16724 KB |
Output is correct |
7 |
Correct |
8 ms |
16696 KB |
Output is correct |
8 |
Correct |
8 ms |
16744 KB |
Output is correct |
9 |
Correct |
7 ms |
16724 KB |
Output is correct |
10 |
Correct |
7 ms |
16748 KB |
Output is correct |
11 |
Correct |
7 ms |
16740 KB |
Output is correct |
12 |
Correct |
7 ms |
16724 KB |
Output is correct |
13 |
Correct |
8 ms |
16752 KB |
Output is correct |
14 |
Correct |
8 ms |
16724 KB |
Output is correct |
15 |
Correct |
10 ms |
16728 KB |
Output is correct |
16 |
Correct |
10 ms |
16724 KB |
Output is correct |
17 |
Correct |
9 ms |
16852 KB |
Output is correct |
18 |
Correct |
8 ms |
16744 KB |
Output is correct |
19 |
Correct |
9 ms |
17144 KB |
Output is correct |
20 |
Correct |
8 ms |
16892 KB |
Output is correct |
21 |
Correct |
8 ms |
16852 KB |
Output is correct |
22 |
Correct |
9 ms |
16980 KB |
Output is correct |
23 |
Correct |
9 ms |
17060 KB |
Output is correct |
24 |
Correct |
9 ms |
16980 KB |
Output is correct |
25 |
Correct |
25 ms |
18860 KB |
Output is correct |
26 |
Correct |
79 ms |
23292 KB |
Output is correct |
27 |
Correct |
175 ms |
30164 KB |
Output is correct |
28 |
Correct |
114 ms |
22764 KB |
Output is correct |
29 |
Correct |
128 ms |
28152 KB |
Output is correct |
30 |
Correct |
157 ms |
24176 KB |
Output is correct |
31 |
Correct |
302 ms |
38312 KB |
Output is correct |
32 |
Correct |
315 ms |
33868 KB |
Output is correct |
33 |
Correct |
42 ms |
23216 KB |
Output is correct |
34 |
Correct |
139 ms |
33680 KB |
Output is correct |
35 |
Correct |
226 ms |
50688 KB |
Output is correct |
36 |
Correct |
419 ms |
50708 KB |
Output is correct |
37 |
Correct |
322 ms |
50720 KB |
Output is correct |
38 |
Correct |
322 ms |
50668 KB |
Output is correct |