Submission #261203

# Submission time Handle Problem Language Result Execution time Memory
261203 2020-08-11T14:20:27 Z amoo_safar Treatment Project (JOI20_treatment) C++17
100 / 100
459 ms 28524 KB
// Zende bad Shoma nasime faghat !
#include <bits/stdc++.h>

#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " : " << x << '\n'

using namespace std;

typedef long long ll;
typedef long double ld;
typedef string str;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;

const ll Mod = 1000000007LL;
const int N = 2e5 + 10;
const ll Inf = 2242545357980376863LL;
const ll Log = 30;
const int Inf2 = 2'000'000'000;

int n, m;
int T[N], L[N], R[N], C[N], mk[N];
int Tc[N];

ll dis[N];

struct Segment {
	pii seg[N << 2];
	Segment (){
		memset(seg, 0, sizeof seg);
	}
	pii Get(int id, int l, int r, int L, int R){
		if(r <= L || R <= l) return pii(Inf2, -1);
		if(l <= L && R <= r) return seg[id];
		int mid = (L + R) >> 1;
		return min(Get(id << 1, l, r, L, mid), Get(id << 1 | 1, l, r, mid, R));
	}
	void Set(int id, int idx, int v, int L, int R){
		if(R - L == 1){
			seg[id] = {v, L};
			return ;
		}
		int mid = (L + R) >> 1;
		if(idx < mid)
			Set(id << 1, idx, v, L, mid);
		else
			Set(id << 1 | 1, idx, v, mid, R);

		seg[id] = min(seg[id << 1], seg[id << 1 | 1]);
	}
};
Segment S1, S2;


bool Edge(int idx, int j){
	if(j == -1) return false;
	if(mk[j]) return false;

	if(T[j] <= T[idx]){
		if(L[j] + (T[idx] - T[j]) <= R[idx] + 1)
			return true;
	}
	if(T[j] > T[idx]){
		if(R[idx] - (T[j] - T[idx]) >= L[j] - 1)
			return true;
	}
	return false;
}

int main(){
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n >> m;
	vector<pll> A;
	for(int i = 1; i <= m; i++){
		cin >> T[i] >> L[i] >> R[i] >> C[i];
		A.pb({T[i], i});
	}

	/*
	cerr << "####\n";
	for(int i = 1; i <= m; i++) for(int j = 1; j <= m; j++)
		if(Edge(i, j))
			cerr << i << " -> " << j << '\n';
	cerr << "###\n";
	*/
	sort(all(A));
	for(int i = 0; i < m; i++) Tc[A[i].S] = i;


	ll ans = Inf;
	set<pll> st;
	memset(dis, 31, sizeof dis);
	for(int i = 1; i <= m; i++){
		if(L[i] == 1){
			dis[i] = C[i];
			st.insert({dis[i], i});
			S1.Set(1, Tc[i], Inf2, 0, m);
			S2.Set(1, Tc[i], Inf2, 0, m);
		} else {
			S1.Set(1, Tc[i], L[i] - T[i], 0, m);
			S2.Set(1, Tc[i], L[i] + T[i], 0, m);
		}
	}

	while(!st.empty()){
		int idx = st.begin()->S; 
		st.erase(st.begin());
		//debug(idx);
		/*
		ll mn = Inf, idx = -1;
		for(int i = 1; i <= m; i++){
			if(mk[i]) continue;
			if(dis[i] < mn){
				mn = dis[i];
				idx = i;
			}
		}
		if(idx == -1) break;
		*/
		int RT = Tc[idx];
		//S1.Set(1, RT, Inf, 0, m);
		//S2.Set(1, RT, Inf, 0, m);

		//mk[idx] = 1;
		if(R[idx] == n) ans = min(ans, dis[idx]);

		//debug(idx);
		while(true){
			int al = S1.Get(1, 0, RT, 0, m).S;
			if(al == -1) break;
			int wh = A[al].S;
			//cerr << idx << ' ' << wh << '\n';
			if(!Edge(idx, wh)) break;
			S1.Set(1, Tc[wh], Inf2, 0, m);
			S2.Set(1, Tc[wh], Inf2, 0, m);
			
			mk[wh] = 1;
			dis[wh] = dis[idx] + C[wh];
			st.insert({dis[wh], wh});
		}

		while(true){

			int al = S2.Get(1, RT, m, 0, m).S;
			if(al == -1) break;
			int wh = A[al].S;
			//int wh = A[S2.Get(1, RT, m, 0, m).S].S;
			//cerr << idx << ' ' << wh << '\n';
			if(!Edge(idx, wh)) break;
			S1.Set(1, Tc[wh], Inf2, 0, m);
			S2.Set(1, Tc[wh], Inf2, 0, m);
			
			mk[wh] = 1;
			dis[wh] = dis[idx] + C[wh];
			st.insert({dis[wh], wh});
		}
		/*
		for(int j = 1; j <= m; j++){
			//if(L[j] > R[idx] + 1) continue;
			if(mk[j]) continue;

			if(T[j] <= T[idx]){
				if(L[j] + (T[idx] - T[j]) <= R[idx] + 1)
					dis[j] = min(dis[j], dis[idx] + C[j]);
			}
			if(T[j] > T[idx]){
				if(R[idx] - (T[j] - T[idx]) >= L[j] - 1)
					dis[j] = min(dis[j], dis[idx] + C[j]);
			}
		}
		*/
	}

	if(ans >= Inf) return cout << "-1\n", 0;
	cout << ans << '\n';

	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 309 ms 18536 KB Output is correct
2 Correct 178 ms 21896 KB Output is correct
3 Correct 271 ms 23144 KB Output is correct
4 Correct 329 ms 22352 KB Output is correct
5 Correct 270 ms 27368 KB Output is correct
6 Correct 228 ms 21864 KB Output is correct
7 Correct 187 ms 21736 KB Output is correct
8 Correct 97 ms 21224 KB Output is correct
9 Correct 96 ms 21224 KB Output is correct
10 Correct 92 ms 21228 KB Output is correct
11 Correct 343 ms 27876 KB Output is correct
12 Correct 353 ms 27756 KB Output is correct
13 Correct 354 ms 25964 KB Output is correct
14 Correct 352 ms 25960 KB Output is correct
15 Correct 289 ms 21612 KB Output is correct
16 Correct 282 ms 21608 KB Output is correct
17 Correct 276 ms 20840 KB Output is correct
18 Correct 309 ms 26988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 14464 KB Output is correct
2 Correct 8 ms 14464 KB Output is correct
3 Correct 9 ms 14464 KB Output is correct
4 Correct 9 ms 14464 KB Output is correct
5 Correct 9 ms 14464 KB Output is correct
6 Correct 9 ms 14464 KB Output is correct
7 Correct 8 ms 14464 KB Output is correct
8 Correct 9 ms 14464 KB Output is correct
9 Correct 8 ms 14464 KB Output is correct
10 Correct 9 ms 14464 KB Output is correct
11 Correct 8 ms 14464 KB Output is correct
12 Correct 11 ms 14496 KB Output is correct
13 Correct 8 ms 14464 KB Output is correct
14 Correct 9 ms 14464 KB Output is correct
15 Correct 9 ms 14464 KB Output is correct
16 Correct 9 ms 14464 KB Output is correct
17 Correct 12 ms 14464 KB Output is correct
18 Correct 10 ms 14464 KB Output is correct
19 Correct 12 ms 14464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 14464 KB Output is correct
2 Correct 8 ms 14464 KB Output is correct
3 Correct 9 ms 14464 KB Output is correct
4 Correct 9 ms 14464 KB Output is correct
5 Correct 9 ms 14464 KB Output is correct
6 Correct 9 ms 14464 KB Output is correct
7 Correct 8 ms 14464 KB Output is correct
8 Correct 9 ms 14464 KB Output is correct
9 Correct 8 ms 14464 KB Output is correct
10 Correct 9 ms 14464 KB Output is correct
11 Correct 8 ms 14464 KB Output is correct
12 Correct 11 ms 14496 KB Output is correct
13 Correct 8 ms 14464 KB Output is correct
14 Correct 9 ms 14464 KB Output is correct
15 Correct 9 ms 14464 KB Output is correct
16 Correct 9 ms 14464 KB Output is correct
17 Correct 12 ms 14464 KB Output is correct
18 Correct 10 ms 14464 KB Output is correct
19 Correct 12 ms 14464 KB Output is correct
20 Correct 16 ms 14720 KB Output is correct
21 Correct 18 ms 14848 KB Output is correct
22 Correct 17 ms 14764 KB Output is correct
23 Correct 16 ms 14848 KB Output is correct
24 Correct 23 ms 14848 KB Output is correct
25 Correct 19 ms 14764 KB Output is correct
26 Correct 19 ms 14764 KB Output is correct
27 Correct 18 ms 14720 KB Output is correct
28 Correct 19 ms 14848 KB Output is correct
29 Correct 18 ms 14720 KB Output is correct
30 Correct 14 ms 14720 KB Output is correct
31 Correct 13 ms 14720 KB Output is correct
32 Correct 19 ms 14976 KB Output is correct
33 Correct 19 ms 14976 KB Output is correct
34 Correct 19 ms 14848 KB Output is correct
35 Correct 19 ms 15000 KB Output is correct
36 Correct 19 ms 14976 KB Output is correct
37 Correct 18 ms 14756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 309 ms 18536 KB Output is correct
2 Correct 178 ms 21896 KB Output is correct
3 Correct 271 ms 23144 KB Output is correct
4 Correct 329 ms 22352 KB Output is correct
5 Correct 270 ms 27368 KB Output is correct
6 Correct 228 ms 21864 KB Output is correct
7 Correct 187 ms 21736 KB Output is correct
8 Correct 97 ms 21224 KB Output is correct
9 Correct 96 ms 21224 KB Output is correct
10 Correct 92 ms 21228 KB Output is correct
11 Correct 343 ms 27876 KB Output is correct
12 Correct 353 ms 27756 KB Output is correct
13 Correct 354 ms 25964 KB Output is correct
14 Correct 352 ms 25960 KB Output is correct
15 Correct 289 ms 21612 KB Output is correct
16 Correct 282 ms 21608 KB Output is correct
17 Correct 276 ms 20840 KB Output is correct
18 Correct 309 ms 26988 KB Output is correct
19 Correct 9 ms 14464 KB Output is correct
20 Correct 8 ms 14464 KB Output is correct
21 Correct 9 ms 14464 KB Output is correct
22 Correct 9 ms 14464 KB Output is correct
23 Correct 9 ms 14464 KB Output is correct
24 Correct 9 ms 14464 KB Output is correct
25 Correct 8 ms 14464 KB Output is correct
26 Correct 9 ms 14464 KB Output is correct
27 Correct 8 ms 14464 KB Output is correct
28 Correct 9 ms 14464 KB Output is correct
29 Correct 8 ms 14464 KB Output is correct
30 Correct 11 ms 14496 KB Output is correct
31 Correct 8 ms 14464 KB Output is correct
32 Correct 9 ms 14464 KB Output is correct
33 Correct 9 ms 14464 KB Output is correct
34 Correct 9 ms 14464 KB Output is correct
35 Correct 12 ms 14464 KB Output is correct
36 Correct 10 ms 14464 KB Output is correct
37 Correct 12 ms 14464 KB Output is correct
38 Correct 16 ms 14720 KB Output is correct
39 Correct 18 ms 14848 KB Output is correct
40 Correct 17 ms 14764 KB Output is correct
41 Correct 16 ms 14848 KB Output is correct
42 Correct 23 ms 14848 KB Output is correct
43 Correct 19 ms 14764 KB Output is correct
44 Correct 19 ms 14764 KB Output is correct
45 Correct 18 ms 14720 KB Output is correct
46 Correct 19 ms 14848 KB Output is correct
47 Correct 18 ms 14720 KB Output is correct
48 Correct 14 ms 14720 KB Output is correct
49 Correct 13 ms 14720 KB Output is correct
50 Correct 19 ms 14976 KB Output is correct
51 Correct 19 ms 14976 KB Output is correct
52 Correct 19 ms 14848 KB Output is correct
53 Correct 19 ms 15000 KB Output is correct
54 Correct 19 ms 14976 KB Output is correct
55 Correct 18 ms 14756 KB Output is correct
56 Correct 281 ms 23016 KB Output is correct
57 Correct 269 ms 24296 KB Output is correct
58 Correct 217 ms 20840 KB Output is correct
59 Correct 231 ms 21224 KB Output is correct
60 Correct 245 ms 21864 KB Output is correct
61 Correct 238 ms 20976 KB Output is correct
62 Correct 249 ms 23272 KB Output is correct
63 Correct 205 ms 21736 KB Output is correct
64 Correct 227 ms 21740 KB Output is correct
65 Correct 275 ms 21864 KB Output is correct
66 Correct 219 ms 21992 KB Output is correct
67 Correct 277 ms 22504 KB Output is correct
68 Correct 254 ms 22504 KB Output is correct
69 Correct 258 ms 22408 KB Output is correct
70 Correct 309 ms 22632 KB Output is correct
71 Correct 263 ms 22380 KB Output is correct
72 Correct 239 ms 22400 KB Output is correct
73 Correct 363 ms 22784 KB Output is correct
74 Correct 120 ms 21992 KB Output is correct
75 Correct 133 ms 21992 KB Output is correct
76 Correct 395 ms 28264 KB Output is correct
77 Correct 364 ms 28520 KB Output is correct
78 Correct 384 ms 26212 KB Output is correct
79 Correct 319 ms 22376 KB Output is correct
80 Correct 321 ms 21996 KB Output is correct
81 Correct 132 ms 22120 KB Output is correct
82 Correct 336 ms 21736 KB Output is correct
83 Correct 301 ms 21608 KB Output is correct
84 Correct 335 ms 21740 KB Output is correct
85 Correct 230 ms 22372 KB Output is correct
86 Correct 216 ms 22380 KB Output is correct
87 Correct 253 ms 22508 KB Output is correct
88 Correct 257 ms 22504 KB Output is correct
89 Correct 241 ms 22376 KB Output is correct
90 Correct 367 ms 28320 KB Output is correct
91 Correct 305 ms 25448 KB Output is correct
92 Correct 238 ms 22376 KB Output is correct
93 Correct 352 ms 22376 KB Output is correct
94 Correct 378 ms 22632 KB Output is correct
95 Correct 309 ms 22352 KB Output is correct
96 Correct 459 ms 28404 KB Output is correct
97 Correct 452 ms 28500 KB Output is correct
98 Correct 408 ms 28520 KB Output is correct
99 Correct 379 ms 28456 KB Output is correct
100 Correct 356 ms 28392 KB Output is correct
101 Correct 383 ms 28516 KB Output is correct
102 Correct 377 ms 28524 KB Output is correct
103 Correct 282 ms 23324 KB Output is correct