#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ii pair<ll,ll>
#define fi first
#define se second
#define endl '\n'
#define puf push_front
#define pof pop_front
#define pub push_back
#define pob pop_back
#define rep(x,s,e) for (auto x=s-(s>e);x!=e-(s>e);s<e?x++:x--)
#define all(x) (x).begin(),(x).end()
#define sz(x) (int) (x).size()
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
bool used[100005];
struct node{
int s,e,m;
vector<ii> v;
node *l,*r;
node (int _s,int _e){
s=_s,e=_e,m=s+e>>1;
if (s!=e){
l=new node(s,m);
r=new node(m+1,e);
}
}
void ins(int i,ii j){
v.pub(j);
if (s==e) return;
if (i<=m) l->ins(i,j);
else r->ins(i,j);
}
int query(int i,int j,int k){
if (s==i && e==j){
while (!v.empty() && used[v.back().se]) v.pob();
if (v.empty()) return -1;
if (v.back().fi<=k) return v.back().se;
else return -1;
}
else if (j<=m) return l->query(i,j,k);
else if (m<i) return r->query(i,j,k);
else{
int temp=l->query(i,m,k);
if (temp==-1) return r->query(m+1,j,k);
else return temp;
}
}
void proc(){
sort(all(v),[](ii i,ii j){
return i>j;
});
if (s!=e) l->proc(),r->proc();
}
} *root1=new node(0,100005),*root2=new node(0,100005);
int n,k;
struct E{
int l,r;
int t;
ll cost;
int dt; //discretized time
};
vector<E> v;
struct upd{
int segno; //which tree
int i,j,k;
ll val;
};
struct cmp{
bool operator () (upd i,upd j){
return i.val>j.val;
}
};
priority_queue<upd,vector<upd>,cmp> pq;
int main(){
cin.tie(0);
cout.tie(0);
cin.sync_with_stdio(false);
cin>>n>>k;
int a,b,c,d;
rep(x,0,k){
cin>>a>>b>>c>>d;
v.pub({b,c+1,a,d});
}
vector<int> allt;
rep(x,0,k) allt.pub(v[x].t);
sort(all(allt));
allt.erase(unique(all(allt)),allt.end());
map<int,int> tid;
rep(x,0,sz(allt)) tid[allt[x]]=x+1;
rep(x,0,k) v[x].dt=tid[v[x].t];
ll ans=1e18;
rep(x,0,k){
if (v[x].l==1){
if (v[x].r==n+1) ans=min(ans,v[x].cost);
pq.push(upd({1,v[x].dt,100005,v[x].r+v[x].t,v[x].cost}));
pq.push(upd({2,0,v[x].dt-1,v[x].r-v[x].t,v[x].cost}));
}
else{
root1->ins(v[x].dt,ii(v[x].l+v[x].t,x));
root2->ins(v[x].dt,ii(v[x].l-v[x].t,x));
}
}
root1->proc();
root2->proc();
while (!pq.empty()){
upd u=pq.top();
pq.pop();
if (u.segno==1){
while (true){
int x=root1->query(u.i,u.j,u.k);
if (x==-1) break;
used[x]=true;
pq.push(upd({1,v[x].dt,100005,v[x].r+v[x].t,v[x].cost+u.val}));
pq.push(upd({2,0,v[x].dt-1,v[x].r-v[x].t,v[x].cost+u.val}));
if (v[x].r==n+1) ans=min(ans,v[x].cost+u.val);
}
}
else{
while (true){
int x=root2->query(u.i,u.j,u.k);
if (x==-1) break;
used[x]=true;
pq.push(upd({1,v[x].dt,100005,v[x].r+v[x].t,v[x].cost+u.val}));
pq.push(upd({2,0,v[x].dt-1,v[x].r-v[x].t,v[x].cost+u.val}));
if (v[x].r==n+1) ans=min(ans,v[x].cost+u.val);
}
}
}
if (ans==1e18) cout<<"-1"<<endl;
else cout<<ans<<endl;
}
Compilation message
treatment.cpp: In constructor 'node::node(int, int)':
treatment.cpp:29:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
29 | s=_s,e=_e,m=s+e>>1;
| ~^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
576 ms |
98708 KB |
Output is correct |
2 |
Correct |
566 ms |
98576 KB |
Output is correct |
3 |
Correct |
483 ms |
96860 KB |
Output is correct |
4 |
Correct |
476 ms |
96608 KB |
Output is correct |
5 |
Correct |
547 ms |
104416 KB |
Output is correct |
6 |
Correct |
514 ms |
98572 KB |
Output is correct |
7 |
Correct |
469 ms |
98572 KB |
Output is correct |
8 |
Correct |
483 ms |
98240 KB |
Output is correct |
9 |
Correct |
528 ms |
98408 KB |
Output is correct |
10 |
Correct |
417 ms |
98452 KB |
Output is correct |
11 |
Correct |
612 ms |
104760 KB |
Output is correct |
12 |
Correct |
612 ms |
104648 KB |
Output is correct |
13 |
Correct |
610 ms |
104816 KB |
Output is correct |
14 |
Correct |
634 ms |
104648 KB |
Output is correct |
15 |
Correct |
597 ms |
98576 KB |
Output is correct |
16 |
Correct |
583 ms |
98472 KB |
Output is correct |
17 |
Correct |
580 ms |
98572 KB |
Output is correct |
18 |
Correct |
592 ms |
104780 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
25364 KB |
Output is correct |
2 |
Correct |
34 ms |
25312 KB |
Output is correct |
3 |
Correct |
27 ms |
25304 KB |
Output is correct |
4 |
Correct |
28 ms |
25368 KB |
Output is correct |
5 |
Correct |
24 ms |
25288 KB |
Output is correct |
6 |
Correct |
28 ms |
25284 KB |
Output is correct |
7 |
Correct |
27 ms |
25284 KB |
Output is correct |
8 |
Correct |
30 ms |
25340 KB |
Output is correct |
9 |
Correct |
26 ms |
25364 KB |
Output is correct |
10 |
Correct |
30 ms |
25316 KB |
Output is correct |
11 |
Correct |
27 ms |
25292 KB |
Output is correct |
12 |
Correct |
27 ms |
25336 KB |
Output is correct |
13 |
Correct |
26 ms |
25292 KB |
Output is correct |
14 |
Correct |
27 ms |
25340 KB |
Output is correct |
15 |
Correct |
26 ms |
25340 KB |
Output is correct |
16 |
Correct |
27 ms |
25320 KB |
Output is correct |
17 |
Correct |
29 ms |
25284 KB |
Output is correct |
18 |
Correct |
27 ms |
25352 KB |
Output is correct |
19 |
Correct |
29 ms |
25388 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
25364 KB |
Output is correct |
2 |
Correct |
34 ms |
25312 KB |
Output is correct |
3 |
Correct |
27 ms |
25304 KB |
Output is correct |
4 |
Correct |
28 ms |
25368 KB |
Output is correct |
5 |
Correct |
24 ms |
25288 KB |
Output is correct |
6 |
Correct |
28 ms |
25284 KB |
Output is correct |
7 |
Correct |
27 ms |
25284 KB |
Output is correct |
8 |
Correct |
30 ms |
25340 KB |
Output is correct |
9 |
Correct |
26 ms |
25364 KB |
Output is correct |
10 |
Correct |
30 ms |
25316 KB |
Output is correct |
11 |
Correct |
27 ms |
25292 KB |
Output is correct |
12 |
Correct |
27 ms |
25336 KB |
Output is correct |
13 |
Correct |
26 ms |
25292 KB |
Output is correct |
14 |
Correct |
27 ms |
25340 KB |
Output is correct |
15 |
Correct |
26 ms |
25340 KB |
Output is correct |
16 |
Correct |
27 ms |
25320 KB |
Output is correct |
17 |
Correct |
29 ms |
25284 KB |
Output is correct |
18 |
Correct |
27 ms |
25352 KB |
Output is correct |
19 |
Correct |
29 ms |
25388 KB |
Output is correct |
20 |
Correct |
47 ms |
28576 KB |
Output is correct |
21 |
Correct |
45 ms |
28684 KB |
Output is correct |
22 |
Correct |
54 ms |
29120 KB |
Output is correct |
23 |
Correct |
49 ms |
29132 KB |
Output is correct |
24 |
Correct |
52 ms |
29716 KB |
Output is correct |
25 |
Correct |
46 ms |
29512 KB |
Output is correct |
26 |
Correct |
46 ms |
29556 KB |
Output is correct |
27 |
Correct |
47 ms |
29544 KB |
Output is correct |
28 |
Correct |
46 ms |
29632 KB |
Output is correct |
29 |
Correct |
45 ms |
29504 KB |
Output is correct |
30 |
Correct |
44 ms |
29584 KB |
Output is correct |
31 |
Correct |
41 ms |
29552 KB |
Output is correct |
32 |
Correct |
62 ms |
30164 KB |
Output is correct |
33 |
Correct |
51 ms |
30112 KB |
Output is correct |
34 |
Correct |
49 ms |
29772 KB |
Output is correct |
35 |
Correct |
49 ms |
30108 KB |
Output is correct |
36 |
Correct |
49 ms |
30136 KB |
Output is correct |
37 |
Correct |
50 ms |
29760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
576 ms |
98708 KB |
Output is correct |
2 |
Correct |
566 ms |
98576 KB |
Output is correct |
3 |
Correct |
483 ms |
96860 KB |
Output is correct |
4 |
Correct |
476 ms |
96608 KB |
Output is correct |
5 |
Correct |
547 ms |
104416 KB |
Output is correct |
6 |
Correct |
514 ms |
98572 KB |
Output is correct |
7 |
Correct |
469 ms |
98572 KB |
Output is correct |
8 |
Correct |
483 ms |
98240 KB |
Output is correct |
9 |
Correct |
528 ms |
98408 KB |
Output is correct |
10 |
Correct |
417 ms |
98452 KB |
Output is correct |
11 |
Correct |
612 ms |
104760 KB |
Output is correct |
12 |
Correct |
612 ms |
104648 KB |
Output is correct |
13 |
Correct |
610 ms |
104816 KB |
Output is correct |
14 |
Correct |
634 ms |
104648 KB |
Output is correct |
15 |
Correct |
597 ms |
98576 KB |
Output is correct |
16 |
Correct |
583 ms |
98472 KB |
Output is correct |
17 |
Correct |
580 ms |
98572 KB |
Output is correct |
18 |
Correct |
592 ms |
104780 KB |
Output is correct |
19 |
Correct |
29 ms |
25364 KB |
Output is correct |
20 |
Correct |
34 ms |
25312 KB |
Output is correct |
21 |
Correct |
27 ms |
25304 KB |
Output is correct |
22 |
Correct |
28 ms |
25368 KB |
Output is correct |
23 |
Correct |
24 ms |
25288 KB |
Output is correct |
24 |
Correct |
28 ms |
25284 KB |
Output is correct |
25 |
Correct |
27 ms |
25284 KB |
Output is correct |
26 |
Correct |
30 ms |
25340 KB |
Output is correct |
27 |
Correct |
26 ms |
25364 KB |
Output is correct |
28 |
Correct |
30 ms |
25316 KB |
Output is correct |
29 |
Correct |
27 ms |
25292 KB |
Output is correct |
30 |
Correct |
27 ms |
25336 KB |
Output is correct |
31 |
Correct |
26 ms |
25292 KB |
Output is correct |
32 |
Correct |
27 ms |
25340 KB |
Output is correct |
33 |
Correct |
26 ms |
25340 KB |
Output is correct |
34 |
Correct |
27 ms |
25320 KB |
Output is correct |
35 |
Correct |
29 ms |
25284 KB |
Output is correct |
36 |
Correct |
27 ms |
25352 KB |
Output is correct |
37 |
Correct |
29 ms |
25388 KB |
Output is correct |
38 |
Correct |
47 ms |
28576 KB |
Output is correct |
39 |
Correct |
45 ms |
28684 KB |
Output is correct |
40 |
Correct |
54 ms |
29120 KB |
Output is correct |
41 |
Correct |
49 ms |
29132 KB |
Output is correct |
42 |
Correct |
52 ms |
29716 KB |
Output is correct |
43 |
Correct |
46 ms |
29512 KB |
Output is correct |
44 |
Correct |
46 ms |
29556 KB |
Output is correct |
45 |
Correct |
47 ms |
29544 KB |
Output is correct |
46 |
Correct |
46 ms |
29632 KB |
Output is correct |
47 |
Correct |
45 ms |
29504 KB |
Output is correct |
48 |
Correct |
44 ms |
29584 KB |
Output is correct |
49 |
Correct |
41 ms |
29552 KB |
Output is correct |
50 |
Correct |
62 ms |
30164 KB |
Output is correct |
51 |
Correct |
51 ms |
30112 KB |
Output is correct |
52 |
Correct |
49 ms |
29772 KB |
Output is correct |
53 |
Correct |
49 ms |
30108 KB |
Output is correct |
54 |
Correct |
49 ms |
30136 KB |
Output is correct |
55 |
Correct |
50 ms |
29760 KB |
Output is correct |
56 |
Correct |
597 ms |
89940 KB |
Output is correct |
57 |
Correct |
612 ms |
90008 KB |
Output is correct |
58 |
Correct |
740 ms |
101588 KB |
Output is correct |
59 |
Correct |
779 ms |
101536 KB |
Output is correct |
60 |
Correct |
640 ms |
93980 KB |
Output is correct |
61 |
Correct |
763 ms |
101676 KB |
Output is correct |
62 |
Correct |
603 ms |
90152 KB |
Output is correct |
63 |
Correct |
712 ms |
107060 KB |
Output is correct |
64 |
Correct |
716 ms |
107100 KB |
Output is correct |
65 |
Correct |
574 ms |
98088 KB |
Output is correct |
66 |
Correct |
600 ms |
93836 KB |
Output is correct |
67 |
Correct |
674 ms |
106340 KB |
Output is correct |
68 |
Correct |
523 ms |
105748 KB |
Output is correct |
69 |
Correct |
465 ms |
101800 KB |
Output is correct |
70 |
Correct |
776 ms |
106892 KB |
Output is correct |
71 |
Correct |
560 ms |
109684 KB |
Output is correct |
72 |
Correct |
475 ms |
109360 KB |
Output is correct |
73 |
Correct |
772 ms |
110420 KB |
Output is correct |
74 |
Correct |
372 ms |
109736 KB |
Output is correct |
75 |
Correct |
345 ms |
109148 KB |
Output is correct |
76 |
Correct |
818 ms |
124328 KB |
Output is correct |
77 |
Correct |
855 ms |
120312 KB |
Output is correct |
78 |
Correct |
844 ms |
124072 KB |
Output is correct |
79 |
Correct |
854 ms |
111356 KB |
Output is correct |
80 |
Correct |
847 ms |
115296 KB |
Output is correct |
81 |
Correct |
633 ms |
111180 KB |
Output is correct |
82 |
Correct |
818 ms |
114928 KB |
Output is correct |
83 |
Correct |
860 ms |
110264 KB |
Output is correct |
84 |
Correct |
853 ms |
110548 KB |
Output is correct |
85 |
Correct |
747 ms |
110940 KB |
Output is correct |
86 |
Correct |
748 ms |
111012 KB |
Output is correct |
87 |
Correct |
770 ms |
110904 KB |
Output is correct |
88 |
Correct |
758 ms |
110284 KB |
Output is correct |
89 |
Correct |
755 ms |
110788 KB |
Output is correct |
90 |
Correct |
859 ms |
120084 KB |
Output is correct |
91 |
Correct |
809 ms |
116316 KB |
Output is correct |
92 |
Correct |
764 ms |
110920 KB |
Output is correct |
93 |
Correct |
871 ms |
111196 KB |
Output is correct |
94 |
Correct |
884 ms |
111224 KB |
Output is correct |
95 |
Correct |
851 ms |
111056 KB |
Output is correct |
96 |
Correct |
896 ms |
120240 KB |
Output is correct |
97 |
Correct |
883 ms |
120168 KB |
Output is correct |
98 |
Correct |
909 ms |
120156 KB |
Output is correct |
99 |
Correct |
871 ms |
120156 KB |
Output is correct |
100 |
Correct |
846 ms |
114984 KB |
Output is correct |
101 |
Correct |
902 ms |
120016 KB |
Output is correct |
102 |
Correct |
919 ms |
120124 KB |
Output is correct |
103 |
Correct |
839 ms |
112296 KB |
Output is correct |