#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());
struct node{
int s,e,m;
set<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.insert(j);
if (s==e) return;
if (i<=m) l->ins(i,j);
else r->ins(i,j);
}
void rem(int i,ii j){
v.erase(j);
if (s==e) return;
if (i<=m) l->rem(i,j);
else r->rem(i,j);
}
int query(int i,int j,int k){
if (s==i && e==j){
if (v.empty()) return -1;
if ((*v.begin()).fi<=k) return (*v.begin()).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;
}
}
} *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));
}
}
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;
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}));
root1->rem(v[x].dt,ii(v[x].l+v[x].t,x));
root2->rem(v[x].dt,ii(v[x].l-v[x].t,x));
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;
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}));
root1->rem(v[x].dt,ii(v[x].l+v[x].t,x));
root2->rem(v[x].dt,ii(v[x].l-v[x].t,x));
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:27:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
27 | s=_s,e=_e,m=s+e>>1;
| ~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1322 ms |
266956 KB |
Output is correct |
2 |
Correct |
1095 ms |
267056 KB |
Output is correct |
3 |
Correct |
1091 ms |
214596 KB |
Output is correct |
4 |
Correct |
1144 ms |
214732 KB |
Output is correct |
5 |
Correct |
1246 ms |
273920 KB |
Output is correct |
6 |
Correct |
1138 ms |
267524 KB |
Output is correct |
7 |
Correct |
1211 ms |
266956 KB |
Output is correct |
8 |
Correct |
865 ms |
264716 KB |
Output is correct |
9 |
Correct |
797 ms |
266660 KB |
Output is correct |
10 |
Correct |
633 ms |
266908 KB |
Output is correct |
11 |
Correct |
1405 ms |
276208 KB |
Output is correct |
12 |
Correct |
1517 ms |
275912 KB |
Output is correct |
13 |
Correct |
1425 ms |
276212 KB |
Output is correct |
14 |
Correct |
1411 ms |
275984 KB |
Output is correct |
15 |
Correct |
1329 ms |
267200 KB |
Output is correct |
16 |
Correct |
1310 ms |
266928 KB |
Output is correct |
17 |
Correct |
1283 ms |
267156 KB |
Output is correct |
18 |
Correct |
1382 ms |
276192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
37836 KB |
Output is correct |
2 |
Correct |
30 ms |
37820 KB |
Output is correct |
3 |
Correct |
31 ms |
37868 KB |
Output is correct |
4 |
Correct |
30 ms |
37796 KB |
Output is correct |
5 |
Correct |
31 ms |
37828 KB |
Output is correct |
6 |
Correct |
35 ms |
37868 KB |
Output is correct |
7 |
Correct |
30 ms |
37828 KB |
Output is correct |
8 |
Correct |
29 ms |
37864 KB |
Output is correct |
9 |
Correct |
30 ms |
37888 KB |
Output is correct |
10 |
Correct |
30 ms |
37860 KB |
Output is correct |
11 |
Correct |
30 ms |
37880 KB |
Output is correct |
12 |
Correct |
29 ms |
37876 KB |
Output is correct |
13 |
Correct |
29 ms |
37888 KB |
Output is correct |
14 |
Correct |
33 ms |
37836 KB |
Output is correct |
15 |
Correct |
29 ms |
37876 KB |
Output is correct |
16 |
Correct |
30 ms |
37868 KB |
Output is correct |
17 |
Correct |
30 ms |
37836 KB |
Output is correct |
18 |
Correct |
29 ms |
37908 KB |
Output is correct |
19 |
Correct |
29 ms |
37812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
37836 KB |
Output is correct |
2 |
Correct |
30 ms |
37820 KB |
Output is correct |
3 |
Correct |
31 ms |
37868 KB |
Output is correct |
4 |
Correct |
30 ms |
37796 KB |
Output is correct |
5 |
Correct |
31 ms |
37828 KB |
Output is correct |
6 |
Correct |
35 ms |
37868 KB |
Output is correct |
7 |
Correct |
30 ms |
37828 KB |
Output is correct |
8 |
Correct |
29 ms |
37864 KB |
Output is correct |
9 |
Correct |
30 ms |
37888 KB |
Output is correct |
10 |
Correct |
30 ms |
37860 KB |
Output is correct |
11 |
Correct |
30 ms |
37880 KB |
Output is correct |
12 |
Correct |
29 ms |
37876 KB |
Output is correct |
13 |
Correct |
29 ms |
37888 KB |
Output is correct |
14 |
Correct |
33 ms |
37836 KB |
Output is correct |
15 |
Correct |
29 ms |
37876 KB |
Output is correct |
16 |
Correct |
30 ms |
37868 KB |
Output is correct |
17 |
Correct |
30 ms |
37836 KB |
Output is correct |
18 |
Correct |
29 ms |
37908 KB |
Output is correct |
19 |
Correct |
29 ms |
37812 KB |
Output is correct |
20 |
Correct |
78 ms |
46788 KB |
Output is correct |
21 |
Correct |
81 ms |
46708 KB |
Output is correct |
22 |
Correct |
114 ms |
49220 KB |
Output is correct |
23 |
Correct |
82 ms |
49216 KB |
Output is correct |
24 |
Correct |
84 ms |
47884 KB |
Output is correct |
25 |
Correct |
93 ms |
49304 KB |
Output is correct |
26 |
Correct |
85 ms |
49492 KB |
Output is correct |
27 |
Correct |
87 ms |
49408 KB |
Output is correct |
28 |
Correct |
93 ms |
47940 KB |
Output is correct |
29 |
Correct |
87 ms |
49196 KB |
Output is correct |
30 |
Correct |
59 ms |
49476 KB |
Output is correct |
31 |
Correct |
63 ms |
49472 KB |
Output is correct |
32 |
Correct |
105 ms |
49928 KB |
Output is correct |
33 |
Correct |
105 ms |
49928 KB |
Output is correct |
34 |
Correct |
111 ms |
49468 KB |
Output is correct |
35 |
Correct |
109 ms |
49988 KB |
Output is correct |
36 |
Correct |
101 ms |
49964 KB |
Output is correct |
37 |
Correct |
100 ms |
49384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1322 ms |
266956 KB |
Output is correct |
2 |
Correct |
1095 ms |
267056 KB |
Output is correct |
3 |
Correct |
1091 ms |
214596 KB |
Output is correct |
4 |
Correct |
1144 ms |
214732 KB |
Output is correct |
5 |
Correct |
1246 ms |
273920 KB |
Output is correct |
6 |
Correct |
1138 ms |
267524 KB |
Output is correct |
7 |
Correct |
1211 ms |
266956 KB |
Output is correct |
8 |
Correct |
865 ms |
264716 KB |
Output is correct |
9 |
Correct |
797 ms |
266660 KB |
Output is correct |
10 |
Correct |
633 ms |
266908 KB |
Output is correct |
11 |
Correct |
1405 ms |
276208 KB |
Output is correct |
12 |
Correct |
1517 ms |
275912 KB |
Output is correct |
13 |
Correct |
1425 ms |
276212 KB |
Output is correct |
14 |
Correct |
1411 ms |
275984 KB |
Output is correct |
15 |
Correct |
1329 ms |
267200 KB |
Output is correct |
16 |
Correct |
1310 ms |
266928 KB |
Output is correct |
17 |
Correct |
1283 ms |
267156 KB |
Output is correct |
18 |
Correct |
1382 ms |
276192 KB |
Output is correct |
19 |
Correct |
30 ms |
37836 KB |
Output is correct |
20 |
Correct |
30 ms |
37820 KB |
Output is correct |
21 |
Correct |
31 ms |
37868 KB |
Output is correct |
22 |
Correct |
30 ms |
37796 KB |
Output is correct |
23 |
Correct |
31 ms |
37828 KB |
Output is correct |
24 |
Correct |
35 ms |
37868 KB |
Output is correct |
25 |
Correct |
30 ms |
37828 KB |
Output is correct |
26 |
Correct |
29 ms |
37864 KB |
Output is correct |
27 |
Correct |
30 ms |
37888 KB |
Output is correct |
28 |
Correct |
30 ms |
37860 KB |
Output is correct |
29 |
Correct |
30 ms |
37880 KB |
Output is correct |
30 |
Correct |
29 ms |
37876 KB |
Output is correct |
31 |
Correct |
29 ms |
37888 KB |
Output is correct |
32 |
Correct |
33 ms |
37836 KB |
Output is correct |
33 |
Correct |
29 ms |
37876 KB |
Output is correct |
34 |
Correct |
30 ms |
37868 KB |
Output is correct |
35 |
Correct |
30 ms |
37836 KB |
Output is correct |
36 |
Correct |
29 ms |
37908 KB |
Output is correct |
37 |
Correct |
29 ms |
37812 KB |
Output is correct |
38 |
Correct |
78 ms |
46788 KB |
Output is correct |
39 |
Correct |
81 ms |
46708 KB |
Output is correct |
40 |
Correct |
114 ms |
49220 KB |
Output is correct |
41 |
Correct |
82 ms |
49216 KB |
Output is correct |
42 |
Correct |
84 ms |
47884 KB |
Output is correct |
43 |
Correct |
93 ms |
49304 KB |
Output is correct |
44 |
Correct |
85 ms |
49492 KB |
Output is correct |
45 |
Correct |
87 ms |
49408 KB |
Output is correct |
46 |
Correct |
93 ms |
47940 KB |
Output is correct |
47 |
Correct |
87 ms |
49196 KB |
Output is correct |
48 |
Correct |
59 ms |
49476 KB |
Output is correct |
49 |
Correct |
63 ms |
49472 KB |
Output is correct |
50 |
Correct |
105 ms |
49928 KB |
Output is correct |
51 |
Correct |
105 ms |
49928 KB |
Output is correct |
52 |
Correct |
111 ms |
49468 KB |
Output is correct |
53 |
Correct |
109 ms |
49988 KB |
Output is correct |
54 |
Correct |
101 ms |
49964 KB |
Output is correct |
55 |
Correct |
100 ms |
49384 KB |
Output is correct |
56 |
Correct |
1808 ms |
214068 KB |
Output is correct |
57 |
Correct |
1735 ms |
216712 KB |
Output is correct |
58 |
Correct |
2045 ms |
266648 KB |
Output is correct |
59 |
Correct |
2031 ms |
267128 KB |
Output is correct |
60 |
Correct |
2435 ms |
266780 KB |
Output is correct |
61 |
Correct |
2039 ms |
266712 KB |
Output is correct |
62 |
Correct |
1747 ms |
216288 KB |
Output is correct |
63 |
Correct |
1842 ms |
270872 KB |
Output is correct |
64 |
Correct |
1797 ms |
270872 KB |
Output is correct |
65 |
Correct |
1552 ms |
270204 KB |
Output is correct |
66 |
Correct |
2214 ms |
266940 KB |
Output is correct |
67 |
Correct |
2796 ms |
269656 KB |
Output is correct |
68 |
Correct |
2432 ms |
271424 KB |
Output is correct |
69 |
Correct |
2015 ms |
271096 KB |
Output is correct |
70 |
Execution timed out |
3105 ms |
270564 KB |
Time limit exceeded |
71 |
Halted |
0 ms |
0 KB |
- |