Submission #241693

# Submission time Handle Problem Language Result Execution time Memory
241693 2020-06-25T09:22:53 Z AMO5 Treatment Project (JOI20_treatment) C++
100 / 100
248 ms 52100 KB
#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define eb emplace_back
#define mt make_tuple
#define all(x) (x).begin(), (x).end() 
#define MOD 1000000007

typedef long long ll;
typedef pair <int, int> ii;
typedef pair <ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef long double ld;
typedef pair<ll,int>pli;
const ll INF=1e18;
const int mxn=1e5+5;

/* T_i<=T_j, L_j+T_j<=R_i+T_i
 * T_i>=T_j, L_j-T_j<=R_i-T_i
 * connect them with edge then perform Dij
 * start from 1 -> n, note that the citizen 1 and n only affected by citizen 2 and n-1 respectively
*/

struct st{
	int t,le,ri,cost;
	st(int t,int le, int ri, int cost):t(t),le(le),ri(ri),cost(cost){};
};

struct point{
	int m, p, ind;
	point(int m, int p, int ind):m(m),p(p),ind(ind){};
	bool operator < (const point &r)const {
		return m<r.m;
	}
};

struct node{
	int ind; ll d;
	bool operator < (const node &r) const {
		return d>r.d;
	}
};

vector<st>str;
vector<point>pt;
vector<pli>tree[mxn*4];
bool vis[mxn];

void build(int id, int le, int ri){
	if(le==ri){
		tree[id].eb(pt[le].p,pt[le].ind);
	}else{
		int mid=(le+ri)/2;
		build(id*2,le,mid);
		build(id*2+1,mid+1,ri);
		tree[id].resize(tree[id*2].size()+tree[id*2+1].size());
		merge(all(tree[id*2]),all(tree[id*2+1]),tree[id].begin(),greater<pli>());
	}
	return;
}

void getnodes(int pos, int val, int id, int le, int ri, vi &v){
	if(le>=pos)return;
	if(ri<pos){
		while(!tree[id].empty()&&tree[id].back().fi<=val){
			//fi = point.p = le+t, val = ri+t, le+t<=ri+t for t_i<=t_j
			int ind = tree[id].back().se;
			if(!vis[ind])v.eb(ind);
			tree[id].pop_back();
		}
		return;
	}
	int mid=(le+ri)/2;
	getnodes(pos,val,id*2,le,mid,v);
	getnodes(pos,val,id*2+1,mid+1,ri,v);
	return;
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0);
    //freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
	int n,m; cin >> n >> m;
	int t,le,ri,cost;
	for(int i=0; i<m; i++){
		cin >> t >> le >> ri >> cost;
		str.eb(t,le,ri,cost);
		pt.eb(le-t,le+t,i);
	}
	sort(all(pt));
	build(1,0,m-1);
	/*
	for(int i=0; i<m; i++){
		cout << i << ' ' << pt[i].m << ' ' << pt[i].p << ' ' << pt[i].ind << '\n';
	}
	*/ 
	ll dist[m];
	for(int i=0; i<m; i++)dist[i]=INF,vis[i]=0;
	priority_queue<node>pq;
	for(int i=0; i<m; i++){
		if(str[i].le==1){
			vis[i]=1;
			dist[i]=(ll)str[i].cost;
			pq.push({i,dist[i]});
		}
	}
	while(!pq.empty()){
		int u = pq.top().ind;
		pq.pop();
		vi vt;
		int pos = lower_bound(all(pt),point(str[u].ri-str[u].t+2,-1,-1))-pt.begin();
		int val = str[u].ri+str[u].t+1;
		getnodes(pos,val,1,0,m-1,vt);
		for(int v:vt){
			if(vis[v])continue;
			vis[v]=1;
			dist[v] = dist[u]+(ll)str[v].cost;
			pq.push({v,dist[v]});
		}
		 
	}
	
	ll ans = INF;
	for(int i=0; i<m; i++){
		if(str[i].ri==n){
			ans = min(ans,dist[i]);
		}
	}
	if(ans==INF)ans=-1;
	cout << ans << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 153 ms 45820 KB Output is correct
2 Correct 141 ms 47364 KB Output is correct
3 Correct 150 ms 48240 KB Output is correct
4 Correct 153 ms 48220 KB Output is correct
5 Correct 175 ms 50308 KB Output is correct
6 Correct 168 ms 47612 KB Output is correct
7 Correct 154 ms 47232 KB Output is correct
8 Correct 123 ms 47360 KB Output is correct
9 Correct 113 ms 47232 KB Output is correct
10 Correct 122 ms 47364 KB Output is correct
11 Correct 181 ms 50820 KB Output is correct
12 Correct 184 ms 50812 KB Output is correct
13 Correct 211 ms 50300 KB Output is correct
14 Correct 201 ms 50300 KB Output is correct
15 Correct 175 ms 47360 KB Output is correct
16 Correct 163 ms 47364 KB Output is correct
17 Correct 158 ms 46824 KB Output is correct
18 Correct 190 ms 50172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 9728 KB Output is correct
2 Correct 10 ms 9728 KB Output is correct
3 Correct 10 ms 9728 KB Output is correct
4 Correct 10 ms 9728 KB Output is correct
5 Correct 11 ms 9728 KB Output is correct
6 Correct 10 ms 9728 KB Output is correct
7 Correct 11 ms 9728 KB Output is correct
8 Correct 10 ms 9728 KB Output is correct
9 Correct 11 ms 9728 KB Output is correct
10 Correct 13 ms 9728 KB Output is correct
11 Correct 11 ms 9728 KB Output is correct
12 Correct 12 ms 9728 KB Output is correct
13 Correct 10 ms 9728 KB Output is correct
14 Correct 11 ms 9728 KB Output is correct
15 Correct 11 ms 9728 KB Output is correct
16 Correct 10 ms 9728 KB Output is correct
17 Correct 10 ms 9728 KB Output is correct
18 Correct 11 ms 9728 KB Output is correct
19 Correct 11 ms 9728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 9728 KB Output is correct
2 Correct 10 ms 9728 KB Output is correct
3 Correct 10 ms 9728 KB Output is correct
4 Correct 10 ms 9728 KB Output is correct
5 Correct 11 ms 9728 KB Output is correct
6 Correct 10 ms 9728 KB Output is correct
7 Correct 11 ms 9728 KB Output is correct
8 Correct 10 ms 9728 KB Output is correct
9 Correct 11 ms 9728 KB Output is correct
10 Correct 13 ms 9728 KB Output is correct
11 Correct 11 ms 9728 KB Output is correct
12 Correct 12 ms 9728 KB Output is correct
13 Correct 10 ms 9728 KB Output is correct
14 Correct 11 ms 9728 KB Output is correct
15 Correct 11 ms 9728 KB Output is correct
16 Correct 10 ms 9728 KB Output is correct
17 Correct 10 ms 9728 KB Output is correct
18 Correct 11 ms 9728 KB Output is correct
19 Correct 11 ms 9728 KB Output is correct
20 Correct 17 ms 11392 KB Output is correct
21 Correct 17 ms 11392 KB Output is correct
22 Correct 17 ms 11392 KB Output is correct
23 Correct 16 ms 11392 KB Output is correct
24 Correct 18 ms 11520 KB Output is correct
25 Correct 18 ms 11392 KB Output is correct
26 Correct 17 ms 11392 KB Output is correct
27 Correct 18 ms 11392 KB Output is correct
28 Correct 18 ms 11520 KB Output is correct
29 Correct 18 ms 11392 KB Output is correct
30 Correct 15 ms 11392 KB Output is correct
31 Correct 16 ms 11392 KB Output is correct
32 Correct 18 ms 11648 KB Output is correct
33 Correct 18 ms 11648 KB Output is correct
34 Correct 18 ms 11392 KB Output is correct
35 Correct 18 ms 11520 KB Output is correct
36 Correct 17 ms 11520 KB Output is correct
37 Correct 16 ms 11264 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 153 ms 45820 KB Output is correct
2 Correct 141 ms 47364 KB Output is correct
3 Correct 150 ms 48240 KB Output is correct
4 Correct 153 ms 48220 KB Output is correct
5 Correct 175 ms 50308 KB Output is correct
6 Correct 168 ms 47612 KB Output is correct
7 Correct 154 ms 47232 KB Output is correct
8 Correct 123 ms 47360 KB Output is correct
9 Correct 113 ms 47232 KB Output is correct
10 Correct 122 ms 47364 KB Output is correct
11 Correct 181 ms 50820 KB Output is correct
12 Correct 184 ms 50812 KB Output is correct
13 Correct 211 ms 50300 KB Output is correct
14 Correct 201 ms 50300 KB Output is correct
15 Correct 175 ms 47360 KB Output is correct
16 Correct 163 ms 47364 KB Output is correct
17 Correct 158 ms 46824 KB Output is correct
18 Correct 190 ms 50172 KB Output is correct
19 Correct 10 ms 9728 KB Output is correct
20 Correct 10 ms 9728 KB Output is correct
21 Correct 10 ms 9728 KB Output is correct
22 Correct 10 ms 9728 KB Output is correct
23 Correct 11 ms 9728 KB Output is correct
24 Correct 10 ms 9728 KB Output is correct
25 Correct 11 ms 9728 KB Output is correct
26 Correct 10 ms 9728 KB Output is correct
27 Correct 11 ms 9728 KB Output is correct
28 Correct 13 ms 9728 KB Output is correct
29 Correct 11 ms 9728 KB Output is correct
30 Correct 12 ms 9728 KB Output is correct
31 Correct 10 ms 9728 KB Output is correct
32 Correct 11 ms 9728 KB Output is correct
33 Correct 11 ms 9728 KB Output is correct
34 Correct 10 ms 9728 KB Output is correct
35 Correct 10 ms 9728 KB Output is correct
36 Correct 11 ms 9728 KB Output is correct
37 Correct 11 ms 9728 KB Output is correct
38 Correct 17 ms 11392 KB Output is correct
39 Correct 17 ms 11392 KB Output is correct
40 Correct 17 ms 11392 KB Output is correct
41 Correct 16 ms 11392 KB Output is correct
42 Correct 18 ms 11520 KB Output is correct
43 Correct 18 ms 11392 KB Output is correct
44 Correct 17 ms 11392 KB Output is correct
45 Correct 18 ms 11392 KB Output is correct
46 Correct 18 ms 11520 KB Output is correct
47 Correct 18 ms 11392 KB Output is correct
48 Correct 15 ms 11392 KB Output is correct
49 Correct 16 ms 11392 KB Output is correct
50 Correct 18 ms 11648 KB Output is correct
51 Correct 18 ms 11648 KB Output is correct
52 Correct 18 ms 11392 KB Output is correct
53 Correct 18 ms 11520 KB Output is correct
54 Correct 17 ms 11520 KB Output is correct
55 Correct 16 ms 11264 KB Output is correct
56 Correct 171 ms 48532 KB Output is correct
57 Correct 189 ms 48868 KB Output is correct
58 Correct 158 ms 46460 KB Output is correct
59 Correct 167 ms 46964 KB Output is correct
60 Correct 204 ms 47612 KB Output is correct
61 Correct 161 ms 46620 KB Output is correct
62 Correct 161 ms 48516 KB Output is correct
63 Correct 159 ms 47360 KB Output is correct
64 Correct 172 ms 47452 KB Output is correct
65 Correct 157 ms 47492 KB Output is correct
66 Correct 177 ms 47748 KB Output is correct
67 Correct 222 ms 48376 KB Output is correct
68 Correct 218 ms 47996 KB Output is correct
69 Correct 205 ms 48132 KB Output is correct
70 Correct 245 ms 48384 KB Output is correct
71 Correct 214 ms 48132 KB Output is correct
72 Correct 197 ms 48124 KB Output is correct
73 Correct 221 ms 48252 KB Output is correct
74 Correct 131 ms 48096 KB Output is correct
75 Correct 121 ms 48016 KB Output is correct
76 Correct 199 ms 51512 KB Output is correct
77 Correct 225 ms 51332 KB Output is correct
78 Correct 234 ms 50820 KB Output is correct
79 Correct 232 ms 48268 KB Output is correct
80 Correct 180 ms 47744 KB Output is correct
81 Correct 129 ms 47996 KB Output is correct
82 Correct 181 ms 47332 KB Output is correct
83 Correct 183 ms 47232 KB Output is correct
84 Correct 248 ms 47364 KB Output is correct
85 Correct 192 ms 48256 KB Output is correct
86 Correct 165 ms 48004 KB Output is correct
87 Correct 188 ms 48000 KB Output is correct
88 Correct 167 ms 48208 KB Output is correct
89 Correct 163 ms 48004 KB Output is correct
90 Correct 247 ms 51456 KB Output is correct
91 Correct 234 ms 50052 KB Output is correct
92 Correct 178 ms 48132 KB Output is correct
93 Correct 231 ms 48004 KB Output is correct
94 Correct 186 ms 48252 KB Output is correct
95 Correct 208 ms 47992 KB Output is correct
96 Correct 238 ms 51460 KB Output is correct
97 Correct 224 ms 51204 KB Output is correct
98 Correct 222 ms 51200 KB Output is correct
99 Correct 220 ms 51204 KB Output is correct
100 Correct 206 ms 51072 KB Output is correct
101 Correct 223 ms 51200 KB Output is correct
102 Correct 215 ms 52100 KB Output is correct
103 Correct 197 ms 48644 KB Output is correct